Results 1 to 2 of 2

Thread: Thumbnail Generator

  1. #1
    Join Date
    May 2005
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Thumbnail Generator

    Hi guys,

    I'm looking for either a programme or script that can generate thumbnails from large images. I want it to be able to output an image at a certain ratio (i.e. 100x100) and but rather than squash the original, to take the centre of the large image to create the small image.

    I know it can be done, but can anyone recommend how or what with?

    Thanks!
    Matt

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    take the centre of the large image to create the small image.
    The scripts I've written for generating thumbnails are designed to have a maximum height and width of 100x100. It then finds which is the large dimension and shrinks the other proportionally.
    A 1000x500px image would become 100x50, and so forth.
    Additionally, if both dimensions are under 100x100, then it simply leaves as is, for a smaller image, such as a logo, rather than stretching it.

    It would be possible to take the center, but this isn't a great representation of the full image. It would also lead to pixelated previews for small images (unless you're sure you wont have any).

    Here's a script that will do what I posted above. If you need it to do something else, ask, and I'll see what can be done.

    PHP Code:
    <?php
    //thumbnail.php
    //use this script as any image
    //ex: <img src="thumbnail.php?img=trees.jpg">
    //Note that you can use any path you'd like, but I would recommend
    //adding something to the script to disallow remote images.
    //This way, no one just uses your script.
    //Adding another directory to the path within the script will solve this:
    //$path = 'images/'.$img;

    //There should be no real need to modify any of this
    //but I have marked several areas that can have custom values

    include('bmp.php');
    //BMP functions -- SEE NOTE

    // The file
    $img = isset($_GET['img'])?$_GET['img']:'';
    if (
    $img == '') die('Image Does Not Exist');
    //You could enable a default image with the error message
    //so the error displayed in a page as an image (rather than
    //an X for image not loaded).
    //instead of die(), use $img = 'error.jpg';

    //as described above, this prevents custom locations of images
    //so no one can use your script from another page
    $dir 'images/';
    //change as you want

    $filename $dir.urldecode($_GET['img']);

    function 
    getimagetype($img) {
        
    $size getimagesize($img);
        
    $mime $size['mime'];
        
    $type substr($mime,strpos($mime,'/')+1);
        if (
    in_array($type, array('gif''png''bmp'))) {
            return 
    $type;
        }
        else if (
    $type == 'jpeg') {
            return 
    'jpg';
        }
        else {
            return 
    FALSE;
        }
    }

    if (
    getimagetype($filename)) {
        
    $ext getimagetype($filename);
        
    $imageextok 1;
    }
    $extn=0;
    foreach (array(
    "jpg","gif","png","bmp") as $imageext) {
        
    $extn++;
        if (
    $ext == $imageext) { break; }
    }

    if(
    $imageextok != 1) { die('Image Filetype Incorrect - '.$filename); }

    // Set a maximum height and width
    $width 100;
    $height 100;

    // Make image
    if ($extn == 1) {
        if (@
    imagecreatefromjpeg($filename)) $image imagecreatefromjpeg($filename);
        }
    else if (
    $extn == 2) {
        if (@
    imagecreatefromgif($filename)) $image imagecreatefromgif($filename);
        }
    else if (
    $extn == 3) {
        if (@
    imagecreatefrompng($filename)) $image imagecreatefrompng($filename);
        }
    else if (
    $extn == 4) {
        if (@
    imagecreatefrombmp($filename)) $image imagecreatefrombmp($filename);
        }
    if (!isset(
    $image)) die('Image Filetype Incorrect.');

    // Get new dimensions
    $width_orig imagesx($image);
    $height_orig imagesy($image);

    //override w/h with smaller values if image is under 100x100 for w&h
    if ($width_orig<$width && $height_orig<$height) {
       
    $height $height_orig;
       
    $width $width_orig;
    }
    else {
       
    $ratio_orig $width_orig/$height_orig;
       if (
    $width/$height $ratio_orig) {
          
    $width $height*$ratio_orig;
       } else {
          
    $height $width/$ratio_orig;
       }
    }

    // Resample
    $image_p imagecreatetruecolor($width$height);
    imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

    // Content type
    header('Content-type: image/gif');
    // Output
    imagegif($image_pnull30);
    ////FORMAT OPTIONS:
    ////replace 'gif' in the last two lines with 'png' or 'jpeg' if desired.
    ////for jpg, quality (30 above) is needed; for png, remove 3rd parameter
    ////bmp would also work if you have bmp.php included, but not good for
    ////thumbnails, due to size.
    ?>
    Note: to use this you must have PHP installed/enabled on your server.

    For BMP images to work, you must include the bmp.php script. That can be found here (and by a google search for 'imagecreatefrombmp'): http://www.hotscripts.com/Detailed/41077.html

    Also, this script is a bit slow, but that's just how it works generating thumbnails from huge images, at least with the PHP GD library.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •