Forum Moderators: coopster

Message Too Old, No Replies

Dynamic graphics (GD) vs. stored thumbnails

Which route to go for diskspace vs. server load.

         

Duskrider

6:10 pm on May 10, 2007 (gmt 0)

10+ Year Member



The site I'm currently working on uses GD in php to create dynamic images and then scale them down to a reasonable thumbnail size and display them to my visitors. Currently the site doesn't store any of these files... they're all served dynamically. To keep it simple let's say that each user of the site gets their own 400 x 400 image created dynamically (database driven). Now every other visitor to the site can do a search for images and are presented with up to 9 thumbnails of other user's design at 150x150 on the same page.

The original image sizes are 400x400 and are only displayed one at a time that way. The thumbnails are generated from the exact same script but are resized to 150x150 and then displayed up to 9 on a page. I've been noticing that the display of these thumbnails can be slow at times and I'm beginning to think I'd be better off actually storing each thumbnail as an actual image file and serving it that way, rather than depending on GD to do all the grunt work every time someone requests a page with 9 thumbnails.

Hopefully you can see what I mean.

I'm looking for thoughs, experiences with GD, and recommendations on this. I don't want to overtax the server displaying images, but I would also rather avoid storing tens of thousands of thumbnails if I can avoid it too.

Just how much power does GD take to create an image. How about 9 at the same time? How about if I've got 5 users at once requesting a total of 45 images being generated at the same time? The more I scale it up the more I start to think I'm better off sacrificing the storage.

Thanks!

jatar_k

6:44 pm on May 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I imagine you are looking for a more in depth answer than I'm about to give but..

It's just too slow to gen images on the fly, I always store them, you can have it be automated, made when you upload the photo or have a script you can run that will gen them for you.

size isn't much of an issue, they're thumbnails, they're supposed to be tiny.

Anything that slows things down for the user is an issue

henry0

7:12 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't hurt to review
THIS
[webmasterworld.com] still missing part of the answer but could be useful :) in dealing with serving volume

joelgreen

1:28 pm on May 11, 2007 (gmt 0)

10+ Year Member



You could cache images for some time, and have some cron job deleting all images older than NN. This would speedup further image serving and will limit server space usage to only most recent images.

whoisgregg

1:42 pm on May 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need for a cron, just have the script check the filemtime of the cached image. If it's too old, recreate it.

<?php
$image_name = 'some-test-image.png';
$cache_file_name = $_SERVER['DOCUMENT_ROOT'].'/cache/'.$image_name;
$cache_age = ( 60 * 60 * 2 ); // two hours
if(
(file_exists($cache_file_name)) &&
(filemtime($cache_file_name) > ( time() - $cache_age ))
){
// then use the cached version
} else {
// rebuild the cached version and export it to the cache path
// your image/thumbnail generation code here, then export to the right place image*
// imagepng( $im, $cache_file_name );
}
echo '<img src="/cache/'.$image_name.'" />';
?>

joelgreen

2:53 pm on May 11, 2007 (gmt 0)

10+ Year Member



If it's too old, recreate it.

I suggested to delete old images to minimize disk space usage. Some images may be never really needed/accessed again. So why keep them on the server? This will free space for other images.

whoisgregg

4:24 pm on May 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you're right. I misread your post to mean a cron would rebuild the cached images, not just clean out old ones. Sorry about that, I must not have fully woken up yet when I posted that. ;)