Skip to content
Related Articles

Related Articles

PHP | imagecropauto() Function
  • Last Updated : 29 Jan, 2020

The imagecropauto() function is an inbuilt function in PHP which is used to crop an image automatically using one of the available modes.

Syntax:

resource imagecropauto( resource $image, int $mode,
                      float $threshold, int $color )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $image: It specifies the image resource to be cropped.
  • $mode (Optional): It specifies an integer corresponding to a crop mode.

    List of CROP modes are given below:

    • IMG_CROP_DEFAULT (0)
    • IMG_CROP_TRANSPARENT (1)
    • IMG_CROP_BLACK (2)
    • IMG_CROP_WHITE (3)
    • IMG_CROP_SIDES (4)
    • IMG_CROP_THRESHOLD (5)
  • $threshold (Optional): It specifies the tolerance in percent to be used while comparing the image color and the color to crop.
  • $color (Optional): It specifies either an RGB color value or a palette index.

Return Value: This function returns TRUE on success.



Exceptions: This function throws Exception on error.

Below given programs illustrate the imagecropauto() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load the png image
$im = imagecreatefrompng(
   
// Crop the extra white area
$cropped = imagecropauto($im, IMG_CROP_WHITE);
  
// Convert it to a png file
header('Content-type: image/png');  
imagepng($cropped);
?>

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load the png image
$im = imagecreatefrompng(
   
// Crop the image
$cropped = imagecropauto($im, IMG_CROP_BLACK);
  
// Convert it to a png file
header('Content-type: image/png');  
imagepng($cropped);
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.imagecropauto.php

My Personal Notes arrow_drop_up
Recommended Articles
Page :