Help with ImageCopyResampled

Hi, I’m a beginner to PHP and was hoping an expert could help me.

I would like to crop the central part of a long image in to a square. I have an image that looks like this:

Img51.imageshack.us/img51/5335/6cuy.png

I would like to crop it’s center in to a square like this:

img854.imageshack.us/img854/5963/9bo9.png

However, my images are turning out like this:

img202.imageshack.us/img202/6073/s731.jpg

Here is the PHP code that I am using but am having no luck whatsoever:

[php]if($OriginalHeight > $OriginalWidth){
$StartY = $OriginalHeight / 4;
$StartX = 0;
$EndY = $StartY * 3;
$EndX = $OriginalWidth;
}

if($OriginalHeight < $OriginalWidth){
$StartY = 0;
$StartX = $OriginalWidth / 4;
$EndY = $OriginalHeight;
$EndX = $StartX * 3;
}

if($OriginalHeight == $OriginalWidth){
$StartY = 0;
$StartX = $OriginalWidth;
$EndY = $OriginalHeight;
$EndX = $OriginalHeight;
}

imagecopyresampled($NewImage, $SrcImage, $EndX, $EndY, $StartX, $StartY, $NewImageSize, $NewImageSize, $OriginalWidth, $OriginalHeight);[/php]

PLease can you help me? Thank you.

looks to me like you’re placing the new image at the coordinates of the the old image.
change this line:
[php]imagecopyresampled($NewImage, $SrcImage, $EndX, $EndY, $StartX, $StartY, $NewImageSize, $NewImageSize, $OriginalWidth, $OriginalHeight);[/php]

to this:
[php]imagecopyresampled($NewImage, $SrcImage, 0, 0, $StartX, $StartY, $NewImageSize, $NewImageSize, $OriginalWidth, $OriginalHeight);[/php]

Let me know how you get on.
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service