imagecropauto
Description
The imagecropauto of Image for PHP crop an image automatically using one of the available modes.
Syntax
imagecropauto( GdImage $image, int $mode = IMG_CROP_DEFAULT, float $threshold = 0.5, int $color = -1 ): GdImage|false
Parameters
image
A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().
mode
One of the following constants:
Constant | Description |
---|---|
IMG_CROP_DEFAULT | Same as IMG_CROP_TRANSPARENT. |
IMG_CROP_TRANSPARENT | Crops out a transparent background. |
IMG_CROP_BLACK | Crops out a black background. |
IMG_CROP_WHITE | Crops out a white background. |
IMG_CROP_SIDES | Uses the 4 corners of the image to attempt to detect the background to crop. |
IMG_CROP_THRESHOLD | Crops an image using the given threshold and color. |
threshold
Specifies the tolerance in percent to be used while comparing the image color and the color to crop. The method used to calculate the color difference is based on the color distance in the RGB(a) cube.
Used only in IMG_CROP_THRESHOLD mode.
color
Either an RGB color value or a palette index.
Used only in IMG_CROP_THRESHOLD mode.
Return
Returns a cropped image object on success or false on failure. If the complete image was cropped, imagecrop() returns false.
Examples
1 · return
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $return = imagecropauto($image); var_dump($return);
object(GdImage)#2 (0) { }
2 · base64 · image
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 0; $green = 0; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); imagecolortransparent($image, $color); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $image = imagecropauto($image); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABQklEQVRoge2b0Y7CMAwEW748n85DEEhAG0+ctptq5xHJ9miTO4k7Z13SlFKCHyLWTHFzfMav0wyN7PPrMeuY1FGCzboPiBYys+S9RuXALP/jhppEzYZooVYhs4Fa8YZts+FawbYNs4O0Is0fxw1OspfZoYE1R2yanaC1P2i20zwtsJ1xU2V2cmBbQ3Uz0zX7Ps1LjvLv6EkyuzCwXwHdzHTNPqd5+VFW3hq6mdmM87pnIpesUmV0M7MZR9dsXcSuf6WUopuZzTg24+iarYK/Miq6mdmMYzOOzTg249iMYzOOzTg24+ia+Ts6x2Ycm3F0zfy3bY7NOLpm/i8ix2Yc7x5wdM28fcPx/hlH18wbmBxv+nIm3NtuVua54xZ+RfRNRbALZcw7lHivsa3mf+9E++bL7/Kurm9GX8m93m/GR2bu5ROMDHch+IoYzgAAAABJRU5ErkJggg=="> </body> </html>
3 · base64 · mode · IMG_CROP_DEFAULT
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 0; $green = 0; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); imagecolortransparent($image, $color); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $mode = IMG_CROP_DEFAULT; $image = imagecropauto($image, $mode); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABQklEQVRoge2b0Y7CMAwEW748n85DEEhAG0+ctptq5xHJ9miTO4k7Z13SlFKCHyLWTHFzfMav0wyN7PPrMeuY1FGCzboPiBYys+S9RuXALP/jhppEzYZooVYhs4Fa8YZts+FawbYNs4O0Is0fxw1OspfZoYE1R2yanaC1P2i20zwtsJ1xU2V2cmBbQ3Uz0zX7Ps1LjvLv6EkyuzCwXwHdzHTNPqd5+VFW3hq6mdmM87pnIpesUmV0M7MZR9dsXcSuf6WUopuZzTg24+iarYK/Miq6mdmMYzOOzTg249iMYzOOzTg24+ia+Ts6x2Ycm3F0zfy3bY7NOLpm/i8ix2Yc7x5wdM28fcPx/hlH18wbmBxv+nIm3NtuVua54xZ+RfRNRbALZcw7lHivsa3mf+9E++bL7/Kurm9GX8m93m/GR2bu5ROMDHch+IoYzgAAAABJRU5ErkJggg=="> </body> </html>
4 · base64 · mode · IMG_CROP_TRANSPARENT
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 0; $green = 0; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); imagecolortransparent($image, $color); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $mode = IMG_CROP_TRANSPARENT; $image = imagecropauto($image, $mode); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABQklEQVRoge2b0Y7CMAwEW748n85DEEhAG0+ctptq5xHJ9miTO4k7Z13SlFKCHyLWTHFzfMav0wyN7PPrMeuY1FGCzboPiBYys+S9RuXALP/jhppEzYZooVYhs4Fa8YZts+FawbYNs4O0Is0fxw1OspfZoYE1R2yanaC1P2i20zwtsJ1xU2V2cmBbQ3Uz0zX7Ps1LjvLv6EkyuzCwXwHdzHTNPqd5+VFW3hq6mdmM87pnIpesUmV0M7MZR9dsXcSuf6WUopuZzTg24+iarYK/Miq6mdmMYzOOzTg249iMYzOOzTg24+ia+Ts6x2Ycm3F0zfy3bY7NOLpm/i8ix2Yc7x5wdM28fcPx/hlH18wbmBxv+nIm3NtuVua54xZ+RfRNRbALZcw7lHivsa3mf+9E++bL7/Kurm9GX8m93m/GR2bu5ROMDHch+IoYzgAAAABJRU5ErkJggg=="> </body> </html>
5 · base64 · mode · IMG_CROP_BLACK
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $mode = IMG_CROP_BLACK; $image = imagecropauto($image, $mode); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABQklEQVRoge2b0Y7CMAwEW748n85DEEhAG0+ctptq5xHJ9miTO4k7Z13SlFKCHyLWTHFzfMav0wyN7PPrMeuY1FGCzboPiBYys+S9RuXALP/jhppEzYZooVYhs4Fa8YZts+FawbYNs4O0Is0fxw1OspfZoYE1R2yanaC1P2i20zwtsJ1xU2V2cmBbQ3Uz0zX7Ps1LjvLv6EkyuzCwXwHdzHTNPqd5+VFW3hq6mdmM87pnIpesUmV0M7MZR9dsXcSuf6WUopuZzTg24+iarYK/Miq6mdmMYzOOzTg249iMYzOOzTg24+ia+Ts6x2Ycm3F0zfy3bY7NOLpm/i8ix2Yc7x5wdM28fcPx/hlH18wbmBxv+nIm3NtuVua54xZ+RfRNRbALZcw7lHivsa3mf+9E++bL7/Kurm9GX8m93m/GR2bu5ROMDHch+IoYzgAAAABJRU5ErkJggg=="> </body> </html>
6 · base64 · mode · IMG_CROP_WHITE
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 255; $green = 255; $blue = 255; $color = imagecolorallocate($image, $red, $green, $blue); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $mode = IMG_CROP_WHITE; $image = imagecropauto($image, $mode); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABiElEQVRoge2bS47DIBBEwXNw5KP5ZM6CUeLEGKjmVy31W0ah+qUArxx/nqdrY9/3+4chhMZY32KWdLrS4ic0KzpdkfltgjWQluD7EdhMNkawCjOTacnWAmYtWoKEWrN2LTSnyqyXFpRWNuurVZ9ZMBuhVZkseZ7NIWc2rrCa/Eez0VrFKdp2c05h+VmqOptZWGaiqs5I+DWbv5VPc5V0tqqw5HQlnVHxMVu7lXcHDZ2xQW/GcMgibxP6zggxM5zNMR3/SPTh7owTM8MxM5w/7/1qhwTHcfB2ZmY4ZoZjZjhmhmNmOGaGY2Y4ZoZDbNb+As8IQgjEna0WeMTMcMwMZ3M93vzrS/Th7owTM8P5N+O5BG8T+s4I0WDGcNSuDho6Y+PLbO2G/kxX0plbV9t9rp7OeEiYzd/Q5ERVnbm5tT3N0taZm1VbZkqus9Fy+XyFuxkZV1sxudzZCLmazKrd7CtXmVZ7znrJ1ecAN6BdDkrA7maLHLoWfmrI5ASrJM8zdIzsx/D+5/UFQy6D+pEYC7cAAAAASUVORK5CYII="> </body> </html>
7 · base64 · mode · IMG_CROP_SIDES
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 255; $green = 0; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $mode = IMG_CROP_SIDES; $image = imagecropauto($image, $mode); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABfklEQVRoge2by5LDIAwEsX8c8uWbA1veZI1BI16jKvXRFaTOCHOIneMn9PJK6X4xli5CHD1mRadPevyUZk2nT3R+p2INpKX4fAY207VRrMLMdFq6tYBZj5aigtSsXwutIzIbpQVVa5uN1ZLXbJjN0BJW1pxna6iZzQtMUv/RbLZWs4u1aa4JrN7LVGYrA6t0NJUZCf/N1o/yqa+RzHYFVuxuJDMq/sz2jvLuYCEzNujNGDZZ5jKhz4wQN8M5A9P2z2Qf7sw4cTMcN8M5EtmRccGbmZvhuBmOm+G4GY6b4bgZjpvhuBkOsVn/CzwziCkRZ7Zb4BE3w3EznDOMePNvLNF/2VPiZji/Zjw3QfSnO3osmDFstehPq7v4Mts70Gj1TZJdsd372smMh4LZ+oEWO5rKLKyN7amXtczCqtgqXWqZzZar1zc4zcy82JqV25nNkJPUFE1zrJywmnSfjZKT1wHugH45qAJ2b/bIoWvhU0Mnp1ilOc/QNrovw/uf1zdxhXs+gnpmmQAAAABJRU5ErkJggg=="> </body> </html>
8 · base64 · mode · IMG_CROP_THRESHOLD · threshold color
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $red = 255; $green = 0; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); $x = 0; $y = 0; imagefill($image, $x, $y, $color); $red = 128; $green = 128; $blue = 128; $color = imagecolorallocate($image, $red, $green, $blue); $center_x = 50; $center_y = 50; $width = 50; $height = 100; imagefilledellipse($image, $center_x, $center_y, $width, $height, $color); $red = 255; $green = 32; $blue = 0; $color = imagecolorallocate($image, $red, $green, $blue); $mode = IMG_CROP_THRESHOLD; $threshold = 1.0; $image = imagecropauto($image, $mode, $threshold, $color); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>';
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAABkCAIAAACkSJ8yAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABfklEQVRoge2by5LDIAwEsX8c8uWbA1veZI1BI16jKvXRFaTOCHOIneMn9PJK6X4xli5CHD1mRadPevyUZk2nT3R+p2INpKX4fAY207VRrMLMdFq6tYBZj5aigtSsXwutIzIbpQVVa5uN1ZLXbJjN0BJW1pxna6iZzQtMUv/RbLZWs4u1aa4JrN7LVGYrA6t0NJUZCf/N1o/yqa+RzHYFVuxuJDMq/sz2jvLuYCEzNujNGDZZ5jKhz4wQN8M5A9P2z2Qf7sw4cTMcN8M5EtmRccGbmZvhuBmOm+G4GY6b4bgZjpvhuBkOsVn/CzwziCkRZ7Zb4BE3w3EznDOMePNvLNF/2VPiZji/Zjw3QfSnO3osmDFstehPq7v4Mts70Gj1TZJdsd372smMh4LZ+oEWO5rKLKyN7amXtczCqtgqXWqZzZar1zc4zcy82JqV25nNkJPUFE1zrJywmnSfjZKT1wHugH45qAJ2b/bIoWvhU0Mnp1ilOc/QNrovw/uf1zdxhXs+gnpmmQAAAABJRU5ErkJggg=="> </body> </html>
Links
Related
Image
- gd_info
- getimagesize
- getimagesizefromstring
- image_type_to_extension
- image_type_to_mime_type
- imageaffine
- imageaffinematrixconcat
- imageaffinematrixget
- imagealphablending
- imageantialias
- imagearc
- imageavif
- imagebmp
- imagechar
- imagecharup
- imagecolorallocate
- imagecolorallocatealpha
- imagecolorat
- imagecolorclosest
- imagecolorclosestalpha
- imagecolorclosesthwb
- imagecolordeallocate
- imagecolorexact
- imagecolorexactalpha
- imagecolormatch
- imagecolorresolve
- imagecolorresolvealpha
- imagecolorset
- imagecolorsforindex
- imagecolorstotal
- imagecolortransparent
- imageconvolution
- imagecopy
- imagecopymerge
- imagecopymergegray
- imagecopyresampled
- imagecopyresized
- imagecreate
- imagecreatefromavif
- imagecreatefrombmp
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromtga
- imagecreatefromwbmp
- imagecreatefromwebp
- imagecreatefromxbm
- imagecreatefromxpm
- imagecreatetruecolor
- imagecrop
- imagedashedline
- imagedestroy
- imageellipse
- imagefill
- imagefilledarc
- imagefilledellipse
- imagefilledpolygon
- imagefilledrectangle
- imagefilltoborder
- imagefilter
- imageflip
- imagefontheight
- imagefontwidth
- imageftbbox
- imagefttext
- imagegammacorrect
- imagegetclip
- imagegetinterpolation
- imagegif
- imageinterlace
- imageistruecolor
- imagejpeg
- imagelayereffect
- imageline
- imageloadfont
- imageopenpolygon
- imagepalettecopy
- imagepalettetotruecolor
- imagepng
- imagepolygon
- imagerectangle
- imageresolution
- imagerotate
- imagesavealpha
- imagescale
- imagesetbrush
- imagesetclip
- imagesetinterpolation
- imagesetpixel
- imagesetstyle
- imagesetthickness
- imagesettile
- imagestring
- imagestringup
- imagesx
- imagesy
- imagetruecolortopalette
- imagettfbbox
- imagettftext
- imagetypes
- imagewbmp
- imagewebp
- imagexbm
- iptcembed
- iptcparse