Reputation: 823
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
Upvotes: 13
Views: 49529
Reputation: 164736
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);
Somebody implemented a handy class that encapsulates this logic here ~ ImageManipulator.php
Upvotes: 36
Reputation: 31058
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.sstatic.net/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')
Upvotes: 21
Reputation: 888
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.
Upvotes: 2
Reputation: 3029
This might help you.
function cropCentered($img, $w, $h)
{
$cx = $img->getWidth() / 2;
$cy = $img->getHeight() / 2;
$x = $cx - $w / 2;
$y = $cy - $h / 2;
if ($x < 0) $x = 0;
if ($y < 0) $y = 0;
return $img->crop($x, $y, $w, $h);
}
I'm assuming you're using the GD library. $img is a GD image, $w and $h are width and height, respectively, you want your new image to have. In your case, $w = 200, $h = 130.
Upvotes: 0