Performance Notes

April 30, 2015 ยท View on GitHub

You should not generate images on the fly, especially not on high traffic sites, it might get your server locked up in the worst case because of the amount of simultaneous requests. The first request will hit your server and start generating the image while others try to do that at the same time, causing the site become locked up in the worst case. There is no need to generate images on each request, in fact it is bad practice because it will just put load on you servers CPU and consume memory.

It's better to generate the needed versions directly after an image was uploaded and if other versions are needed later, generate them by a shell script. For this purpose there is a method in the ImagineComponent that will turn the image operation array into a string, see ImagineComponent::getHash();

Suffix your image with the string generated by this method to be able to batch delete a file that has versions of it cached. The intended usage of this is to store the files like this for example:

my_horse.thumbnail+width-100-height+100.jpg

Instead of having all the image operations in the filename, which could become pretty long and maybe exceed filesystem limitations, it would be better to hash the string to generate a file name like this:

my_horse.e18c820f1c3da390e3d01b4fb91b0f68.jpg

You can even truncate the hash to 8 or maybe even 6 or 4 characters. No warranty given on the probability of duplicates in this case.

my_horse.e18c82.jpg

So after upload store your image meta data in a database, give the filename the id of the record and suffix it with this string and store the string also in the database. In the views, if no further control over the image access is needed, you can simply link the image directly.

$this->Html->image('/images/05/04/61/my_horse.thumbnail+width-100-height+100.jpg');