Friday, October 24, 2025
HomeLanguagesPHP | Imagick cropImage() Function

PHP | Imagick cropImage() Function

The Imagick::cropImage() function is an inbuilt function in PHP which is used to extracts the region of the image.
Syntax: 
 

int Imagick::cropImage( $width, $height, $x, $y )

Parameters: This function accept four parameters as mention above and describe below. 
 

  • $width: This parameter is used to specify the width of the cropped image.
  • $height: This parameter is used to specify the height of the cropped image.
  • $x: This parameter is used to specify the X coordinate of the cropped image of top left corner.
  • $y: This parameter is used to specify the Y coordinate of the cropped image of top left corner.

Return Value: This function returns True on success.
Below programs illustrate the Imagick::cropImage() function in PHP:
Original Image: 
 

Program 1: 
 

php




<?php
   
// require_once('path/vendor/autoload.php');
    
// Create an imagick object
 $image = new Imagick(
    
// Imagick function to crop Image 
$image->cropImage(390, 100, 0, 0);
       
header("Content-Type: image/jpg");
   
// Display the image
echo $image->getImageBlob();
?> 


Output: 
 

 

Original Image: 
 

Program 2: 
 

php




<?php
$string = "Computer Science portal for Geeks!";
  
// creating new image of above String
// and add color and background
$im = new Imagick();
$draw = new ImagickDraw();
 
// Fill the color in image
$draw->setFillColor(new ImagickPixel('green'));
 
// Set the text font size
$draw->setFontSize(50);
 
$matrix = $im->queryFontMetrics($draw, $string);
$draw->annotation(0, 40, $string);
$im->newImage($matrix['textWidth'], $matrix['textHeight'],
         new ImagickPixel('white'));
          
// Draw the image        
$im->drawImage($draw);
 
// Set the image format
$im->setImageFormat('png');
  
// Imagick function to crop Image
$im->cropImage(420, 120, 0, 0);
         
header("Content-Type: image/jpg");
     
// Display the image
echo $im->getImageBlob();
?>


Output: 
 

Reference: http://php.net/manual/en/imagick.cropimage.php
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS