Naveen Kumar
Naveen Kumar

Reputation: 4601

Create thumbnails using PHP or manully add thumbnails?

I have a Folder called "Gallery" inside which i have created a folder for Thumbails called "Gallery-Thumbs", where i manually add thumbnails.

My question is whether it is better to manually add thumbnails or create thumbnails dynamically using "GD" or "ImageMagick".

Problems with adding Thumbnails manually

So does using PHP ImageProcessing function add additional overhead in creating thumbnails or is the correct approach?

-- Updated for answering Queries

  1. How you add images to the "gallery" folder?

    • Through Direct file upload
  2. How those images and thumbnails are accessed?

    • I use glob to get list of files in respective folders
  3. How do you (want to) map images to thumbnails?

    • For thumbnails i use imageName-thumb.jpg, so they appear in the list in the same order as they are in main image folder

Upvotes: 3

Views: 1851

Answers (5)

BasisPoints
BasisPoints

Reputation: 1

As stated previously you need PHP 5 with GD support. If you have these then here is a very handy function to create thumbnails of a given dimensions and quality ($options) from every image in a given directory ($from_dir) and save them to another directory ($to_dir).

function make_thumbnails($from_dir,$to_dir, $options){
    $files = scandir($from_dir);
    $exclude = array('.','..','etc.');
    foreach($files as $fi => $fv){
        if(!in_array($fv,$exclude)){
            $from_file = $from_dir.$fv;
            $to_file = $target_dir.$fv;
            list($img_width, $img_height,$img_type) = getimagesize($from_file);
            $scale = min($options['max_width'] / $img_width,
                                        $options['max_height'] / $img_height
                               );
            $new_width = $img_width * $scale;
            $new_height = $img_height * $scale;
            $new_img = imagecreatetruecolor($new_width, $new_height);
            $src_img = imagecreatefromjpeg($from_file);
            $success = $src_img && imagecopyresampled(
                    $new_img,
                    $src_img,
                    0, 0, 0, 0,
                    $new_width,
                    $new_height,
                    $img_width,
                    $img_height
            ) && imagejpeg($new_img, $to_file, $options['quality']);
                              //Monitor results with $success - returns 1 or null
            echo '<br />success:['.$success.']';
        }
    }
}

//Set options
$from_dir = ':/source/dir';
$to_dir = ':/destination/dir';
$options = array();
$options['max_width'] = 100;
$options['max_height'] = 100;
$options['quality'] = 100;

// Make thumbs...
make_thumbnails($from_dir,$to_dir,$options);

Upvotes: 0

Chris Watts
Chris Watts

Reputation: 6725

I highly recommend using GD with some sort of caching to keep the ones that don't change. There is, however, already a superb library for doing this. It's an absolute favourite of mine and gives you easy image compression and resizing just with a GET URL.

Try Smart Image Resizer by ShiftingPixel: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

You can, if you wish, use a website I made as a reference using its page source: http://www.eastwood-whelpton.co.uk/about/gallery.php

Nearly all images on this website use the Smart Image Resizer GD library.

I can also provide the PHP code I used if you wish for examples. This particular code automatically adds any images found in my gallery folder to this page.

Upvotes: 0

Bonzo
Bonzo

Reputation: 5299

I create the thumbnails during upload and either save in a different directory with the same name or add th_ to the front of the filename. You could also upload some code to generate all the thumbs again for the images you currently have in your gallery in case you have missed any. You could also resize to different sizes at the same time, watermark or add other effects. This is my gallery and I have a thumb and normal size; the thumb was sharpened on upload and the normal watermarked. Bother versions have a drop shadow and the resizing etc. was done with Imagemagick. http://www.rubble.info/gallery/

If you check the file is there before displaying it and it is missing you can show a default image.

You can check out my website for lots of different things you can do with php and Imagemagick.

Upvotes: 0

Shaikh Farooque
Shaikh Farooque

Reputation: 2640

If your going with some Dynamic image processing tool then it will be little bit time consuming process, except that it will be a great technique.

If you are going with the manual process then:

  • Make thumbnail compulsory using some client side script
  • It depends on your image orientation

Upvotes: 1

Aziz
Aziz

Reputation: 20765

Use PHP to create thumbnails once and save them in a "thumbnails" directory with the same file names. Then use these now-ready-to-be-used thumbnails directly.

Upvotes: 2

Related Questions