Current Location: Home> Latest Articles> How to Cut Images Using PHP and GD Library | Complete Guide with Code Example

How to Cut Images Using PHP and GD Library | Complete Guide with Code Example

M66 2025-07-29

Introduction

With the growth of the internet, image processing has become an essential part of modern website development. Image slicing is a common requirement that allows you to split a large image into smaller parts or combine smaller images into one. In this article, we will show you how to use PHP along with the GD Library to achieve image slicing, with code examples included.

Preparation

Before starting, ensure that PHP and the GD library are installed on your server. The GD library is a powerful image processing tool that allows you to generate, resize, watermark, and perform other image manipulations. If you're unsure whether GD is installed, you can check it using the phpinfo() function.

Loading the Image

First, we need to load the image that we want to slice into memory. PHP provides functions like imagecreatefromjpeg(), imagecreatefrompng(), and imagecreatefromgif() to create image resources from different formats.

Here’s a simple code example for loading a JPEG image:

<?php<br>$img = imagecreatefromjpeg('example.jpg');

Slicing the Image

Next, we use the GD library’s imagecopyresampled() function to slice the original image into smaller parts. The key is determining the slicing position and size.

The following code demonstrates how to slice a 400x400 image into four 200x200 smaller images:

<?php<br>// Create four 200x200 blank image resources<br>$smallImg1 = imagecreatetruecolor(200, 200);<br>$smallImg2 = imagecreatetruecolor(200, 200);<br>$smallImg3 = imagecreatetruecolor(200, 200);<br>$smallImg4 = imagecreatetruecolor(200, 200);<br><br>// Slice the image<br>imagecopyresampled($smallImg1, $img, 0, 0, 0, 0, 200, 200, 400, 400);<br>imagecopyresampled($smallImg2, $img, 0, 0, 200, 0, 200, 200, 400, 400);<br>imagecopyresampled($smallImg3, $img, 0, 0, 0, 200, 200, 200, 400, 400);<br>imagecopyresampled($smallImg4, $img, 0, 0, 200, 200, 200, 200, 400, 400);<br><br>// Save the sliced images<br>imagejpeg($smallImg1, 'smallImg1.jpg');<br>imagejpeg($smallImg2, 'smallImg2.jpg');<br>imagejpeg($smallImg3, 'smallImg3.jpg');<br>imagejpeg($smallImg4, 'smallImg4.jpg');

In the example above, we first create four blank image resources of size 200x200. Then, using the imagecopyresampled() function, we slice the original image into four smaller ones and save them to files.

Cleaning Up Resources

Once the image slicing is done, we need to release the memory occupied by the image resources. This can be achieved using the imagedestroy() function:

<?php<br>imagedestroy($smallImg1);<br>imagedestroy($smallImg2);<br>imagedestroy($smallImg3);<br>imagedestroy($smallImg4);

By calling the imagedestroy() function, we free up the memory used by imagecreatetruecolor() and imagecopyresampled() to make resources available for other tasks.

Conclusion

This article introduced how to use PHP and the GD library to perform image slicing. First, we load the image into memory, then use the imagecopyresampled() function to slice it into smaller images and save them. Finally, we clean up resources using the imagedestroy() function. We hope this tutorial helps you better understand and use PHP and the GD library for image slicing.