This set of scripts will allow you to build a light-weight images server and dynamic thubnail generation & caching, based on a repository where the original images will remain “untouched”.
Actually, the Image Server allow two options for original images storage: local storage and Amazon S3 storage.
Image Server integrates the official Amazon S3 library and PHP Thumb for image generation / resizing.
- Point a VirtualHost DocumentRoot to
/public/
folder. - Use
/public/index.php
script as a dispatcher for all requests for files that doesn’t exists. There’s several ways to accomplish this, depending on what web server you use. (Fast implementation for Apache:ErrorDocument 404 /index.php
) - Optimize your web server configuration to set a far future expire date for static files.
chmod 777 ./public
Some example usages:
Case 1:
- Original images stored locally at /home/me/media/.
- Remote images will be called from http://stc.server.net/.
- http://stc.server.net/users/john.jpg will show (and locally cache) original image from /home/me/media/users/john.jpg
- http://stc.server.net/users/john_a.jpg will show (and locally cache) avatar size thumbnail from /home/me/media/users/john.jpg
Note that we directly translate request route to local path.
Lines to customize:
$config['filesystem'] = 'local';
$config['local_files'] = '/home/me/media/';
Case 2
- Original images stored locally at /home/me/media/.
- Remote images will be called from http://stc.server.net/.
- http://stc.server.net/category-name/1312.jpg will show (and locally cache) original image from /home/me/media/1312.jpg
- http://stc.server.net/category-name/seo-optimized-product-name_1312.jpg will show (and locally cache) original image from /home/me/media/1312.jpg
- http://stc.server.net/category-name/seo-optimized-product-name_1312_a.jpg will show (and locally cache) avatar size thumbnail from /home/me/media/1312.jpg
Note that we are using an unexistent /category-name/ folder, and a seo-optimized-product-name add to real image name (a numeric product id) on URL requests.
Lines to customize:
$config['filesystem'] = 'local';
$config['local_files'] = '/home/me/media/';
$config['path_conversions'] = array(
'/^category-name\/.+-([0-9]+)\./' => '\\1.'
);
Since this image server will mantain local copies of every requested and resized image, you’ll need to expire and remove local cached images when you make changes to original images or remove them.
You can make a REST call prepending /delete/
to image request path. For instance, for deleting local cached image http://stc.domain.net/user/obokaman.jpg (and all the related thumbnails), you can call http://stc.domain.net/delete/user/obokaman.jpg
This call will return you a JSON answer with an array called “deleted-images” containing all images that has been removed from local cache.
It will be a good practice to automatically remove those locally cached images that hasn’t been accessed for a long time. You can set up a cronjob to check your local images cache and remove those images that hasn’t been accessed for, let’s say, last month. You can retrieve this information using the filatime
php function or find %path% -atime +%num%
command, wich would return all files in path that were accessed more than num days ago.
For instance:
find /home/me/media/ -atime +5 | xargs rm
This will delete all files in /home/me/media/ not being accessed in last five days.