crop image top-middle

See all posts See thread Reply

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
Re: crop image top-middle new!
by Yaser Riaz, 2 years, 10 months ago
Hi, by using your code, the image is being saved but it is all black, can you tell me what the issue could beReply