I want to crop an image from the center in the size 200 * 130 the image to be cropped may vary in size, if the image is smaller we wont crop it i know how to this part where i can check height and with of image but kind of struck into the thing of cropping from the middle of the image As i cant figure it out how to keep the center as crop point and than outward crop it
GD comes bundled with all PHP installations from version 4.3.6 onwards so chances are, you have it.
Here's the steps you need to take...
imagecreatefrom*()
functions. The one you use depends on the type of image you're dealing withimagesx()
and imagesy()
imagecopy()
Find crop coordinates
$width = imagesx($img);
$height = imagesy($img);
$centreX = round($width / 2);
$centreY = round($height / 2);
$cropWidth = 200;
$cropHeight = 130;
$cropWidthHalf = round($cropWidth / 2); // could hard-code this but I'm keeping it flexible
$cropHeightHalf = round($cropHeight / 2);
$x1 = max(0, $centreX - $cropWidthHalf);
$y1 = max(0, $centreY - $cropHeightHalf);
$x2 = min($width, $centreX + $cropWidthHalf);
$y2 = min($height, $centreY + $cropHeightHalf);
Feel free to use my image manipulation class, it should make some aspects much easier - https://gist.github.com/880506
$im = new ImageManipulator('/path/to/image');
$centreX = round($im->getWidth() / 2);
$centreY = round($im->getHeight() / 2);
$x1 = $centreX - 100;
$y1 = $centreY - 65;
$x2 = $centreX + 100;
$y2 = $centreY + 65;
$im->crop($x1, $y1, $x2, $y2); // takes care of out of boundary conditions automatically
$im->save('/path/to/cropped/image');
Here is a native implementation of a function (called cropAlign
) that can crop an image to a given width and height with align to the 9 standard points (4 edges, 4 corners, 1 center).
Just pass the image, the desired size of the crop, and the alignment on the two axis (you can use left
, center
, right
or top
, middle
, bottom
irregardless from the axis) for the cropAlign
function.
Description
cropAlign(resource $image, int $width, int $height, string $horizontalAlign = 'center', string $verticalAlign = 'middle')
Parameters
image
: An image resource, returned by one of the image creation functions, such asimagecreatetruecolor()
.width
: Width of the final cropped image.height
: Height of the final cropped image.horizontalAlign
: Where the crop should be aligned along the horizontal axis. Possible values are:left
/top
,center
/middle
,right
/bottom
.verticalAlign
: Where the crop should be aligned along the vertical axis. Possible values are:left
/top
,center
/middle
,right
/bottom
.Return Values
Return cropped image resource on success or
FALSE
on failure. This comes fromimagecrop()
.
function cropAlign($image, $cropWidth, $cropHeight, $horizontalAlign = 'center', $verticalAlign = 'middle') {
$width = imagesx($image);
$height = imagesy($image);
$horizontalAlignPixels = calculatePixelsForAlign($width, $cropWidth, $horizontalAlign);
$verticalAlignPixels = calculatePixelsForAlign($height, $cropHeight, $verticalAlign);
return imageCrop($image, [
'x' => $horizontalAlignPixels[0],
'y' => $verticalAlignPixels[0],
'width' => $horizontalAlignPixels[1],
'height' => $verticalAlignPixels[1]
]);
}
function calculatePixelsForAlign($imageSize, $cropSize, $align) {
switch ($align) {
case 'left':
case 'top':
return [0, min($cropSize, $imageSize)];
case 'right':
case 'bottom':
return [max(0, $imageSize - $cropSize), min($cropSize, $imageSize)];
case 'center':
case 'middle':
return [
max(0, floor(($imageSize / 2) - ($cropSize / 2))),
min($cropSize, $imageSize),
];
default: return [0, $imageSize];
}
}
Here are some crop examples using this image of the Utah teapot.
$im = imagecreatefrompng('https://i.stack.imgur.com/NJcML.png');
imagePng(cropAlign($im, 200, 250, 'center', 'middle'));
imagePng(cropAlign($im, 300, 150, 'left', 'top'));
imagePng(cropAlign($im, 1000, 250, 'right', 'middle'));
cropAlign($im, 200, 250, 'center', 'middle')
cropAlign($im, 300, 150, 'left', 'top')
cropAlign($im, 1000, 250, 'right', 'middle')
Jeez, why are you doing it the hard way? Just simply set the x and y positions as the amount to crop / 2
$imageSize = getimagesize('thumbnail.png');
$croppedImage = imagecrop(imagecreatefrompng('thumbnail.png'), ['x' => 0, 'y' => ($imageSize[1]-$imageSize[0]*(9/16))/2, 'width' => $imageSize[0], 'height' => $imageSize[0]*(9/16)]);
notice how I used my $imageSize[0]*(9/16), which is the amount i am cropping by in the y direction, and i subtracted that from the original image height to find crop amount, then divided by 2. If you want to do the same for width, simply follow the same steps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With