Cropping An Image with PHP and the GD Library

Cropping an image with PHP shouldn’t terribly difficult to do and yet when I attempted to do so several months back I was amazed at how little useful documentation PHP offered. Upon Googling the topic I was further amazed but the lack of useful tutorials on the subject. The few I found were overly complicated and didn’t cover simple image cropping. After reading up as much as I could on the subject, and some trial and error, I was able to accomplish my goal of cropping an image with PHP using the GD library.

So I decided I would post the code I sent them here so anyone else who is looking to crop images with PHP and the GD library can do so without having to endure limited documentation and inventing swear words.

  1. // Original image
  2. $filename = 'someimage.jpg';
  3.  
  4. // Get dimensions of the original image
  5. list($current_width, $current_height) = getimagesize($filename);
  6.  
  7. // The x and y coordinates on the original image where we
  8. // will begin cropping the image
  9. $left = 50;
  10. $top = 50;
  11.  
  12. // This will be the final size of the image (e.g. how many pixels
  13. // left and down we will be going)
  14. $crop_width = 200;
  15. $crop_height = 200;
  16.  
  17. // Resample the image
  18. $canvas = imagecreatetruecolor($crop_width, $crop_height);
  19. $current_image = imagecreatefromjpeg($filename);
  20. imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
  21. imagejpeg($canvas, $filename, 100);

Hopefully that helps demonstrate how to simply crop an image with PHP and the GD library.

I have submitted this to the PHP online documentation for imagecopy().

23 thoughts on “Cropping An Image with PHP and the GD Library

  1. Nice one! I plan to cover creating custom bitmap fonts for GD, after doing some more tests about them.

  2. I want to cropping image of dynamically means i want to put crop window on my image and crop part where i wish. have u that type of code.

  3. it works..!!!
    but not create new image, so original image replased with cropped img…
    how to alternate..??

  4. Dude it is absolutely right…..
    but it isnt working when i try to crop a PNG or a BMP image
    we may use the function imagecreatefrompng() for PNG image
    but what about BMP and GIF images ???
    Please Look into matter and help me !!

  5. Thanks for the info. It got me on the right track; however, you actually do not need to get or use the original image’s width and height. The imagecopy line should be:

    imagecopy($canvas, $current_image, 0, 0, $left, $top, $crop_width, $crop_height);

    This way, you actually get a 200×200 area out of the source image instead of a 640×480 area if the original image was that big.

  6. Man, i’ve been searching a long time for this, and truly, this is: THE perfect cropping code with PHP for the app i’m going to make (i’ll make sure to post the link when it’s ready, and surely i’ll give credits) Great Stuff!!

  7. Thanks for script, works perfectly. Just a quick note for those who use the 3rd param in the imagejpeg / imagepng function that sets the quality. For imagejpeg 100% is the best quality, but for imagepng, quality is compression level, so here 0 is the best!

  8. Hi, I have just been looking at your image cropping tool. I need a cropping tool like yours for my website. But your image cropping tool crops images at 200 x 200 (thumbnail size). Is their a way of cropping images to any size??

  9. Excellent guide to cropping images with PHP – much simpler than trying to figure it from the PHP docs which still aren’t brilliant even after all this time.

    Implemented (with MarkRH’s tweak) and working nicely!

  10. For newbies like me:
    Do not forget to place this line before output via HTML:

    Header(“Content-type: image/jpeg”);

  11. you forgot one important thing, php5-gd need to be installed first. if you’re linux ubuntu user, install php5-gd by apt-get install php5-gd

Leave a Reply

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