Image Resize

I am looking for a script to resize images… basically I have images on my site which some are 1MB +, I want a script to check the size of an image, and if bigger than X, reduce its size (not reduce its size and save the file, just reduce it via script which I can use in an image tag).

Thanks.

Look through the examples and the comments, there are lots of examples you can copy and paste:

http://php.net/manual/en/function.imagecopyresampled.php

Your code will be:

  1. Use filesize() to get the size of the image requested (probably through $_GET parameter to your script)
  2. If over 1MB, use imagecopyresampled() to generate a smaller image
  3. Send the appropriate content-type header (png/jpg/gif whatever format you choose to create)
  4. Send the image data with imagepng/imagejpeg/etc

Hi Dan, I seem to have got this working… but it seems so slow?

When I’m doing say 10-20 images on a page and using the script to resize each one, they all load painfully slow?

Thanks.

I wouldn’t expect it to be painfully slow, but slower than serving static images, sure.

You’re basically asking your server to be Photoshop. It’s doing 20 image manipulations, which takes CPU time to do, every time someone loads the page.

Most sites that do thumbnailing for some reason save the thumbnails so they can be served directly from the files in the future, not recreate them on every page load.

I just posted this: http://www.sitepoint.com/forums/showthread.php?t=665775

Hi Dan,

I have just finished coding the script.

http://www.freemoviedb.com/imgs/upload/thewolfman.jpg ->
http://www.freemoviedb.com/imgs/upload/thumbs/thumb_thewolfman.jpg

http://www.freemoviedb.com/imgs/upload/cityofmotherlylove.png ->
http://www.freemoviedb.com/imgs/upload/thumbs/thumb_cityofmotherlylove.png

It runs through the database and resizes every image to a specified value (which I needed) and then uploads it to a thumb folder.

Did ALL 400 thumbs quicker than it took to load 1 instance of imagecopyresample.

Thanks.

On top of that, to see a huge difference:

http://www.freemoviedb.com/imgs/upload/iamlegend.jpg

->

http://www.freemoviedb.com/imgs/upload/resized/iamlegend.jpg