Current Location: Home> Latest Articles> How to Crop an Image Using the imagesetclip() Function and Draw Various Shapes in the Cropped Area

How to Crop an Image Using the imagesetclip() Function and Draw Various Shapes in the Cropped Area

M66 2025-06-28

In PHP, the imagesetclip() function is a very useful image processing tool that allows developers to define a clipping region, within which only the graphics will be manipulated. By using this function properly, we can draw various shapes (such as rectangles, lines, etc.) on an image, and these shapes will only appear within the clipping region. This article will explain in detail how to use the imagesetclip() function to crop an image and draw various shapes within the cropped area.

1. What is the imagesetclip() function?

The imagesetclip() function is used to set the clipping region for an image. The clipping region is a rectangular area, and only the content within this area will be displayed or manipulated. It is typically used in conjunction with image drawing functions to help developers control the area where graphics are drawn.

2. How to use the imagesetclip() function?

To use the imagesetclip() function, we first need to create an image resource. Then, we define the clipping region using the imageclip() function. Finally, various shapes can be drawn within the clipping region.

Basic Syntax

bool imagesetclip(resource $image);
  • $image: The image resource, which is created using functions like imagecreate().

  • Return Value: Returns true on success, or false on failure.

3. Cropping the Image Using imagesetclip()

The following example demonstrates how to use the imagesetclip() function to crop an image:

<?php
// Create a blank image
$image = imagecreatetruecolor(500, 500);
<p>// Select colors<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
$black = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Fill the background with white<br>
imagefill($image, 0, 0, $white);</p>
<p>// Define the clipping region as a rectangle from (100,100) to (400,400)<br>
imageclip($image, 100, 100, 300, 300);</p>
<p>// Use imagesetclip() to crop the image<br>
imagesetclip($image);</p>
<p>// Draw a black rectangle within the clipping region<br>
imagerectangle($image, 150, 150, 350, 350, $black);</p>
<p>// Output the image<br>
header("Content-Type: image/png");<br>
imagepng($image);</p>
<p>// Free image resources<br>
imagedestroy($image);<br>
?><br>

In this example, we first create a 500x500 pixel image and fill the background with white. Then, we define a clipping region as a rectangle from (100, 100) to (400, 400). After calling the imagesetclip() function, the clipping region is set. We then draw a black rectangle within this region and output the result.

4. Drawing Shapes

Within the clipping region, you can draw various shapes, such as lines, rectangles, ellipses, etc. Here are some common methods for drawing shapes:

Drawing a Line

// Draw a line from (150, 150) to (350, 350)
imageline($image, 150, 150, 350, 350, $black);

Drawing an Ellipse

// Draw an ellipse within the clipping region
imageellipse($image, 250, 250, 200, 100, $black);

Drawing a Polygon

// Draw a pentagon
$points = [200, 100, 300, 100, 350, 200, 250, 300, 150, 200];
imagepolygon($image, $points, 5, $black);

5. Things to Keep in Mind

  • Before calling imagesetclip(), ensure the clipping region has been set using imageclip() or other similar functions.

  • imagesetclip() only affects the content within the clipping region; anything drawn outside this region will not be displayed.

  • The clipping region can be any rectangle, and you can adjust the parameters to control the size and position of the region.

6. Example: Draw Shapes in the Clipped Area and Save the Result

<?php
// Create a blank image
$image = imagecreatetruecolor(500, 500);
<p>// Select colors<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
$black = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Fill the background with white<br>
imagefill($image, 0, 0, $white);</p>
<p>// Define the clipping region as a rectangle from (100,100) to (400,400)<br>
imageclip($image, 100, 100, 300, 300);</p>
<p>// Use imagesetclip() to crop the image<br>
imagesetclip($image);</p>
<p>// Draw a black rectangle within the clipping region<br>
imagerectangle($image, 150, 150, 350, 350, $black);</p>
<p>// Draw a line within the clipping region<br>
imageline($image, 150, 150, 350, 350, $black);</p>
<p>// Draw an ellipse<br>
imageellipse($image, 250, 250, 200, 100, $black);</p>
<p>// Output the image<br>
imagepng($image, 'output_image.png');</p>
<p>// Free image resources<br>
imagedestroy($image);<br>
?><br>

In the example above, we draw a rectangle, a line, and an ellipse within the clipping region. Finally, we save the image as output_image.png to view the shapes within the cropped area.