How to best store and manipulate images on a large site?

I know there are many threads on storing images, but I can’t really arrive at a conclusion in terms of what’s best for my current project.

To put it simply, it’s a mix between eBay and Gumtree.com. It will contain listings which will have photos and images associated with them. Users will be able to upload a set number of photos for each listing. Each photo will need to be resized to a decent full size and also to a thumbnail size. What I am struggling with is:

  • How and where to store the images
    Should I store them on the same server or different?
    Should I store all images in one folder or separate them into a folder for each listing?
    Should I store and keep the original non resized ones?
  • What mechanism or script should I use to validate and resize them
    Should I store them readily resized or resize them each time it’s displayed using some php script?
  • What method gets the most performance

I would appreciate if I could get any guidance on this. Thanks.

Storing images is quite a debatable topic as it’s also quite a personal one and each developer/server admin has their own thoughts on the matter. So I will suggest what I would do so perhaps that will either answer your question or point you in the right direction.

  • How and where to store the images
    Should I store them on the same server or different? How much money do you have?
    Should I store all images in one folder or separate them into a folder for each listing? Same folder is easier.
    Should I store and keep the original non resized ones? If you are going to use it again.
  • What mechanism or script should I use to validate and resize them
    Should I store them readily resized or resize them each time it’s displayed using some php script? Just use the imagecreatefromjpeg function.
  • What method gets the most performance
    A custom method :wink:

If you don’t have much $$$ for servers and extra cool things, then just save them all in a parent /images/ directory, otherwise you can put it on a subdomain on the main domain and point the cname to your image storage server. You can upload an image and then create a uniqueid of it and then md5 the filename and store each size of the image you create as /images/bnmsayhjinkjy342_t.jpg, /images/bnmsayhjinkjy342_s.jpg, /images/bnmsayhjinkjy342_m.jpg, /images/bnmsayhjinkjy342_l.jpg (t=thumbnail, s=small, m=medium, l=large). and then store the md5 hash in your database and you can then access all the files when you need them super easily.

Try a function like the following perhaps:


function resizeImage($image, $width, $height){
    $tmp = split("\\.", basename($image));
    $ext = $tmp[1];
    
    switch( $ext ){
        case "jpg": case "JPG": case "jpeg": case "JPEG": $src_img = imagecreatefromjpeg($image); break;
        case "png": case "PNG": $src_img = imagecreatefrompng($image); break;
    }
    
    $dst_img = imagecreatetruecolor($width, $height);
    
    $src_dimensions = getimagesize($image);
    $src_width = $src_dimensions[0];
    $src_height = $src_dimensions[1];
    
    $prop_src = $src_width/$src_height;
    $prop_dst = $width/$height;
    
    if( $prop_src > $prop_dst ){
        $dst_height = $height;
        $dst_width = $src_width*$height/$src_height;
        $prop_crop = $src_height/$height;
        $src_point_x = abs($src_width-$width*$prop_crop)/2;
        $src_point_y = 0;
    } else {
        $dst_width = $width;
        $dst_height = $src_height*$width/$src_width;
        $prop_crop = $src_width/$width;
        $src_point_x = 0;
        $src_point_y = abs($src_height-$height*$prop_crop)/2;
    }
    
    imagecopyresampled($dst_img, $src_img, 0, 0, $src_point_x, $src_point_y, $dst_width, $dst_height, $src_width, $src_height);
    
    switch( $ext ){
        case "jpg": case "JPG": case "jpeg": case "JPEG": imagejpeg($dst_img, $image); break;
        case "png": case "PNG": imagepng($dst_img, $image); break;
    }
    
}