Codeigniter Image Cache & Auto Size on Demand”
What
This ‘library’ for lack of a better description allows designers to use libraries of high-res images anywhere in the site, at any size, without having to pre-define or pre-scale your images. It can also be used on the fly with user uploaded photos in a gallery setting.
Why
Other alternatives I have found in both codeigniter and other frameworks/cms’s like Drupal, EE, and WordPress require the administrators to pre-define set image sizes like thumbnail, medium, large. While this makes sense for a new site, it can be an issue if you want to redesing and start using a different size for thumbnails, previews etc.
Basic Usage Overview
To use the ‘library’, all you need to do is set your img tag source to point to the controller with a few pre-defined uri segments.
Image Tag: <img src=”/img/size/o/folders_your_image_is_in-your_image.jpg/w/240/h/160/m/auto” />
* If you arn’t using .htaccess to mask your index.php.. change the path to index.php/img/size…..
Sample site using library: http://www.synlawn.com/gallery
Features
- Fully customizable with little work
- Can support any directory depth in any location (does not need to be web accessible)
- Automatically caches called images in their original directory at called size so it only processes once
- Can be dropped into any CI installation – It was even originally written as a Matchbox module
- Maintains your image heirarchy and file name in source (for SEO) and keeps images available for external reference.
- Uses default codeigniter img_library, nothing else to install (unless you do not have imagemagick, netpbm, gd, or gd2)
- Uses a default ‘error/not-found’ image so you can stick a site logo on a high res image to use anywhere in case of an error (no image-not-found issues)
Still working on
- Auto re-cache images if full-size date is newer than cached files date
- Does not support directories or images with a ‘-’ in the name (will replace with less common charactor eventually)
- Add additional flag to render full html doc with self-generated img tag (so you can do ajax call to script for full size images)
- If you have errors turned on images will not resize and you will get a blank rendering.
- other things i haven’t found/thought of yet… please add comments
Installation
Drop the img.php controller into your controllers folder and drop the img_model.php into your models folder. Also, make sure all of your images directories are writable by script user.
Usage & Instructions
Setup your default image and base path
- In the construct of the img_model, set the ‘path’ property to the base directory all your resizable images will be in
- set the ‘default’ property in the img_model to the location of your default ‘not-found’ image. This path is relative to your ‘path’ you set in the last bullet.
The image src path needs 4 key value pairs that get parsed. They are:
O: Origional
This is where you define the path to your image relative to $img_model->path; Your $img_model->path property can be anywhere you script has permission to access (ie: below web accesible www directory). The directories are seperated by ‘-’s so if your origional image is in ‘product_photos/shirts/mens_t.jpg’ your path will look like ‘product_photos-shirts-mens_t.jpg’
W: Width
This is your target width of the generated image in pixels
H: Height
This is your target height of the generated image in pixels
M: Master Dimension
This is your master dimension (defaults to auto). This is where you can require your image be at least the width or height. An example would be on a listing page, you want all of your thumbnails to be the same width or height so your design isn’t screwed up.
Ok, so where are the files?
Download thie zip file: img_cache.zip
Please enjoy, and let me know if there are any issues or ideas.


Hy!
This is an amazing library! But.. not too secure.. Example: If I want to kill/overload your webserver, I just need to write a script that generates different width and height values and then call millions of requests. Your server machine will generates millions of images, your CPU and memory will be on 100% and the HDD will be full soon. Have you got an idea to protect this type of attacks?
Best regards,
Peter
Hey,
good call – when i do stuff like this, i tend to let my “don’t trust users” guard down. – Probably, the easiest way, is to add an allowed sizes config somewhere . this way the most it will generate are one for each size on each image.
Yes, good idea, I will extend with an allowed (size)range controll or sg. like that. Or an other solution: max. number of images in the img_cache path..
Can also go a bit further and limit how many of the specific images. So ‘there are already 10 varients of this image’ etc