Page 1 of 1
Image Crop
Posted: Mon Jan 10, 2011 5:25 pm
by TonsOfFun
I am trying to create a script that will crop an image to 200x200 but I keep getting the error:
The image “[location/to/page]” cannot be displayed because it contains errors.
Here's my code:
Code: Select all
$orig = 'photos/1c23e596271d8eabb2e58d65c79b4beb.jpg';
list($width,$height) = getimagesize($orig);
$imageResized = imagecreatetruecolor(200, 200);
$imageTmp = imagecreatefromjpeg ($orig);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, 200, 200, $width, $height);
imagejpeg($imageResized,100);
Thanks.
Re: Image Crop
Posted: Mon Jan 10, 2011 5:45 pm
by McInfo
1. Are you sure the source image file exists?
2. An appropriate Content-Type header should be sent before sending image data.
Code: Select all
header('Content-Type: image/jpeg');
3. Do you want to create a file named "100", or do you want 100% quality?
Code: Select all
imagejpeg($imageResized, null, 100); // ($image, $filename, $quality)
Re: Image Crop
Posted: Mon Jan 10, 2011 5:46 pm
by mikecampbell
I think you probably want to do this:
Code: Select all
imagejpeg($imageResized,null,100);
Re: Image Crop
Posted: Mon Jan 10, 2011 6:17 pm
by TonsOfFun
McInfo wrote:1. Are you sure the source image file exists?
2. An appropriate Content-Type header should be sent before sending image data.
Code: Select all
header('Content-Type: image/jpeg');
3. Do you want to create a file named "100", or do you want 100% quality?
Code: Select all
imagejpeg($imageResized, null, 100); // ($image, $filename, $quality)
Yes, it does exist.
I have the appropriate header. It's the first thing, right before the DOCTYPE declaration.
I changed it to
Code: Select all
imagejpeg($imageResized,null,100);
and I'm getting the same error.
Re: Image Crop
Posted: Mon Jan 10, 2011 7:39 pm
by McInfo
TonsOfFun wrote:right before the DOCTYPE declaration.
That could be the problem. Image files do not contain HTML. Image data and HTML are not sent in the same server response.
Re: Image Crop
Posted: Mon Jan 10, 2011 8:26 pm
by TonsOfFun
Thanks McInfo, that solved it.