The 4th Annual China PHP Conference


(PHP 5 >= 5.5.0, PHP 7)

imagecropCrop an image using the given coordinates and size, x, y, width and height


resource imagecrop ( resource $image , array $rect )


This function is currently not documented; only its argument list is available.



An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().


Array with keys "x", "y", "width" and "height".

Return Values

Return cropped image resource on success or FALSE on failure.

add a note add a note

User Contributed Notes 1 note

nangomusic at gmail dot com
1 year ago
An example to crop an image stored in $filename:

// Create a blank image and add some text
$ini_filename = 'test.JPG';
$im = imagecreatefromjpeg($ini_filename );

$ini_x_size = getimagesize($ini_filename )[0];
$ini_y_size = getimagesize($ini_filename )[1];

//the minimum of xlength and ylength to crop.
$crop_measure = min($ini_x_size, $ini_y_size);

// Set the content type header - in this case image/jpeg
//header('Content-Type: image/jpeg');

$to_crop_array = array('x' =>0 , 'y' => 0, 'width' => $crop_measure, 'height'=> $crop_measure);
$thumb_im = imagecrop($im, $to_crop_array);

imagejpeg($thumb_im, 'thumb.jpeg', 100);
To Top