Here is a script that takes a regular sized image and resizes it on the fly. It is a PHP Image Resizer. It also caches the result, to save resources and execution time. It’s fairly easy to use:
1. Upload the cache.php script to your document root.
2. Create a directory called ‘cache’, CHMOD to 0777 and place a blank index.html file in there.
And that’s all we need to do. Next, we have to pass the image location to the script and the width & height. This is where we can just use URL variables:
- f = absolute path to the image
- w = width (in pixels)
- h = height (in pixels)
If there are any other features you’ve seen in other PHP Image Resizers and think should be added, feel free to leave a comment.
For instance, to display a resized image, we would have to do something like:
<img src="http://phpduck.com/cache.php?f=/images/original_image.jpg&h=100&w=200" alt="Resized Image" />
There is also the option to crop an image. For instance, if we had an image with an aspect ratio of 4:3, and we shrunk it to a smaller size with an aspect ratio of 2:3, it would appear squashed and distorted. This is where the crop tool comes in handy; while we lose some of the image, we make sure that it at least appears normal. To crop an image, you simply have to include the ‘crop’ variable in the URL. i.e.:
<img src="http://phpduck.com/cache.php?f=/images/original_image.jpg&h=100&w=200&crop" alt="Resized Cropped Image" />
Click the following to download the php image resizer.