How To Crop Images In PHP (Simple Examples)

Welcome to a quick tutorial on how to crop images in PHP. Need to “clip” or “cut” a portion of an image on the server side? Yes, we can.

We can easily crop images in PHP using the GD extension:

  1. Create an image object $img = imagecreatefromjpeg("SOURCE.JPG");
  2. Define the crop area $area = ["x"=>0, "y"=>0, "width"=>500, "height"=>300];
  3. Crop the image $crop = imagecrop($img, $area);
  4. Lastly, save the cropped image imagejpeg($crop, "CROP.JPG", 50);

That covers the quick basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • Please make sure that the GD extension extension=gd (or gd2 prior to PHP8) is enabled in php.ini.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP CROP IMAGES

All right, let us now get into the examples of cropping images in PHP.

 

EXAMPLE 1) SIMPLE IMAGE CROP

1-simple-crop.php
<?php
// (A) CREATE IMAGE OBJECT
$img = imagecreatefromwebp("foxe.webp");

// (B) CROP AREA
// CROP 500 X 300 FROM TOP LEFT CORNER (0,0)
$area = [
  "x" => 0, "y" => 0,
  "width" => 500, "height" => 300
];

// (C) CROP IMAGE
$crop = imagecrop($img, $area);
 
// (D) SAVE CROPPED IMAGE
imagewebp($crop, "demo-A.webp");

// (E) RECOMMENDED CLEANUP - DESTROY IMAGE OBJECTS
imagedestroy($img);
imagedestroy($crop);

Yep, this is sort of the “full version” of the snippet in the above introduction. Should be very straightforward, but let us expand a little bit more:

  1. Here, we are using imagecreatefromwebp() to read a WEBP image. If you are working with other formats, use the respective imagecreatefromjpeg() imagecreatefromgif() imagecreatefrompng(). Yep, this is something I don’t like too, but that is just how GD works.
  2. Specify the crop area. In this example, we will cut out 500 X 300 from the top-left corner (0, 0) of the source image.
  3. Crop the image.
  4. Save the cropped image to a WEBP file using imagewebp(). Same old story, use imagejpeg() imagepng() imagegif() if you want other formats instead.
  5. PHP will automatically clean up the image objects at the end of the script. So this is optional, but it is good to manually free up some memory if you have more processes to do after cropping the image.

 

 

EXAMPLE 2) CROP FROM CENTER

2-center-crop.php
<?php
// (A) CREATE IMAGE OBJECT
$img = imagecreatefromwebp("foxe.webp");
 
// (B) CROP AREA
// (B1) SOURCE IMAGE DIMENSIONS
$swidth = imagesx($img);
$sheight = imagesy($img);
 
// (B2) CROP AREA DIMENSIONS (50% OF SOURCE)
$tocrop = 0.5;
$cwidth = ceil($swidth * $tocrop);
$cheight = ceil($sheight * $tocrop);
 
// (B3) CROP COORDINATES (FROM CENTER OF IMAGE)
$sx = ceil(($swidth / 2) - ($cwidth / 2));
$sy = ceil(($sheight / 2) - ($cheight / 2));
$area = ["x" => $sx, "y" => $sy, "width" => $cwidth, "height" => $cheight];
 
// (C) CROP IMAGE
$crop = imagecrop($img, $area);
 
// (D) SAVE CROPPED IMAGE
imagewebp($crop, "demo-B.webp");

// (E) RECOMMENDED CLEANUP - DESTROY IMAGE OBJECTS
imagedestroy($img);
imagedestroy($crop);

Did I mention that calculating the crop area can get pretty crazy? Let’s walk through it step-by-step.

  • (B1) Get the width and height of the original image.
    • $swidth = imagesx($img);
    • $height = imagesx($img);
  • (B2) Calculate or specify the area you want to crop out. In this example, it is 50% of the original image.
    • $cwidth = ceil($swidth * 0.5);
    • $cheight = ceil($sheight * 0.5);
  • (B3) Getting the center X and Y coordinates are kind of tricky.
    • $sx = ceil($swidth / 2) and $sy = ceil($sheight / 2) will give us the exact center of the image, but that is not where we want to crop from.
    • We have to offset the X coordinate by subtracting half of the crop width – $sx = ceil(CENTER X - (CROP WIDTH / 2));
    • Offset the Y coordinate by subtracting half of the crop height – $sy = ceil(CENTER Y - (CROP HEIGHT / 2));

 

 

EXAMPLE 3) CROP & RESIZE

3-resize-crop.php
<?php
// (A) CREATE IMAGE OBJECT
imagewebp($resized, "demo-C.webp", 90);$img = imagecreatefromwebp("foxe.webp");
 
// (B) CROP AREA
// (B1) SOURCE IMAGE DIMENSIONS
$swidth = imagesx($img);
$sheight = imagesy($img);
 
// (B2) CROP AREA DIMENSIONS (50% OF SOURCE)
$tocrop = 0.5;
$cwidth = ceil($swidth * $tocrop);
$cheight = ceil($sheight * $tocrop);
 
// (B3) CROP COORDINATES (FROM CENTER OF IMAGE)
$sx = ceil(($swidth / 2) - ($cwidth / 2));
$sy = ceil(($sheight / 2) - ($cheight / 2));
$area = ["x" => $sx, "y" => $sy, "width" => $cwidth, "height" => $cheight];
 
// (C) CROP IMAGE
$crop = imagecrop($img, $area);
 
// (D) RESIZE & SAVE CROPPED IMAGE
$tosize = 0.8; // SHRINK CROPPED IMAGE TO 80%
$rwidth = ceil($cwidth * $tosize);
$rheight = ceil($cheight * $tosize);
$resized = imagecreatetruecolor($rwidth, $rheight);
imagecopyresampled($resized, $crop, 0, 0, 0, 0, $rwidth, $rheight, $cwidth, $cheight);
imagepng($resized, "demo-C.png", 9);

Need to further resize the image after cropping? No sweat, just add a few more lines of code. If you want to resize transparent images (PNG GIF WEBP), follow up with my other tutorial on resizing images – Link below.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Crop Image In PHP (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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