Overview
ImageResize is a php class which enables any image within any subfolder to be resized to any proportion. Images won't be stretched (by default) and are cached. The script is unobtrusive; no modification is necessary to the storage location and/or uri of an image and the script only runs when query string parameters are added to an image uri.
Installation
Copy the files class.resize.php
, .htaccess
and resize.php
and the cache
folder to any folder within your website root. If there is already a .htaccess
file, add the contents of the new .htaccess file
to the end of the existing file.
You will now be able to append width
, height
, quality
and mode
to the url and the image displayed will be resized as expected.
Resizing Modes
There are three modes available, specified using the mode
querystring: fit (default), stretch and crop.
Fit
Fit takes the original image and resizes it to fit within the specified width
and height
dimensions:
/path/to/image/spitfire.jpg?width=100&height=100&mode=fit
Original:

156 x 200 pixels
At size:

100 x 100
Output:

77 x 100
Stretch
Stretch takes the original image and stretches (or shrinks) it to the width
and height
dimensions. The aspect ratio of the image is not maintained:
/path/to/image/spitfire.jpg?width=100&height=100&mode=stretch
Original:

156 x 200 pixels
At size:

100 x 100
Output:

100 x 100
Crop
Crop takes the original image and resizes it to the width
and height
dimensions, trimming off any excess around the edges:
/path/to/image/spitfire.jpg?width=100&height=100&mode=crop
Original:

156 x 200 pixels
At size:

100 x 100
Output:

100 x 100
Changing the quality of an image
If you want to change the quality of the output image (and therefore the filesize) add a quality
parameter to the url:
/path/to/image/spitfire.jpg?width=100&height=100&quality=50
Original:

3,952 bytes
Quality = 50%

2,740 bytes
Quality = 25%

2,191 bytes
The default quality is 80%
Caching
ImageResize supports caching - generated files are saved into a cache folder to avoid having to keep regenerating the same resized image. The cache folder location is stored in class.resize.php
in the variable $this->cache_folder
. The maximum age of the cached files is stored in $this->cache_age
.
Possible errors and debugging
If a resized image does not appear, open the image in a new window and add &debug
to the end of the url. A set of data will be shown, including an error log. If there is nothing obvious, copy and paste this into an email to me at adam.hopkinson@gmail.com and I'll do my best to help.
Other stuff
All work is my own, including the spitfire image © Adam Hopkinson 2009