Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating an image in php, display works, saving to file does not

Tags:

php

image

I am creating an image via a php script using imagepng. This works fine and displays well on the website. even saving-as gives me a valid .png file

header( "Content-type: image/png" );
imagepng($my_img);
$save = "../sigs/". strtolower($name) .".png";
//imagepng($my_img, $save, 0, NULL);
imagepng($my_img, $save);

This is the end part of the code I use to generate the file and return it as picture on the website. but both options (tried the one marked out as well) dont save the file to the webserver for later use. The folder where the file is written is even set to chmod 777 at the moment to rule out any issues on that front. the $name is for sure a valid string without spaces.

like image 559
Fons Avatar asked Sep 11 '09 23:09

Fons


2 Answers

Make sure that PHP has permissions to write files to that folder. chmod probably only affects FTP users or particular users.

And try one at a time. i.e.:

header( "Content-type: image/png" );
imagepng($my_img);

then

$save = "../sigs/". strtolower($name) .".png";
imagepng($my_img, $save);

So that you can isolate errors.

Attempt saving in the same folder as the script first, see if there's any problem.

$save = strtolower($name) .".png";
imagepng($my_img, $save);
like image 86
mauris Avatar answered Nov 10 '22 11:11

mauris


Thanks a lot for you help on clearing my mind and having me look from a different angle. All had to do with rights of the file.

As the script generated the file, the rights are not set correct and overwriting is not possible.

after taking out:

header( "Content-type: image/png" );
imagepng($my_img);

I received an error message about not being able to write. when is set the file manual to chmod 755, the script worked like a charm.

so the new code now looks like this:

header( "Content-type: image/png" );
imagepng($my_img);
$save = "../sigs/". strtolower($name) .".png";
chmod($save,0755);
imagepng($my_img, $save, 0, NULL);
imagedestroy($my_img);

Setting the file to be writeable fixed the issue and all is working as intended.

Best regards Fons

like image 11
Fons Avatar answered Nov 10 '22 09:11

Fons