<?php
GdImage|false imagecropauto ( GdImage $image,
int $mode = IMG_CROP_DEFAULT,
float $threshold = 0.5,
int $color = -1 )
where,
$image = The image identifier
$mode = The mode to be used
( SEE the below TABLE )
$threshold = The tolerance to be used while comparing
the image color and the color to crop
$color = The RGB color value or a palette index
?>
CONSTANT | VALUE | MEANING |
IMG_CROP_DEFAULT | 0 | Same as IMG_CROP_TRANSPARENT. Before PHP 7.4.0, the bundled libgd fell back to IMG_CROP_SIDES, if the image had no transparent color |
IMG_CROP_TRANSPARENT | 1 | Crops out a transparent background |
IMG_CROP_BLACK | 2 | Crops out a black background |
IMG_CROP_WHITE | 3 | Crops out a white background |
IMG_CROP_SIDES | 4 | Uses the 4 corners of the image to attempt to detect the background to crop |
IMG_CROP_THRESHOLD | 5 | Crops an image using the given $threshold and $color |
ed48 |
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img01a = 'png/PNG 060 01 rectangle-a.png';
$img01b = 'png/PNG 060 01 rectangle-b.png';
$crp01 = 'png/PNG 060 01 imagecropauto.png';
$id_01 = imagecreate(200, 200);
$clr01 = imagecolorallocate($id_01, 255,0,255);
imagepng($id_01, $img01a);
echo $img01a . '<br><br>( ' .
imagesx($id_01) . 'px X ' .
imagesy($id_01) . 'px )';
$clr_f = imagecolorallocate($id_01, 0, 0, 0);
imagefilledrectangle($id_01, 20, 20, 149, 149, $clr_f);
imagepng($id_01, $img01b);
$id_01_crop = imagecropauto($id_01, IMG_CROP_SIDES);
imagepng($id_01_crop, $crp01);
$nwwidth = imagesx($id_01_crop);
$nwheight = imagesy($id_01_crop);
?>
<br><br>
<img src="<?php echo $img01a; ?>"
alt="<?php echo $img01b; ?>"><br><br><br>
<?php echo $img01b . '<br><br>( ' .
imagesx($id_01) . 'px X ' .
imagesy($id_01) . 'px )'; ?><br><br>
<img src="<?php echo $img01b; ?>"
alt="<?php echo $img01b; ?>"><br><br><br>
<?php echo $crp01 . '<br><br>width = ' .
$nwwidth . 'px height= ' .
$nwheight . 'px'; ?><br><br>
<img src="<?php echo $crp01; ?>"
alt="<?php echo $crp01; ?>"><br><br>
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img02a = 'png/PNG 060 02 rectangle-a.png';
$img02b = 'png/PNG 060 02 rectangle-b.png';
$crp02 = 'png/PNG 060 02 imagecropauto.png';
$id_02 = imagecreate(200, 200);
$clr02 = imagecolorallocate($id_02, 0,0,0);
imagepng($id_02, $img02a);
echo $img02a . '<br><br>( ' .
imagesx($id_02) . 'px X ' .
imagesy($id_02) . 'px )';
$clr_f = imagecolorallocate($id_02, 0, 255, 0);
imagefilledrectangle($id_02, 20, 20, 149, 149, $clr_f);
imagepng($id_02, $img02b);
$id_02_crop = imagecropauto($id_02, IMG_CROP_BLACK);
imagepng($id_02_crop, $crp02);
$nwwidth = imagesx($id_02_crop);
$nwheight = imagesy($id_02_crop);
?>
<br><br>
<img src="<?php echo $img02a; ?>"
alt="<?php echo $img02b; ?>"><br><br><br>
<?php echo $img02b . '<br><br>( ' .
imagesx($id_02) . 'px X ' .
imagesy($id_02) . 'px )'; ?><br><br>
<img src="<?php echo $img02b; ?>"
alt="<?php echo $img02b; ?>"><br><br><br>
<?php echo $crp02 . '<br><br>width = ' .
$nwwidth . 'px height= ' .
$nwheight . 'px'; ?><br><br>
<img src="<?php echo $crp02; ?>"
alt="<?php echo $crp02; ?>"><br><br>
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img03a = 'png/PNG 060 03 rectangle-a.png';
$img03b = 'png/PNG 060 03 rectangle-b.png';
$crp03 = 'png/PNG 060 03 imagecropauto.png';
$id_03 = imagecreatetruecolor(200, 200);
$clr03 = imagecolorallocate($id_03, 0, 0, 0);
imagepng($id_03, $img03a);
echo $img03a . '<br><br>( ' .
imagesx($id_03) . 'px X ' .
imagesy($id_03) . 'px )';
$clr_f = imagecolorallocate($id_03, 0, 0, 255);
imagefilledrectangle($id_03, 20, 20, 149, 149, $clr_f);
imagepng($id_03, $img03b);
$trsh03 = 0.9;
$clr03 = 0x00;
$id_03_crop = imagecropauto($id_03,
IMG_CROP_THRESHOLD, $trsh03, $clr03);
imagepng($id_03_crop, $crp03);
$nwwidth = imagesx($id_03_crop);
$nwheight = imagesy($id_03_crop);
?>
<br><br>
<img src="<?php echo $img03a; ?>"
alt="<?php echo $img03a; ?>"><br><br><br>
<?php echo $img03b . '<br><br>( ' . imagesx($id_03) .
'px X ' . imagesy($id_03) . 'px )'; ?><br><br>
<img src="<?php echo $img03b; ?>"
alt="<?php echo $img03b; ?>"><br><br>
<?php echo $crp03 . '<br><br>threshold = ' . $trsh03 .
'<br>color = ' . $clr03 . '<br>width = ' . $nwwidth .
'px height= ' . $nwheight . 'px'; ?><br><br>
<img src="<?php echo $crp03; ?>"
alt="<?php echo $crp03; ?>"><br><br>
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img04a = 'jpeg/JPEG 028 04 pentagram.jpg';
$crp04 = 'png/PNG 060 04 imagecropauto.png';
$id_04 = imagecreatefromjpeg($img04a);
imagejpeg($id_04, $img04a);
echo $img04a . '<br><br>( ' .
imagesx($id_04) . 'px X ' .
imagesy($id_04) . 'px )';
$id_04_crop = imagecropauto($id_04, IMG_CROP_WHITE);
imagepng($id_04_crop, $crp04);
$nwwidth = imagesx($id_04_crop);
$nwheight = imagesy($id_04_crop);
?>
<br><br>
<img src="<?php echo $img04a; ?>"
alt="<?php echo $img04a; ?>"><br><br><br>
<?php echo $crp04 . '<br><br>width = ' . $nwwidth .
'px height= ' . $nwheight . 'px'; ?><br><br>
<img src="<?php echo $crp04; ?>"
alt="<?php echo $crp04; ?>"><br><br>
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
echo "TC IMG_CROP_DEFAULT<br>";
$im = imagecreatetruecolor(99, 99);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_DEFAULT);
$idefa = "png/PNG 060 05 TCdefa.png";
imagepng($im_crop, $idefa);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$idefa\" alt=\"$idefa\">";
echo "<br><br>Palette IMG_CROP_DEFAULT<br>";
$im = imagecreate(99, 99);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_DEFAULT);
$ipdefa = "png/PNG 060 05 Pdefa.png";
imagepng($im_crop, $ipdefa);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$ipdefa\" alt=\"$ipdefa\">";
echo "<br><br>TC IMG_CROP_SIDES<br>";
$im = imagecreatetruecolor(99, 99);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_SIDES);
$isides = "png/PNG 060 05 Tsides.png";
imagepng($im_crop, $isides);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$isides\" alt=\"$isides\">";
echo "<br><br>Palette IMG_CROP_SIDES<br>";
$im = imagecreate(99, 99);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_SIDES);
$ipsides = "png/PNG 060 05 Psides.png";
imagepng($im_crop, $ipsides);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$ipsides\" alt=\"$ipsides\">";
echo "<br><br>TC IMG_CROP_BLACK<br>";
$im = imagecreatetruecolor(50, 50);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_BLACK);
$iblack = "png/PNG 060 05 Tblack.png";
imagepng($im_crop, $iblack);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$iblack\" alt=\"$iblack\">";
echo "<br><br>Palette IMG_CROP_BLACK<br>";
$im = imagecreate(50, 50);
$bgd = imagecolorallocate($im, 0, 0, 0);
$b = imagecolorallocate($im, 0, 0, 255);
imagefilledrectangle($im, 20, 20, 30, 30, 0xff);
$im_crop = imagecropauto($im, IMG_CROP_BLACK);
$ipblack = "png/PNG 060 05 Pblack.png";
imagepng($im_crop, $ipblack);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
echo "<img src=\"$ipblack\" alt=\"$ipblack\">";
echo "<br><br>IMG_CROP_THRESHOLD<br>";
$idctr = "png/PNG 060 05 logo_noise.png";
$im = imagecreatefrompng($idctr);
$im_crop =
imagecropauto($im, IMG_CROP_THRESHOLD, 0.1, 0x0);
imagepng($im_crop, $idctr);
echo imagesx($im_crop) . 'px X ' .
imagesy($im_crop) . 'px<br><br>';
?>
<img src="<?php echo $idctr; ?>"
alt="<?php echo $idctr; ?>">