PHP: Resizing an image stored on my web host?

S B

New member
I have been trying to simply resize an image that is stored on my webhost for about a week now. Every example i try to implement does not work, even if I copy and paste them directly.

I developed my own code for it from scratch, and after running, I get a resized image saved on my webhost, but the image is completely black. I cannot figure out why.

Here is my code:
---------------------------------------------------
$oldsize=getimagesize($uploadFilename);
$oldwidth=$oldsize[0];
$oldheight=$oldsize[1];

$scale=$oldwidth/50;

$new_width=$oldwidth/$scale;
$new_height=$oldheight/$scale;

$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($uploadFilename);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $oldwidth, $oldheight);

imagejpeg($imageResized, $uploadsDirectory.'resized'.$now.'.jpg', 85);

// Free up memory
imagedestroy($imageResized);
---------------------------------------------------

Any help would be greatly appreciated.
 
Well, here's one "problem", sort of:

imagejpeg($imageResized, $uploadsDirectory.'resized'.$now.'.jpg', 85);

Try adding a space before and after each "." where you cat the filename together. I ALWAYS use spaces with I cat stuff, even though there doesn't seem to be a need to do so according to the PHP specs.

If you're not getting the image in the right place, I can't see why based on this code. I would suggest you rewrite the code to echo some messages if one of the image functions fails. Some of these functions return a bool value on success or failure, so you can see where it's stumbling.

1. Add an "or die('error message')" after the imagecreatetruecolor functions to see if it actually creates the blank image resource.

2. Same thing for imagecreatefromjpeg.

3 imagecopyresampled returns true on success and false on failure. Test for that.

4. Same as #3 for imagejpeg.

Also, have the output of the imagejpeg function echoed to the browser, so you can see if there's an image there before you try to save it. After you create the image, just call imagejpeg($imageResized), then run the script in a browser. This way, you can see if there's really an image there before saving it.

One more note: in the directory on your web host where the image is being written: are the directory permissions correct for the system user to write to the location? If the user account that's running the script on the server can't write to the directory, you'll get nothing. Probably not the issue, but a thought.
 
Back
Top