Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...