JOE DESIGNS

A dev shop in Albuquerque New Mexico.

PHP image on the fly resizing with caching, and croping.

Filed under "general" on 9/2/08

Edit: Feb 5th 2012 1:34 PM

Thanks to palepurple some needed updates have happened to this script.

Changes:

*   2012/01/30 - David Goodwin - call escapeshellarg on parameters going into the shell
*   @param string $imagePath - either a local absolute/relative path, or a remote URL (e.g. http://...flickr.com/.../ ). See SECURITY note above.
*   @param array $opts (w(pixels), h(pixels), crop(boolean), scale(boolean), thumbnail(boolean), maxOnly(boolean), canvas-color(#abcabc), output-filename(string), cache\_http\_minutes(int))
*   @return new URL for resized image.    

Edit: July 1st 2009 1:46 AM

There is a new version of php image resize which now supports remote images, you can download it now or visit the github page here:

Visit PHP Image Resize (GitHub)

There is also a new example page included in the repository.

---------------------------------------------------------------------

Just thought I would share a recent function that we have built to resize images on the fly, so if you have an image that is 800x800, and you want to make a thumbnail to 200x200 just use the function like this:

<img src="<?php echo resize("PATH_TO_IMAGE",array("w"=>200,"h"=>200)); ?>" border="0" />

Be sure to check out the example page.

Configuring is pretty simple, just make sure the cache directory that you choose is writable by the server, and you should be up and running.

The script will compare the generated images timestamp to the original image's timestamp, so if the original image is changed all cached images will be re-built.

I hope you enjoy, and let us know if you have any questions, thanks!