Current Location: Home> Latest Articles> How to crop an image with imagegetclip and apply scaling at the same time

How to crop an image with imagegetclip and apply scaling at the same time

gitbox 2025-05-26

1. Preparation

First, make sure that the PHP environment has enabled GD library support. The GD library is the basic library for PHP to process images, and is often used for image creation, cropping, scaling, synthesis and other operations.

You can use the following code to check whether the GD library is enabled:

 <?php
if (extension_loaded('gd')) {
    echo "GDThe library is enabled";
} else {
    echo "GDLibrary not enabled,Please install or enableGDLibrary extension";
}
?>

2. Load the original image

Load image resources through the GD library functions imagecreatefromjpeg() , imagecreatefrommpng() , etc. Here we take JPEG as an example:

 <?php
$sourcePath = 'gitbox.net/images/sample.jpg'; // Note that the domain name has been replaced withgitbox.net
$srcImg = imagecreatefromjpeg($sourcePath);
if (!$srcImg) {
    die("Unable to load the picture");
}
?>

3. Implement cropping and scaling functions

Here is a custom function that mimics the behavior of imagegetclip :

 <?php
/**
 * Crop and scale the image
 * 
 * @param resource $srcImg  Source image resources
 * @param int $clipX        Cropping starting pointXcoordinate
 * @param int $clipY        Cropping starting pointYcoordinate
 * @param int $clipWidth    Crop width
 * @param int $clipHeight   Cropping height
 * @param int $newWidth     Target width(After zooming)
 * @param int $newHeight    Target height(After zooming)
 * 
 * @return resource         裁剪After zooming的新图像资源
 */
function imagegetclip($srcImg, $clipX, $clipY, $clipWidth, $clipHeight, $newWidth, $newHeight) {
    // Create a target image resource
    $dstImg = imagecreatetruecolor($newWidth, $newHeight);

    // Stay transparent(againstPNGandGIF)
    imagealphablending($dstImg, false);
    imagesavealpha($dstImg, true);

    // Crop and scale
    imagecopyresampled(
        $dstImg,       // Target image
        $srcImg,       // Source image
        0, 0,          // Target image起点coordinate
        $clipX, $clipY,// Source imageCropping starting pointcoordinate
        $newWidth, $newHeight,   // Target image尺寸
        $clipWidth, $clipHeight  // Source image裁剪尺寸
    );

    return $dstImg;
}
?>

4. Use examples

The following shows how to call this function to crop and scale the image and save the result:

 <?php
// 载入Source image
$sourcePath = 'gitbox.net/images/sample.jpg';
$srcImg = imagecreatefromjpeg($sourcePath);

// 设定裁剪参数and缩放尺寸
$clipX = 100;
$clipY = 50;
$clipWidth = 200;
$clipHeight = 150;
$newWidth = 400;  // enlarge2Double
$newHeight = 300;

// Calling the clipping and scaling function
$dstImg = imagegetclip($srcImg, $clipX, $clipY, $clipWidth, $clipHeight, $newWidth, $newHeight);

// Output image to file
imagejpeg($dstImg, 'gitbox.net/images/output.jpg', 90);

// Free up resources
imagedestroy($srcImg);
imagedestroy($dstImg);

echo "Crop and scale完成,The image has been saved to output.jpg";
?>

5. Summary

Through the above steps, we use the GD library to implement the core function of the imagegetclip function - crop the specified area of ​​the image and scale it to the target size. This method is not only suitable for JPEG, but also can easily expand the support for PNG, GIF and other formats. Just replace the image loading function and save function.

This method of processing combining cropping and scaling is widely used in avatar cropping, thumbnail generation, image editing tools and other scenarios.