Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop Image From Center PHP

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

like image 815
June Avatar asked Jul 31 '11 18:07

June


3 Answers

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...

  1. Create an image resource using one of the GD imagecreatefrom*() functions. The one you use depends on the type of image you're dealing with
  2. Determine the image dimensions using imagesx() and imagesy()
  3. Determine your crop coordinates using the following algorithm and crop using 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');
like image 121
Phil Avatar answered Oct 21 '22 14:10

Phil


Image crop with configurable alignment

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).

Alignment points

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.

Specification

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 as imagecreatetruecolor().
  • 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 from imagecrop().

Source code

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];
    }
}

Example usage

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'));

Input

Example input: Utah teapot

Output

cropAlign($im, 200, 250, 'center', 'middle')

Output #1

cropAlign($im, 300, 150, 'left', 'top')

Output #2

cropAlign($im, 1000, 250, 'right', 'middle')

Output #3

like image 38
totymedli Avatar answered Oct 21 '22 15:10

totymedli


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.

like image 2
Ethan SK Avatar answered Oct 21 '22 15:10

Ethan SK