Sunday, November 23, 2025
HomeLanguagesPHP | imagecrop() Function

PHP | imagecrop() Function

The imagecrop() function is an inbuilt function in PHP which is used to crop an image to the given rectangle. This function crops an image to the given rectangular area and returns the resulting image. The given image is not modified.

Syntax:

resource imagecrop ( $image, $rect )

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

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $rect: The cropping rectangle as array with keys x, y, width and height.

Return Value: This function return cropped image resource on success or False on failure.

Below programs illustrate the imagecrop() function in PHP:

Program:




<?php
  
// Create an image from given image
$im = imagecreatefrompng(
  
// find the size of image
$size = min(imagesx($im), imagesy($im));
  
// Set the crop image size 
$im2 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => 250, 'height' => 150]);
if ($im2 !== FALSE) {
    header("Content-type: image/png");
       imagepng($im2);
    imagedestroy($im2);
}
imagedestroy($im);
?>


output:
image crop

Related Articles:

Reference: http://php.net/manual/en/function.imagecrop.php

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS