16

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.

2
  • 1
    Maybe you should post the code where you actually save the image to the filesystem? Commented Sep 12, 2009 at 0:00
  • 2
    @rogeriopvl - the code where the image is saved into the filesystem is there. please read the PHP manual about imagepng() function. Commented Sep 12, 2009 at 6:54

4 Answers 4

42

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);
Sign up to request clarification or add additional context in comments.

Comments

12

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

Comments

4

Are you sure the relative path is correct? That can be a bit confusing if that script is called from another script.

You could try to change the path to:

$save = $_SERVER['DOCUMENT_ROOT'] . "/sigs/" . strtolower($name) . ".png";

Edit: And of course check the return value of imagepng() and your error log

Comments

2

Your code, Fons, provoked the usual problem I was having of displaying the Image created using the GD Library but disabling any Html code after the Php. By removing the line below

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

and only using the next 2 lines in Php I was able to save the file and then access the Image in the Html section (bottom line of code) without destroying the Html coding.

$save='./img/Graph.png'; chmod($save,0755);
imagepng($image,$save,0,NULL); imagedestroy($image);

<img width="500" height="350" align="top" alt="" src="./img/Graph.png" />

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.