Reply to Re: crop image top-middle

Re: crop image top-middle new!
by Test, 17 years, 4 months ago
// Set our crop dimensions.
$width = 100;
$height = 75;
// Get dimensions of existing image
$dimensions = getimagesize('path/to/image');
// Prepare canvas
$canvas = imagecreatetruecolor($width,$height);
$piece = imagecreatefromjpeg('path/to/image');
// Prepare image resizing and crop -- Center crop location
$newwidth = $dimensions[0] / 2;
$newheight = $dimensions[1] / 2;
$cropLeft = ($newwidth/2) - ($width/2);
$cropHeight = ($newheight/2) - ($height/2);
// Generate the cropped image
imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight, $width, $height, $newwidth, $newheight);
// Write image or fail
if (imagejpeg($canvas,'path/to/save/file',90)) {
echo 'Image crop successful';
} else {
echo 'Image crop failed';
}
// Clean-up
imagedestroy($canvas);
imagedestroy($piece);
Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important