Backend Development 4 min read

Image Cropping with PHP GD Library: Step‑by‑Step Guide

This article explains how to use PHP's GD library to verify its installation, define source and target dimensions, create a new image, crop the original using imagecopyresampled, save the result, and release resources, providing a complete step‑by‑step guide for image cropping in backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Image Cropping with PHP GD Library: Step‑by‑Step Guide

Image cropping is a common requirement in web development for adjusting image size and removing unwanted parts to fit different page layouts. In PHP, the GD library offers a set of powerful functions to handle such image manipulation tasks.

Code Example

First, ensure that the GD extension is installed and enabled. The following code checks for the GD library:

<?php
    // Check if GD is installed and enabled
    if (extension_loaded('gd') && function_exists('gd_info')) {
        echo "GD library is installed and enabled!";
    } else {
        echo "GD library is not installed or not enabled!";
    }
?>

Next, prepare the source image and the desired crop dimensions. Assuming the source image is source.jpg with a width of 800 px and height of 600 px, we want to crop it to 400 px × 300 px. The implementation is shown below:

<?php
    // Define source image and crop dimensions
    $sourceImage = "source.jpg"; // original image path
    $sourceWidth = 800;  // original width
    $sourceHeight = 600; // original height
    $cropWidth = 400;   // desired crop width
    $cropHeight = 300;  // desired crop height

    // Create a new true color image with the crop size
    $cropImage = imagecreatetruecolor($cropWidth, $cropHeight);

    // Copy and resample the source image into the new image
    imagecopyresampled($cropImage, $sourceImage, 0, 0, 0, 0, $cropWidth, $cropHeight, $sourceWidth, $sourceHeight);

    // Save the cropped image to a file
    imagejpeg($cropImage, "crop.jpg");

    // Free memory
    imagedestroy($sourceImage);
    imagedestroy($cropImage);
?>

In the code above, imagecreatetruecolor creates a blank canvas with the target dimensions, imagecopyresampled performs the actual cropping and scaling, and imagejpeg writes the result to crop.jpg . Finally, imagedestroy releases the image resources.

Through this example, it is clear that using PHP and the GD library for image cropping is straightforward: prepare the source image, specify the crop size, apply GD functions, and save the output. The GD library also supports other operations such as scaling, rotating, and adding watermarks, enabling developers to handle a wide range of image processing needs in their backend projects.

PHPImage CroppingGD Library
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.