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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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 = imagecreatefrompng("girl.png");

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

// (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 imagecreatefrompng() to read a PNG image. If you are working with other formats, use the respective imagecreatefromjpeg() imagecreatefromgif() imagecreatefromwebp(). 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 200 X 200 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 = imagecreatefrompng("girl.png");
 
// (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
$img = imagecreatefrompng("girl.png");
 
// (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.5; // SHRINK CROPPED IMAGE TO 50%
$rwidth = ceil($cwidth * $tosize);
$rheight = ceil($cheight * $tosize);
$resized = imagecreatetruecolor($rwidth, $rheight);
imagecopyresampled($resized, $crop, 0, 0, 0, 0, $rwidth, $rheight, $cwidth, $cheight);
imagewebp($resized, "demo-C.webp", 90);

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.

 

 

EXTRAS

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

 

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 *