Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practice when it comes to storing images for a gallery?

My question is not about storing images on disk or in DB.

  • Images will be stored on disk
  • Image path and other image data will be saved in database.
  • Images will be given a unique filename
  • Images will be stored in 3 sizes
  • In time there may be many images used by many users

My questions are:
- Should images be stored in one folder, or many folders?
- Is it ok to use md5 for creating unique id's? E.g. md5(id+filename+random_num)
- Should images be cached on server or on clients browser / computer?

Anything else I should think of?

The solution is using php, apache and mysql. We use Uploadify for uploading images.

Some code I use today

  /**
   * Calculate dir tree for object
   * Folders starts from 00 to FF (HEX) and can have just as
   * many subfolders (I think :)
   * @param $id - User ID
   * @param $type - Image category
   * @return string
   */
  function calculateDirTree($id, $type)
  {
      $hashUserID   = substr(hash('md5', $id), -4);
      $parentFolder = substr($hashUserID,0,2);
      $subfolder    = substr($hashUserID,2);    
      $basePath     = $type."/".$parentFolder.'/'.$subfolder.'/';

      return $basePath;
  }  
like image 302
Steven Avatar asked Jan 25 '12 09:01

Steven


People also ask

Can you store images in key value store?

The key-value store is simple storage that can be used for storing any kind of data. It can be JSON or HTML documents, zip files, images, or simply strings.

How images are stored in postgresql?

For example, suppose you have a table containing the file names of images and you also want to store the image in a bytea column: CREATE TABLE images (imgname text, img bytea); To insert an image, you would use: File file = new File("myimage.


2 Answers

Should images be stored in one folder, or many folders?

You are talking about "100k - 200k images" so many folders is a must have. Try to have max. ~1000 images in on folder.

Is it ok to use md5 for creating unique id's? E.g. md5(id+filename+random_num)

Yes, you can do this. It will avoid problems with long filenames.

Should images be cached on server or on clients browser / computer?

The should be cached on the client side. The problem with so many images is that it creates high traffic. Caching on the client help reducing this.

like image 147
PiTheNumber Avatar answered Nov 15 '22 14:11

PiTheNumber


Depending on the number of images you want to handle, I would strongly suggest using several folders. The easiest way should be to use the first letter of the file name to create a folder structure. I think, the numbers are something like this:

less than 1000 images  --> one folder
less than 20000 images --> one level of folders (a, b, c, ...)
more                   --> several levels (a containing aa, ab, b containing ba, bb, ...)

YMMV

like image 39
nfechner Avatar answered Nov 15 '22 13:11

nfechner