Current Location: Home> Latest Articles> PHP Image Cropping and Resizing: How to Handle Image Dimensions and Cropping in PHP

PHP Image Cropping and Resizing: How to Handle Image Dimensions and Cropping in PHP

M66 2025-06-13

How to Crop and Resize Images in PHP

In today's digital age, image processing is a common task. Whether you're developing a website or a mobile app, you often need to crop and resize images. This article will demonstrate how to crop and resize images in PHP, with relevant code examples provided to help you get started.

Overview

Before we begin, make sure the GD library extension for PHP is installed. The GD library is a popular graphics library that provides a set of functions for image processing. You can confirm whether the GD library is installed by using the `phpinfo()` function.

Image Cropping

Image cropping refers to extracting a specific region from the original image. By cropping, we can isolate and keep the desired portion of the image. Below is an example code that demonstrates how to crop an image using the GD library.

<?php
// Original image path
$sourceImagePath = 'path/to/source/image.jpg';
// Create a new image resource
$sourceImage = imagecreatefromjpeg($sourceImagePath);

// Cropping starting coordinates
$x = 100;
$y = 100;

// Width and height of the cropped area
$width = 200;
$height = 200;

// Create a new image resource for the cropped image
$croppedImage = imagecreatetruecolor($width, $height);

// Crop the image
imagecopy($croppedImage, $sourceImage, 0, 0, $x, $y, $width, $height);

// Save the cropped image
$savePath = 'path/to/save/cropped/image.jpg';
imagejpeg($croppedImage, $savePath);

// Free resources
imagedestroy($sourceImage);
imagedestroy($croppedImage);

echo 'Image cropped successfully, the new image is saved at: ' . $savePath;
?>

In the example above, we use the `imagecreatefromjpeg()` function to load the original image. Then, we specify the starting coordinates and the width and height of the area to crop. Finally, we use the `imagejpeg()` function to save the cropped image.

Image Resizing

Image resizing refers to changing the dimensions of an image. By resizing, we can adjust the image to fit a specific size. Below is an example code that demonstrates how to resize an image using the GD library.

<?php
// Original image path
$sourceImagePath = 'path/to/source/image.jpg';
// Create a new image resource
$sourceImage = imagecreatefromjpeg($sourceImagePath);

// Resized width and height
$newWidth = 400;
$newHeight = 400;

// Create a new image resource for the resized image
$scaledImage = imagescale($sourceImage, $newWidth, $newHeight);

// Save the resized image
$savePath = 'path/to/save/scaled/image.jpg';
imagejpeg($scaledImage, $savePath);

// Free resources
imagedestroy($sourceImage);
imagedestroy($scaledImage);

echo 'Image resized successfully, the new image is saved at: ' . $savePath;
?>

In the example above, we use the `imagecreatefromjpeg()` function to create an image resource from the original image. We then specify the new width and height for the resized image, use the `imagescale()` function to create the resized image, and finally save the resized image using the `imagejpeg()` function.

Conclusion

By using the GD library extension and the relevant functions, we can easily crop and resize images in PHP. The code examples provided in this article show how to perform these operations and should help you handle images more efficiently. For further details on other functions and methods of the GD library, please refer to the official documentation.