www.webdeveloper.com
Results 1 to 12 of 12

Thread: Thumbail generator query

  1. #1
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654

    [Resolved]Thumbail generator query

    I'm wanting to use the script below found on http://www.icant.co.uk/articles/phpthumbnails/ but I'm just wondering how it would be called. Would I use
    PHP Code:
    include( "thumbnail_generator.pgp" ); 
    in my image upload script?
    Thanks

    PHP Code:
    <?php 
    $imagefolder
    ='./img/';
    $thumbsfolder='./thumb_img/';
    $pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
    $pics=ditchtn($pics,"tn_");
    if (
    $pics[0]!="")
    {
        foreach (
    $pics as $p)
        {
            
    createthumb($p,"tn_".$p,150,150);
        }
    }

    /*
        Function ditchtn($arr,$thumbname)
        filters out thumbnails
    */
    function ditchtn($arr,$thumbname)
    {
        foreach (
    $arr as $item)
        {
            if (!
    preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
        }
        return 
    $tmparr;
    }

    /*
        Function createthumb($name,$filename,$new_w,$new_h)
        creates a resized image
        variables:
        $name        Original filename
        $filename    Filename of the resized image
        $new_w        width of resized image
        $new_h        height of resized image
    */    
    function createthumb($name,$filename,$new_w,$new_h)
    {
        
    $system=explode(".",$name);
        if (
    preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
        if (
    preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
        
    $old_x=imageSX($src_img);
        
    $old_y=imageSY($src_img);
        if (
    $old_x $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$old_y*($new_h/$old_x);
        }
        if (
    $old_x $old_y
        {
            
    $thumb_w=$old_x*($new_w/$old_y);
            
    $thumb_h=$new_h;
        }
        if (
    $old_x == $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$new_h;
        }
        
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
        
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
        if (
    preg_match("/png/",$system[1]))
        {
            
    imagepng($dst_img,$filename); 
        } else {
            
    imagejpeg($dst_img,$filename); 
        }
        
    imagedestroy($dst_img); 
        
    imagedestroy($src_img); 
    }

    /*
            Function directory($directory,$filters)
            reads the content of $directory, takes the files that apply to $filter 
            and returns an array of the filenames.
            You can specify which files to read, for example
            $files = directory(".","jpg,gif");
                    gets all jpg and gif files in this directory.
            $files = directory(".","all");
                    gets all files.
    */
    function directory($dir,$filters)
    {
        
    $handle=opendir($dir);
        
    $files=array();
        if (
    $filters == "all"){while(($file readdir($handle))!==false){$files[] = $file;}}
        if (
    $filters != "all")
        {
            
    $filters=explode(",",$filters);
            while ((
    $file readdir($handle))!==false)
            {
                for (
    $f=0;$f<sizeof($filters);$f++):
                    
    $system=explode(".",$file);
                    if (
    $system[1] == $filters[$f]){$files[] = $file;}
                endfor;
            }
        }
        
    closedir($handle);
        return 
    $files;
    }
    ?>
    Last edited by Dopple; 03-27-2006 at 08:11 AM. Reason: Resolved

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    What do you want it to do? Looking at the file, if you want to use it as an include it needs to be modified and the static nature becomes dynamic.

  3. #3
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Really what I'm after is for it to take any image after uploading and resize it. I found your example on this thread but couldn't work out how to get that working either (no offence, I'm still generally new to the PHP game). If you think your one there would be better, let me know and I'll post my question on that thread if you'd be so good as to explain it a bit for me? (just so that it's on the appropriate thread)
    Thanks

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Let's continue here now you have started, rather than drag an old thread out of the archive. Using that function may not be the best thing for what you want. Because it produces a scaled down version of the source image the images can be all different sizes and shapes and this might not be a good thing for your html/css layout. This is demonstrate well by this example which used a function like that to build the thumbnails. I recently wrote a function to get round that problem and it was used to create the thumbnails at this page which are all exactly the same size. This allowed a completely liquid layout of the thumbnails and captions using only CSS and no tables. As you will see, when the page is resized the number of columns changes to fit the page width, something completely impossible with tables.

    I can help you produce either of these styles or something different but you need to be very specific about your needs.

  5. #5
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    It would be the second style that I would be after. What I have is one folder (thumb_img) for the thumbnails and one (img) for the actual images. What I would like, and I'm not sure if this is the most logical and space friendly way, is to have the user upload the image to the folder "img" which also inserts the details (name, caption, filename) of the upload into a mysql database (I have this part working already) and during the upload I would like the thumb to be resized to roughly the size that the thumbs are on the second link you posted and then saved to the "thumb_img" folder. The thumbs would be the same name as the img so that I could use the mysql to display the contents of both folders. To display I would just output them in one long line, allowing css to space them. I should be ok to get the output working on my own. I already have the page displaying the full sized images.

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    PHP Code:
    <?php

    function resizeSemiAbstractTop($source$destination NULL$w 120$h 90)
    {
        
    $source_image = @file_get_contents($source)
            or die(
    'Could not open'.$source);
        
    $source_image = @imagecreatefromstring($source_image)
            or die(
    $source.' is not a valid image');
        
    $sw imagesx($source_image);
        
    $sh imagesy($source_image);
        
    $ar $sw/$sh;
        
    $tar $w/$h;
        if(
    $ar >= $tar)
        {
            
    $x1 round(($sw - ($sw * ($tar/$ar)))/2);
            
    $x2 round($sw * ($tar/$ar));
            
    $y1 0;
            
    $y2 $sh;
        }
        else
        {
            
    $x1 0;
            
    $y1 0;
            
    $x2 $sw;
            
    $y2 round($sw/$tar);
        }
        
    $slate = @imagecreatetruecolor($w$h) or die('Invalid thumbnail dimmensions');
        
    imagecopyresampled($slate$source_image00$x1$y1$w$h$x2$y2);
        if(!
    $destinationheader('Content-type: image/jpeg');
        @
    imagejpeg($slate$destination100) or die('Directory permission problem');
        
    ImageDestroy($slate);
        
    ImageDestroy($source_image);
        if(!
    $destination) exit;
        return 
    true;
    }

    ?>
    Put that in a file and require() when you want to use the function. If you can't figure out how to use it consult the examples in post #6 of this thread. It you still can't figure it out come back and I will write something specific for you.

  7. #7
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Gotcha. Well the weekends just started and I fancy a cold one. Going to see V for Vendetta tonight. I'll try that out on Sunday and if I have any problems I'll let you know.
    Much appreciated Bokeh.

  8. #8
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Ok I'm calling the function like so. $imgname is just the file name. I am getting the directory permission error though. Both folders, img and thumb_img have 755 permissions, which is fine for just uploading the image. Should the thumbnail directory be 777? Also does it look like I'm declaring the variables correctly?
    PHP Code:
    # Image location... (Can be HTTP or file path) 
    $image_location '../img/' $imgname

    # Destination file... (Server file path, not HTTP) 
    $save_abstraction_to '../thumb_img/'

    # open the file containing the function 
    require_once($_SERVER['DOCUMENT_ROOT'] . '/admin/thumbnail_generator.php'); 

    # output to file 
    resizeSemiAbstractTop($image_location$save_abstraction_to); 

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by Dopple
    PHP Code:
    # Destination file... (Server file path, not HTTP) 
    $save_abstraction_to '../thumb_img/'
    The above is just a path. No file name! Try:
    PHP Code:
    # Destination file... (Server file path, not HTTP) 
    $save_abstraction_to '../thumb_img/'.$imgname
    That will give the thumbnail the same name as its parent image.

  10. #10
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Thanks. that's got it working now.
    Much appreciated!!!

  11. #11
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

  12. #12
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Yes that's exactly what I was after thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles