SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    410
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating thumbs on the fly

    Hi folks...long time no post...

    I have a gd gallery installed on my server. I have uploaded images in a specific folder and i was wondering how i can pull these images onto the page at a specific thumb size. For example max hieght 100px/max width 100px, but keeping the aspect ratio.

    Ive seen a comprehensive tutorial on how to upload main images, and simulatiouly create a thumb, however is it possible to create them on the fly.

    Note: Information of the image file will be pulled down from a database.

    Thanks in advance

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is simple but you should only do it if you are only going to use the image once. Is that the case here?

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    410
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep...i have an initial page that pulls down records (each of these have text and a thumb image)...when the recordset is clicked for further info, the mian image will be shown on the next page...

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But are you sure this record is only going to be accessed once? If it is going to be accessed several times, it's best to create the thumbnail and then store it and pull its location from the database along with the record text.

    This is a way to create a 100x100 thumbnail from a large JPEG image:
    PHP Code:
    <?php
    function createthumb($img) {
    $imgsize getimagesize($img);
    list(
    $width$height) = $imgsize;
    $imgbuffer imagecreatetruecolor(100100);
    $image imagecreatefromjpeg($img);
    imagecopyresampled($imgbuffer$image0000100100$width$height);
    $imgoutput imagepng($imgbuffer"thumb.png");
    imagedestroy($imgbuffer);
    return 
    $imgoutput;
    }
    That outputs the thumb to a file (thumb.png). If you really want to do it on the fly, do this:
    PHP Code:
    imagepng($imgbuffer); 
    instead of the other line above.

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not just use phpThumb, that way you upload at whatever size you want and you have loads of display options including aspect ratio
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    410
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    um...food for thought.

    Im doing a small artist database so maybe it could get quite heavy as it grows....You mentioned it'd best to create the thumbs separately...is there a simple automated script that can be implemented during the upload process of the main image? (or is that the first script that you gave?!)....(sorry bit of a newbie), if so, how can this be action during a upload method in a form...if you could hold my hand on this it'd be appreciated!!

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the upload script, save the orig size image, and create the thumb.

    store thier filenames in the db.

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Modification of script above:
    PHP Code:
    <?php
    function createthumb($img) {
    $imgsize getimagesize($img);
    list(
    $width$height) = $imgsize;
    $imgbuffer imagecreatetruecolor(100100);
    $image imagecreatefromjpeg($img);
    imagecopyresampled($imgbuffer$image0000100100$width$height);
    $imgoutput imagepng($imgbuffer"thumbs/$img.png");
    imagedestroy($imgbuffer);
    return 
    $imgoutput;
    }

    $img getimagesize($_FILES['image']) ? $_FILES['image'] : false// modify this to suit where the image is coming from. getimagesize() checks if the image is a proper image
    if (!$img) {
      echo 
    'image is not valid';
    } else {
      
    $result createthumb($img);
      echo 
    $result === true 'success' 'failure';
    }
    ?>
    If there is a failure, there's something wrong with the image or the script, though of course the latter should not happen. So, your user uploads a file, you make sure it's a file, you create the thumbnail and put it in a subfolder called thumbs. The thumbnail has the same filename as the uploaded image except that it is in a subfolder and it has the .png extension attached. Naturally this script works only for uploaded JPEG images - if other types are going to uploaded, the script will have to provide for this with the addition of imagecreatefrompng(), imagecreatefromgif() etc.


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
  •