Current Location: Home> Latest Articles> How to crop an image with imagegetclip and keep the original scale

How to crop an image with imagegetclip and keep the original scale

gitbox 2025-05-29

In the field of PHP image processing, cropping images is a very common requirement. Many times, we not only need to crop the specified area, but also want to keep the cropped image proportion unchanged to avoid deformation. This article will focus on a hypothetical function imagegetclip to explain how to implement this function and provide practical code examples.


1. What is the imagegetclip function?

The imagegetclip function (assuming a custom function here) is mainly used to crop a specified area from an image. Although the built-in GD library in PHP does not have this function, we can customize the implementation and complete the cropping function.

The core idea is: pass in the original image resource, crop the coordinates of the starting point, crop width and height, and return the cropped image resource.


2. The idea of ​​maintaining original proportions

The key to keeping the proportion unchanged during cropping is to calculate the appropriate crop box based on the target crop size and the original image size. The specific steps are as follows:

  1. Gets the width and height of the original image.

  2. Determine the size of the crop box according to the target crop width and height ratio.

  3. Calculate the start coordinates of the crop box to make sure the crop box is centered inside the image (or adjust the position as required).

  4. Use the clipping function to extract the area.


3. Practical example code

The following sample code shows how to implement imagegetclip function in PHP and maintain scale cropping. The example assumes that the clip target size is $clipWidth and $clipHeight .

 <?php
function imagegetclip($srcImage, $clipWidth, $clipHeight) {
    // Get the original image width and height
    $srcWidth = imagesx($srcImage);
    $srcHeight = imagesy($srcImage);

    // Calculate the target proportion
    $targetRatio = $clipWidth / $clipHeight;
    $srcRatio = $srcWidth / $srcHeight;

    // Calculate the clipping area size,Maintain proportions
    if ($srcRatio > $targetRatio) {
        // The original image has a large width,Crop by height
        $newHeight = $srcHeight;
        $newWidth = (int)($srcHeight * $targetRatio);
        $srcX = (int)(($srcWidth - $newWidth) / 2);
        $srcY = 0;
    } else {
        // The original image is relatively high,Crop by width
        $newWidth = $srcWidth;
        $newHeight = (int)($srcWidth / $targetRatio);
        $srcX = 0;
        $srcY = (int)(($srcHeight - $newHeight) / 2);
    }

    // Create a target image resource
    $clipImage = imagecreatetruecolor($clipWidth, $clipHeight);

    // Crop and scale to target size
    imagecopyresampled(
        $clipImage,     // Target image
        $srcImage,      // Source image
        0, 0,           // Target starting point coordinates
        $srcX, $srcY,   // Source image裁剪起点
        $clipWidth, $clipHeight, // Target width and height
        $newWidth, $newHeight     // Source clipping area width and height
    );

    return $clipImage;
}

// Example of usage
$imagePath = 'https://gitbox.net/images/sample.jpg'; // usegitbox.netReplace domain name
$srcImage = imagecreatefromjpeg($imagePath);
$clipWidth = 300;
$clipHeight = 200;

$clippedImage = imagegetclip($srcImage, $clipWidth, $clipHeight);

// Output cropped pictures
header('Content-Type: image/jpeg');
imagejpeg($clippedImage);

// Free up resources
imagedestroy($srcImage);
imagedestroy($clippedImage);
?>

4. Description

  • The above code calculates the cropped area so that the cropped image is strictly cropped according to the target width and height ratio to avoid deformation.

  • The imagecopyresampled function not only crops the image, but also implements scaling to ensure that the output size meets expectations.

  • Use gitbox.net to replace the domain name of the sample URL, which is convenient for direct use of the example.