views:

483

answers:

4

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, Thanks.

+6  A: 

This sounds like premature optimisation. Do you know how many users your site will have/ how much computational grunt your server(s) will have? Go with the simplest (maintenance-wise) option, i.e. resize on the fly, until performance becomes an issue, then figure out from there what to do.

It might be an idea to implement some sort of server side caching of your rescaled images, if they're likely to be repeatedly hit, but I don't think this need extend as far as explicit pre-rendering.

spender
Thanks, for the advice. Im currently caching all thumbnails as there will be many in one page and left the dynamic resizing for the picture page and the download options.
Pablo
A: 

how big is the gap between the two methods in speed and performance?

Negligible. Negligible, probably - unless you're dealing with enormous images, or a very large number of images.

Cam
You've clearly never run benchmarks on resizing dozens of images on the fly. There can be a very noticeable difference.
warrenm
I don't think this is a well qualified answer. Images can be very large, and quality resizing can be quite expensive.
spender
upon upload im resizing the images to max width of 1024px
Pablo
+6  A: 

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.

warrenm
you are right, i have 400GB available on my live host. I think i'll just store all the resized images. Thanks for sharing.
Pablo
+2  A: 

I strongly advice you to cache your images, and NOT resize on-the-fly.

resizing of images is very CPU intensive and memory consuming for your server.

If you have a gallery of images that is going to scale on the fly, the page is going to load the images slowly, say something like 3-10 seconds, depends on original filesize.

When resizing it takes about 3 bytes pr pixel of your memory. So If you have an image 1000x1000 to be resized, it will take about 3MB of memory. If your one of your webpages has many of these resize-on-the-fly images, say 20, it will take about 60MB of RAM of your server. Maybe not, since most clients only requests 4 images at the time, but 12MB is still a lot for a pageload. I would only scale on the fly if the source image is less that 100x100 px.

TIP: A great lib for scaling and saving thumbs is PhpThumb

PHP_Jedi
Thanks for the input, im defiantly storing the images in the filesystem.
Pablo