How to Resize/Crop Images in PHP with the GD Library

This tutorial will show you how to resize or crop images in the PHP programming language. Code examples are provided which can be copy and pasted into your project.

GD Library vs ImageMagick

Image resizing in PHP is performed by an optional image processing library. The two most popular image processing libraries for PHP are GD and ImageMagick.

Both work well, but I’ll be sticking to the GD Library for this tutorial as it is the most popular and supported.

Installing the GD PHP Extension

The GD library extension is not always installed in the default PHP deployments provided on Linux distributions. To check if it is installed you can use the phpinfo() function to view information about your PHP installation – including the list of loaded extensions.

If you’re on the command line you can also run:

php -i | grep 'GD'
function image_resize($image_file_path, $resize_width, $resize_height, $crop = FALSE) {

    // Get the current width and height of the image
    list($width, $height) = getimagesize($image_file_path);

    // Calculate the ratio of the image
    $ratio = $width / $height;

    // If $crop is TRUE, do not stretch the image if the ratio cannot be fit into the new dimensions
    if ($crop) {

        // Calculate the required width and height for the crop depending on whether the image is wider than it is tall (landscape or portrait)
        if ($width > $height) {

            $width = ceil($width-($width*abs($ratio - ($resize_width / $resize_height))));

        } else {

            $height = ceil($height-($height*abs($ratio - ($resize_width / $resize_height))));

        }

        $new_width = $resize_width;
        $new_height = $resize_height;

    } else {

        if ($resize_width/$resize_height > $ratio) {

            $new_width = $resize_height*$ratio;
            $new_height = $resize_height;

        } else {

            $new_height = $resize_width/$ratio;
            $new_width = $resize_width;

        }

    }

    // Depending on the format of the image, different functions must be used to create an image object from the file
    // pathinfo() is used to determine the image type by the file extension
    switch (pathinfo($image_file_path)['extension]) {
        case 'png':
            $image = imagecreatefrompng($image_file_path);
            break;
        case 'gif':
            $image = imagecreatefromgif($image_file_path);
            break;
        default:
            $image = imagecreatefromjpeg($image_file_path);
    }

    // Create a new empty image object which will hold the resized image   
    $resized_image = imagecreatetruecolor($new_width, $new_height);

    // Copy and resize the contents of the original image into the resized image
    imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    // Return the resized image
    return $resized_image;

}

Saving the Resized Image

To save the resized image you will need to write it to a file.

Outputting the Image

To out the image directly to the web browser in PHP you will need to set the browser headers and echo the contents of the image.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment