Image Manager PHP Class

This is a PHP class designed to help with the process of resizing, displaying and captioning images on a website. It uses the GD2 PHP graphics library to resize images, and stores the generated images in a cache to prevent the system from slowing down website loading times.

Version 0.1alpha

This code is free to use for non-commercial purposes. For commercial purposes, it'll probably be free too, but I request that you get in touch at robin@solarisedesign.co.uk first to let me know

What it can do

At the moment, this class can help you to

  • Resize images on-the-fly
  • Store resized images in a temporary directory for faster performance
  • Change the generated quality of image, e.g. to reduce filesize
  • Apply watermarks to an image
  • Add captions to images

The following samples show a few different ways in which this class can be used...

Resizing

Original image is 800x600

Example 1: Resizing the same image to 320x240 and 240x180 respectively Example 1 Code:
<img src="/images/create/showimage.php?image=images/p1.jpg&size=320x240" />
<img src="/images/create/showimage.php?image=images/p1.jpg&size=240x180" />

Captioning

Example 2: Using the caption function to add text to an image, as well as resizing to 320x240. You can set which font you want to use for the caption in config.ini Example 2 Code:
<img src="/images/create/showimage.php?image=images/p2.jpg&size=320x240&caption=Look at the lovely caption!" />
<img src="/images/create/showimage.php?image=images/p2.jpg&size=320x140&caption=Resized to a longer, shallower shape. And a bigger caption too!&caption_fontsize=11" />

Watermarks

Example 3 : Long and wide, with a watermark too! To use watermarks, replace the image watermark.png with your own, transparent PNG image Example 3 Code:
<img src="/images/create/showimage.php?image=images/p3.jpg&size=640x220&use_watermark=yes" />

How To Use

1. Download the PHP class and related files here

2. Extract the files to a directory on a PHP-enabled website host, either on your own machine, or on a remote host.

3. Set the tmp directory to be writable (set permissions to 777 at a minimum)

4. Using the provided .php file, showimage.php, add IMG html tags to display images by referring to the showimage.php file as the "src" of the image (see provided examples to see how this works)

5. Look in the config.ini file to see other settings. These can all be over-ridden by appending them to the query string, e.g. showimage.php?image=images/p2.jpg&size=320x240&use_watermark=yes will show the watermark on the image, as well as resizing it

Notes: Image paths should be relative to the directory where you uploaded the script for maximum working-ness. Other configurations should work, but best practice for now is to use relative paths, e.g. /images/create/showimage.php?image=../../p2.jpg for an image in the root directory

To do:
  • Watermarks cannot be generated by text yet, only transparent PNG images
  • For transparent PNG watermarks, should be possible to use images with white backgrounds, and automatically use transparency, or blend effects
  • Only images from the local host can be used (e.g. www.solarisedesign.co.uk/myimg.jpg is ok, but www.google.com/myimg.jpg is not)
  • Might add in support for rotating images. That would be fun
  • Other things... let me know what you think - robin@solarisedesign.co.uk
My Website Design Portfolio »