Current Location: Home> Latest Articles> How to Crop a Square Image Using PHP's imagecrop Function? Steps and Code Examples

How to Crop a Square Image Using PHP's imagecrop Function? Steps and Code Examples

gitbox 2025-06-09

How to Crop a Square Image Using PHP's imagecrop Function? Steps and Code Examples

When working with images in PHP, cropping is one of the common tasks. PHP provides several functions for image manipulation, and the imagecrop function is particularly useful for cropping. In this article, we’ll focus on how to use the imagecrop function to crop an image into a perfect square. We’ll also walk through the process using actual code examples.

1. Preparation

First, make sure the GD library is installed on your server, as imagecrop is part of the GD library. If you’re unsure whether it's installed, you can check using the following command:

php -m | grep gd

If the output includes gd, it means the GD library is installed. Otherwise, you’ll need to install and enable it.

2. Load the Image Resource

Before cropping, we need to load the image. Image resources are typically created using functions like imagecreatefromjpeg, imagecreatefrompng, or imagecreatefromgif, depending on the image format. For example, to load a JPEG image, use the following code:

$image = imagecreatefromjpeg('your-image.jpg');

This will return an image resource that we can use for further processing.

3. Calculate the Crop Area

To crop the image into a square, we need to calculate a square region. This can be done by comparing the width and height of the image. If the width is greater than the height, we crop a square section from the width, and vice versa. This ensures the cropped area is square.

Here’s the code to get the coordinates and dimensions for cropping:

$width = imagesx($image);  // Get image width
$height = imagesy($image); // Get image height
<p>$minDimension = min($width, $height);  // Use the smaller of width or height as square size</p>
<p>// Calculate crop starting coordinates<br>
$startX = ($width - $minDimension) / 2;<br>
$startY = ($height - $minDimension) / 2;<br>

4. Crop the Image Using imagecrop

Now that we have the crop area’s position and size, we can use the imagecrop function to perform the crop. This function requires a crop array containing the width, height, and starting coordinates.

Here’s how to crop the image:

$cropRect = [
    'x' => $startX,
    'y' => $startY,
    'width' => $minDimension,
    'height' => $minDimension
];
<p>$croppedImage = imagecrop($image, $cropRect);<br>

5. Output the Cropped Image

After cropping, you can use imagejpeg (for JPEG) or imagepng (for PNG) to output the image. To send it directly to the browser, use this code:

header('Content-Type: image/jpeg');
imagejpeg($croppedImage);

To save the cropped image to your server, specify a file path like this:

imagejpeg($croppedImage, 'cropped-image.jpg');

6. Free Up Resources

Finally, don’t forget to free the image resources to avoid memory leaks:

imagedestroy($image);
imagedestroy($croppedImage);

Full Example

<?php
// Load image
$image = imagecreatefromjpeg('your-image.jpg');
<p>// Get width and height<br>
$width = imagesx($image);<br>
$height = imagesy($image);</p>
<p>// Calculate square crop size<br>
$minDimension = min($width, $height);</p>
<p>// Calculate crop start coordinates<br>
$startX = ($width - $minDimension) / 2;<br>
$startY = ($height - $minDimension) / 2;</p>
<p>// Define crop area<br>
$cropRect = [<br>
'x' => $startX,<br>
'y' => $startY,<br>
'width' => $minDimension,<br>
'height' => $minDimension<br>
];</p>
<p>// Crop image<br>
$croppedImage = imagecrop($image, $cropRect);</p>
<p>// Output cropped image<br>
header('Content-Type: image/jpeg');<br>
imagejpeg($croppedImage);</p>
<p>// Clean up<br>
imagedestroy($image);<br>
imagedestroy($croppedImage);<br>
?><br>

7. Conclusion

By following the steps above, you can easily use PHP’s imagecrop function to crop an image into a square. The key is to calculate the crop area based on the original image dimensions to ensure the result is a square. PHP, with its GD library, offers a powerful yet straightforward approach to image processing, making tasks like cropping flexible and convenient.

If you need to perform additional image manipulations like resizing or rotating, you can continue using other functions provided by the GD library. We hope this guide helps you understand how to crop images with PHP.