Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thumbnail from image in db

    i was looking for some php code that resizes images effectively creating a thumbnail without resaving and creating a new file. i found this but the problem is that it apparently only works with local urls linking to images files. my images are stored in a mysql database and are retrieved with another php file called "getdata.php?id=x". this php file does work and i can view the original image fine. when i link to http:/mysite.com/pathto/thumbnail.php?url=getdata.php?id=x it gives me this error:

    Warning: getimagesize(getdata.php?id=2) [function.getimagesize]: failed to open stream: No such file or directory in /home/public_html/path/thumbnail.php on line 7

    this is code for thumbnail.php:

    PHP Code:
        // Get image info from variable
        
    $url $_GET['url'];
        
    $img getimagesize($url) or die();//line 7
        
        // Checks if URL is image
        
    if ( $img[2] == ) {
            
    $pic imagecreatefromgif$url ) or die();
        } elseif ( 
    $img[2] == ) {
            
    $pic imagecreatefromjpeg$url ) or die();
        } elseif ( 
    $img[2] ==) {
            
    $pic imagecreatefrompng$url ) or die();
        } else {exit();}
        
        
        
    // If an image is found and we can determine that it is an image
        
    if ($pic) {
            
    // Get width and height from the image
            
    $width imagesx($pic);
            
    $height imagesy($pic);
            
    $theight 100;
            
            
    // Calculate the new width
            
    $twidth = ($theight $width) / $height;
        
            
    // Create new image
            
    $thumb = @imagecreatetruecolor $twidth$theight )
            or die (
    "Can't create Image!");
        
            
    // Resize the image into a thumb
            
    imagecopyresized($thumb$pic0000,
            
    $twidth$theight$width$height);
        
            
    // Change page type to a jpeg
            
    header ("Content-type: image/jpeg");
        
            
    // Create jpeg image from thumbnail
            
    imagejpeg($thumb,"",75); 
    Last edited by utinaeniduin; 09-03-2007 at 01:01 AM. Reason: url abbreviations messed it up

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    If you upload your images and have control over them then this PHP script might be useful. It generates images from the embedded thumbnails.

    PHP Code:
    <?php
    $file 
    "test.jpg";

        
    $image exif_thumbnail($file$width$height$type);

    if (
    $image!==false) {
        
    header('Content-type: ' .image_type_to_mime_type($type));
        echo 
    $image;
        exit;
    } else {
        
    // no thumbnail available, handle the error here
        
    echo 'No thumbnail available';
    }
    ?>
    Leonard Whistler


  •  

    Posting Permissions

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