- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to crop an image to the given rectangle using imagecrop() function using PHP?
imagecrop() is an inbuilt function in PHP that is used to crop an image to the given rectangle. It crops the image from the given rectangle area and returns the output image.The given image is not modified.
Syntax
resource imagecrop ($image, $rect)
Parameters
imagecrop() takes two parameters, $image and $rect.
$image − It is the parameter returned by the image creation functions, such as imagecreatetruecolor(). It is used to create the size of an image.
$rect − The cropping rectangle is an array with keys X, Y, width, and height.
Return Values
imagecrop() returns the cropped image resource on success or it returns false on failure.
Example
<?php // It will create an image from the given image $img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png'); // This will find the size of the image $size = min(imagesx($img), imagesy($img)); //This will set the size of the cropped image. $img2 = imagecrop($img, ['x' => 0, 'y' => 0, 'width' => 500, 'height' => 320]); if($img2 !== FALSE) { imagepng($img2, 'C:\xampp\htdocs\pic_cropped.png'); imagedestroy($img2); } imagedestroy($img); ?>
Output
Input image before using imagecrop() function
Output image after using imagecrop() function
Example 2
<?php //load an image from the local drive folder. $filename = 'C:\xampp\htdocs\Images\img34.png'; $img = imagecreatefrompng($filename ); $ini_x_size = getimagesize($filename)[0]; $ini_y_size = getimagesize($filename )[1]; //the minimum of xlength and ylength to crop. $crop_measure = min($ini_x_size, $ini_y_size); // Set the content-type header //header('Content-Type: image/png'); $crop_array = array('x' =>0 , 'y' => 0, 'width' => $crop_measure, 'height'=> $crop_measure); $thumb_img = imagecrop($img, $crop_array); imagejpeg($thumb_img, 'thumb.png', 100); ?>
Output
- Related Articles
- How to crop an image automatically using imagecropauto() function in PHP?
- How to crop an image using crop() function in Node Jimp?
- How to rotate an image with a given angle using imagerotate() function in PHP?
- How to destroy an image in PHP using imagedestroy() function?
- How to crop an image along the X-axis using FabricJS?
- How to crop an image along the Y-axis using FabricJS?
- How to get the clipping rectangle using imagegetclip() function in PHP?
- How to ensure an image is a truecolor image using the imageistruecolor() function in PHP?
- How to check if an Image has crop applied using FabricJS?
- How to apply a filter to an image using imagefilter() function in PHP?
- How to get or set the resolution of an image using imageresolution() function in PHP?
- How to crop the height in a cloned image using FabricJS?
- How to crop the width in a cloned image using FabricJS?
- How to crop the left offset in a cloned image using FabricJS?
- How to crop the top offset in a cloned image using FabricJS?

Advertisements