PHP | Gmagick cropimage() Function

The Gmagick::cropimage() function is an inbuilt function in PHP which is used to extracts the region of the image. This function crop the part of image.

Syntax:

public Gmagick::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 the cropped Gmagick object.

Below programs illustrate the Gmagick::cropimage() function in PHP:

Original Image 1:

Program 1:




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


Output:

Original Image 2:

Program 2:




<?php
$string = "Computer Science portal for Geeks!";
Ā Ā Ā 
// Creating new image of above String
// and add color and background color
$im = new Gmagick();
$draw = new GmagickDraw();
Ā Ā 
// Fill the color in image
$draw->setFillColor(new GmagickPixel('green'));
Ā Ā 
// Set the text font size
$draw->setFontSize(50);
Ā Ā 
$metrix = $im->queryFontMetrics($draw, $string);
$draw->annotation(0, 40, $string);
$im->newImage($metrix['textWidth'], $metrix['textHeight'],
Ā Ā Ā Ā Ā Ā Ā Ā Ā new GmagickPixel('white'));
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
// Draw the imageĀ Ā Ā Ā Ā Ā Ā Ā Ā 
$im->drawImage($draw);
Ā Ā 
// Set the image format
$im->setImageFormat('png');
Ā Ā Ā 
// Gmagick 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/gmagick.cropimage.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button