Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP image resize on the fly vs storing resized images

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.

like image 272
Pablo Avatar asked May 12 '10 23:05

Pablo


People also ask

How do I resize an image without losing the details?

If you want to resize an image without losing quality, you need to make sure that the "Resample" checkbox is unchecked. This checkbox tells Paint to change the number of pixels in the image. When you uncheck this box, Paint will not change the number of pixels, and the quality of the image will not be reduced.

Does resizing an image reduce quality?

The most common side effect of scaling an image larger than its original dimensions is that the image may appear to be very fuzzy or pixelated. Scaling images smaller than the original dimensions does not affect quality as much, but can have other side effects.


2 Answers

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).

like image 91
Your Common Sense Avatar answered Sep 28 '22 18:09

Your Common Sense


Dynamic resizing can be a costly procedure (time-wise) depending on the initial size of the images. I've done it in production systems, but when I have the choice, I strongly favor caching to disk. After all, disk space is cheap, and load time is everything on the Web. Even if you simply cache thumbnails at a specific size and do dynamic resizing everywhere else, you can greatly reduce loading times for gallery-style image lists.

like image 25
warrenm Avatar answered Sep 28 '22 18:09

warrenm