I'm building a image sharing site and would like to know the pros and cons of resizing images on the fly with PHP and having the resized images stored.
Which is faster?
Which is more reliable?
How big is the gap between the two methods in speed and performance?
Please note that either way the images go through a PHP script for statistics like views or if hotlinking is allow etc... so is not like it will be a direct link for images if I opt to store the resize images.
I'll appreciated your comments or any helpful links on the subject.
This is absolutely incomparable matters.
Resizing images on the fly, in fact, is like running a DoS attack on your own server. Resize of one usual image require more CPU and RAM than serving one usual request to php script. That's ALREADY a huge impact on performance. Yet a usual thumbnail being shown not alone, but in numbers. So, while showing only one gallery page you are creating dozens of heavy load processes, increasing server load by a factor of ten or more.
Quick and dirty test to prove my words: Let's try to resize relatively small, 1,3 megapixel image
$ /usr/bin/time --format="%MK mem %Es CPU time" /usr/bin/convert angry_birds_1280x800.jpg -resize 100x100 thumb.jpg
10324K mem 0:00.10s CPU time
It took us 0,1s, so, showing 10 image previews will eat up a whole second of your CPU time. While properly written PHP gallery page will take around 0,01s. So, with your resize on the fly you are increasing the server load by a factor of 100.
Same with memory. Each resize process will eat no less than 10M of memory (to resize a 100k image file!) with a total sum of 100M. While usual memory limit for the PHP script is merely 8M and it is seldom reached.
That are the real life numbers.
A somewhat funny thing related to this problem:
Exactly the same PHP user who easily throwing away 1000000s of CPU cycles at the same time being incredible jealous to spare 1 or 2! It is not a figure of speech, here is an example on what I am talking about:
A similar question from someone, whose great concern at the same time in as negligible thing as speed difference between Constants, Variables or Variable Arrays. And who recently run into allowed memory size exhausted problem, as though such a disaster was not enough.
There are TONS of questions and answers on this site, debating nanosecond speed difference of whatever operations, answered with inexhaustible dignity, running tests of millions of iterations to show absolutely negligible difference between one-shot operations of several CPU cycles each.
And at the same time there are questions like this - regarding huge, incomparable difference in terms of performance between two approaches, which looks merely equal to the author.
That's the problem with average PHP user and this site.
The former just have no measure to tell real things from microscopic ones.
Yet the latter have no mechanism for sanity check for the questions - every one answered with equal enthusiasm, even if two questions contradicts with each other (and both with common sense).