Current Location: Home> Latest Articles> Detailed explanation of coordinates and width and height settings when cropping images in imagecrop function

Detailed explanation of coordinates and width and height settings when cropping images in imagecrop function

gitbox 2025-05-29

In PHP, processing image cropping is a very common part of image processing tasks. Since PHP 5.5.0, PHP has built-in imagecrop function to crop part of an image, which is more concise and efficient to use than the previous complicated manual cropping.

This article will introduce in detail how to correctly use the imagecrop function, especially how to set the coordinates and width and height parameters passed in during cropping, avoiding common errors and traps.


1. Introduction to imagecrop function

The prototype of the imagecrop function is as follows:

 imagecrop(GdImage $image, array $rect): ?GdImage
  • $image : The image resource to be cropped.

  • $rect : An associative array that defines the coordinates and dimensions of the clipping area, which must contain the following keys:

    • x : The horizontal coordinate (in pixels, integer) of the upper left corner of the crop area.

    • y : The vertical coordinate (in pixels, integer) of the upper left corner of the crop area.

    • width : The width of the crop area (pixels).

    • height : The height of the crop area (pixel).

When the function succeeds, it returns the cropped new image resource, and when it fails, it returns false .


2. Specific meanings of coordinates and width and height parameters

  1. Coordinates (x, y)
    It indicates the position of the upper left corner of the crop box in the original picture. The coordinates are from the upper left corner as the origin (0,0), the x-axis grows to the right, and the y-axis grows to the downward.

  2. Width and height
    Represents the size of the crop box, in pixels. Must be a positive integer and cannot exceed the remaining available area of ​​the original image.


3. How to set parameters correctly?

  1. Ensure that the coordinates and sizes do not exceed the boundary of the original image <br> For example, if the original image width is $imgWidth and the height is $imgHeight , it must be satisfied:

     $x >= 0 && $y >= 0
    $x + $width <= $imgWidth
    $y + $height <= $imgHeight
    

    Otherwise, the cropping operation fails and returns false .

  2. Parameter type must be an integer <br> Non-integer arguments can cause errors or exceptions.

  3. Ensure that the width and height of the crop area are greater than 0
    When the width or height is 0 or negative, the cropping is invalid.


4. Sample code

Suppose we have an image example.jpg , crop it starting from coordinates (50, 30), 200 pixels wide and 150 pixels high.

 <?php
// Load the picture
$image = imagecreatefromjpeg('http://gitbox.net/example.jpg');
if (!$image) {
    die('Failed to load the image');
}

// Get the original image size
$imgWidth = imagesx($image);
$imgHeight = imagesy($image);

// Set crop parameters
$cropRect = [
    'x' => 50,
    'y' => 30,
    'width' => 200,
    'height' => 150,
];

// Determine whether the cutting area is legal
if ($cropRect['x'] < 0 || $cropRect['y'] < 0 ||
    $cropRect['x'] + $cropRect['width'] > $imgWidth ||
    $cropRect['y'] + $cropRect['height'] > $imgHeight) {
    die('Crop area exceeds the image boundary');
}

// Crop pictures
$cropped = imagecrop($image, $cropRect);
if ($cropped !== false) {
    // Save cropped pictures
    imagejpeg($cropped, 'cropped_example.jpg');
    imagedestroy($cropped);
    echo "Successful cut!";
} else {
    echo "Cropping failed!";
}

// Free up resources
imagedestroy($image);
?>

5. Frequently Asked Questions and Solutions

  • Cropping area exceeds the picture boundary <br> Causes imagecrop to return false . Solution: First detect and limit the coordinates and dimension ranges.

  • Width or height or coordinates are negative or non-integer <br> Use the intval() function to ensure the correct type before passing the parameter.

  • The original image resource is empty <br> Make sure the image path is correct and that PHP has enabled GD extension.


6. Summary

  • Imagecrop requires clear and correct cropping of rectangular areas.

  • The coordinates x and y refer to the upper left corner position, and the width and height must be positive integers and cannot exceed the original image range.

  • Acquire the image size in advance to verify the legitimacy of the cropping parameters.

  • Be sure to check errors in the code to avoid program exceptions.

Correct understanding of these key points can allow you to efficiently and stably complete the image cropping function in PHP.