HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

imagecrop

Description

The imagecrop of Image for PHP crop an image to the given rectangle.

Syntax

imagecrop(
    GdImage $image,
    array $rectangle
): GdImage|false

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

rectangle

The cropping rectangle as array with keys x, y, width and height.

Return

Return cropped image object on success or false on failure.

Examples

1 · return

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$rectangle = array(
    'x' => 0,
    'y' => 0,
    'width' => 50,
    'height' => 50
);

$return = imagecrop($image, $rectangle);

var_dump($return);
object(GdImage)#2 (0) {
}

2 · base64

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/png/Happy.png';
$image = imagecreatefrompng($filename);

$rectangle = array(
    'x' => 0,
    'y' => 0,
    'width' => 50,
    'height' => 50
);
$image = imagecrop($image, $rectangle);

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,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABBElEQVRYhe3Y2wrDIBAE0Enp//+yfVgQafCyOnspzTw2IofRpIlXKQUxuQbX3m4KAGNKGx/WqqbGlKXW1Bix9kESOusUJCGyOCDJizQP0wRGW2SQ5LAtExPOWFYm7C6iIUiy0Za5CXqWhwlKlpMJGpafCcssVxPWWN4m8P58yJmyAqrCjBVjwpAVZsLP7a3IqtBhBZuQdhHvLzb0qtqv9hSfr/djBPlljvtaRGJVg6ONMrwKmO2t0+OWlsWqatE0Gpb0TqSzVMvXHVxZ8Y/QNv+yiJwIi7iCqqm6g1O3xc1iYaNhSdu6LI/le1PP63Q4aU73YiPZuc2T7q2HpcnD0uRhafIBK4YSj+JL/n4AAAAASUVORK5CYII=">
</body>
</html>