SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,315
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How should I manager thumbs created on the fly?

    In the following script (obtained from a PHP class some months ago) I can take a jpeg image and create a new, smaller, image from it and save the new version in a folder. This uses the PHP GD2 library.

    I have a question: How do I get rid of the new images when the visitor goes to another page - after all, they are going to start multiplying faster than rabbits? What happens when several visitors access the page and the image is constantly overwritten?

    PHP Code:
    <?php
    $image 
    file_get_contents("test.jpg"); // Get the .jpg image
    $source imagecreatefromstring($image); // Get the data about the image
    $src_w imagesx($source); // Get the width of the image
    $src_h imagesy($source); // Get the height of the image

    if( $src_h $src_w ){$dst_h 400// If the H is more than the W, the destination image's H will be 400px
    $dst_w = ($src_w/$src_h)*$dst_h// Resize the W in proportion.
    }
    else{
    $dst_w 400// If the H is less than the W, the destination image's W will be 400px
    $dst_h = ($src_h/$src_w)*$dst_w// Resize the H in proportion.
    }

    $thumb imagecreatetruecolor($dst_w$dst_h);
    imagecopyresampled($thumb,$source,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
    imagejpeg($thumb"newthumb2.jpg");
    echo
    "<img src='newthumb2.jpg' alt=''>";
    ?>
    Although this script names a particular image and destination image, a PHP script will call an image by name from a table and create a destination thumbnail using the image's name.

    Should I simply designate a folder to receive all the destination thumbs and not worry about it?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not check if a thumbnail has been created, if not, create the thumbnail. That way if one thumbnail is created it is permanent and doesn't need to be created again.

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,315
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    [slaps forehead] That's a great idea!


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
  •