Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

15
  • 1
    Are you storing images as BLOBs? I would recommend storing images in the filesystem and insert references in your database. I also recommend reading the full GD (or ImageMagick) documentation to see what other options you have available. Commented Feb 1, 2013 at 15:47
  • 29
    Note, this solution only works for JPEGs. You can replace imagecreatefromjpeg with any of the following: imagecreatefromgd, imagecreatefromgif, imagecreatefrompng, imagecreatefromstring, imagecreatefromwbmp, imagecreatefromxbm, imagecreatefromxpm to deal with different image types. Commented Feb 1, 2013 at 15:50
  • 3
    @GordonFreeman Thanks for the great code snippet, but there is one glitch there, add abs(), like ceil($width-($width*abs($r-$w/$h))) and same to the height part. It is needed for some cases. Commented Jul 31, 2013 at 18:50
  • 1
    Changing the default crop value to true, and resizing the image at http://wallpapercave.com/wp/wc1701171.jpg to 400x128 (banner) created a black image; I can't figure out why it's doing that though. Commented Oct 29, 2016 at 14:12
  • 8
    To save the resized image in the file system add imagejpeg($dst, $file); after the imagecopyresampled($dst,... line. Change $file if you don't want to overwrite the original. Commented Jan 20, 2020 at 20:57