imagepng Failing -- Please Help!

Hello,

I’m working on a php script that reads in all of the image files in a specified directory and then saves out thumbnail versions of each image file to that same directory, using the imagepng function.

This script works wonderfully, but only as long as the my php script is being executed from the same directory as the one I’m passing to imagepng. So if my images are in $_SERVER[‘DOCUMENT_ROOT’]/images/, my .php script needs to be in that directory as well or imagepng will return false.

Any ideas on how I might be able to get around this? For reference, my code looks like this (pseudo code):

[php]$thumbImage //my image Resource, correctly created, resized, and ready to go.
$dir //my destination directory which I’ve read the images from and will now be saving thumbnails to the path is based off of $_SERVER[‘DOCUMENT_ROOT’], rather than a URL.

imagepng($thumbImage, $dir); //Succeeds if my script is in $dir, fails otherwise.[/php]

Thanks very much for the help!

Are your folder permissions set to 755 or 777 if 755 fails ?

[php]chmod($dir,0755);
imagepng($thumbImage, $dir, 0, NULL);
imagedestroy($thumbImage);[/php]

Thanks for the help, lothop. Unfortunately, that doesn’t seem to be the issue. I’ve set all directories and their child directories to 777 with no change in behavior.

Is there a way I could catch an error from imagepng or something like that, so that I could see exactly what’s failing?

[php]error_reporting(E_ALL);[/php] At the top of your page.

Thanks for the error_reporting tip. From that I ended up turning on track_errors in my php.ini file and used $php_errormsg to print out the errors.

It turned out that when I was calling imagecreatefrompng earlier in the code I wasn’t prefixing the filename with the filepath, so it couldn’t find the image. That explains why the code would execute when in the same directory as the image itself, but not when it was in a different directory.

Great, I’m glad you got it working :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service