NYCPHP Meetup

NYPHP.org

[nycphp-talk] photo album

Steve Manes smanes at magpie.com
Mon Dec 23 20:39:48 EST 2002


At 03:39 PM 12/23/2002 -0500, Michael J Lee wrote:
>What would be the best way to store the images in the database?  Links to
>the images?  Can you store the actual images? (I'm guessing that would not
>be recommended . . )

I would never store images in a database.  For one thing, they can't be 
locally cached.  For another it precludes the use of a dedicated image 
server, or at least complicates it.  For another, it takes a lot more 
bandwidth to pull something from a database than it does from a 
filesystem.  And if the application should ever grow to the scale where 
edge delivery is desirable...

You could store the paths in the database but if you have a lot of images 
you have to be careful about overloading directories.  Typically, 
hand-rolled photo archives are based on a bunch of categorized 
directories.  For instance, you might have thousands of photos in the "NYC" 
category and only a handful of snapshots of Boise.  As those directories 
grow, search times slow and maintenance becomes more problematic.

A better solution which tends to balance out a directory tree better is 
create a two or three tiered directory structure based on an MD5 hash of 
the image's name or image id:

     $IMAGE_DIR = "/images";
     $hash  = md5($image_name);
     $relative_path = sprintf("%s/%s/%s/%s", $IMAGE_DIR,
         substr($hash, 0, 2), substr($hash, 2, 2), $image_name);

This creates a pseudo-random distribution of directories.  Run through this 
function, the image "slobbering_dog.jpg" would produce this directory 
hierarchy:

/images/fe/24/slobbering_dog.jpg

Then you only need to store the image's name, or numeric id, in the 
database (along with a category id probably).  If you have millions of 
images, I'd probably increase those node names to three characters.  We 
use  this at CCI too, BTW.





More information about the talk mailing list