Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to crop an image automatically using imagecropauto() function in PHP?
The imagecropauto() function is an inbuilt PHP function that automatically crops an image using one of the available modes to remove unwanted borders or backgrounds.
Note: This function requires the GD extension. Install it using: sudo apt-get install php-gd (Linux) or enable it in php.ini (Windows/XAMPP).
Syntax
resource imagecropauto(resource $image, int $mode, float $threshold, int $color)
Parameters
imagecropauto() accepts four parameters − $image, $mode, $threshold, and $color.
$image − The image resource to be cropped.
-
$mode − Optional. Specifies the crop mode:
IMG_CROP_DEFAULT − Works like IMG_CROP_TRANSPARENT mode.
IMG_CROP_TRANSPARENT − Crops out transparent background.
IMG_CROP_BLACK − Crops out black background.
IMG_CROP_WHITE − Crops out white background.
IMG_CROP_SIDES − Uses four corners to detect background.
IMG_CROP_THRESHOLD − Crops using given threshold and color.
$threshold − Optional. Tolerance percentage for color comparison (0-100).
$color − Optional. RGB color value or palette index for threshold mode.
Return Value
Returns a cropped image resource on success or FALSE on failure. Returns FALSE if the entire image would be cropped.
Example
Here's how to crop white background from an image ?
<?php
// Load the image from file
$img = imagecreatefromjpeg('path/to/your/image.jpg');
// Check if image was loaded successfully
if (!$img) {
die('Failed to load image');
}
// Crop the white background automatically
$cropped = imagecropauto($img, IMG_CROP_WHITE);
if ($cropped !== FALSE) {
// Set content type and output the cropped image
header('Content-type: image/jpeg');
imagejpeg($cropped);
// Free up memory
imagedestroy($cropped);
} else {
echo 'Cropping failed';
}
// Clean up original image
imagedestroy($img);
?>
Cropping with Threshold
For more precise control, you can use threshold mode ?
<?php
$img = imagecreatefromjpeg('image.jpg');
// Define a specific color to crop (white: RGB 255,255,255)
$white = imagecolorallocate($img, 255, 255, 255);
// Crop with 5% threshold tolerance
$cropped = imagecropauto($img, IMG_CROP_THRESHOLD, 0.05, $white);
if ($cropped) {
imagejpeg($cropped, 'cropped_output.jpg');
imagedestroy($cropped);
}
imagedestroy($img);
?>
Common Crop Modes
| Mode | Purpose | Use Case |
|---|---|---|
IMG_CROP_WHITE |
Remove white borders | Scanned documents |
IMG_CROP_BLACK |
Remove black borders | Screenshots with black edges |
IMG_CROP_TRANSPARENT |
Remove transparent areas | PNG images with transparency |
IMG_CROP_THRESHOLD |
Custom color removal | Specific background colors |
Conclusion
The imagecropauto() function provides an efficient way to automatically remove unwanted borders from images. Use different crop modes based on your background color, and combine with threshold for precise control over the cropping process.
