Crop Image From Center PHP

后端 未结 4 1041
梦如初夏
梦如初夏 2020-12-05 05:40

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

相关标签:
4条回答
  • 2020-12-05 06:24

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

    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

    Output

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

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

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

    0 讨论(0)
  • 2020-12-05 06:31

    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.

    0 讨论(0)
  • 2020-12-05 06:34

    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.

    0 讨论(0)
  • 2020-12-05 06:37

    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');
    
    0 讨论(0)
提交回复
热议问题