Image Crop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Image Crop

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Image Crop

Post 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)
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Image Crop

Post by mikecampbell »

I think you probably want to do this:

Code: Select all

imagejpeg($imageResized,null,100); 
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Image Crop

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Image Crop

Post 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.
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Image Crop

Post by TonsOfFun »

Thanks McInfo, that solved it.
Post Reply