www.webdeveloper.com
Results 1 to 3 of 3

Thread: Auto generating links?

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    27

    Auto generating links?

    hi. I'm setting up a sort of photo gallery, and what I want is for the script to pull the images from a folder, make thumbnails from them, and automatically generate links so when i click the thumbnails, they'll appear larger in a box above the thumbs. Preferably they'd be loading off of the server as they were requested. Is this even possible? If it is, I'd love to be directed to some helpful resources because I'm new to this and right now I'm just working off of assumptions. Tutorials would be amazingly helpful.

    Thanks for any help.

  2. #2
    Join Date
    Feb 2007
    Posts
    19

    Creating thumbnails

    Hi

    Actually u need two PHP files for this.

    1. thumbnail.php
    2. To execute ur need.
    PHP Code:
    //To create thubnail
    <?php
    /*
      Tye (tye at tye . ca)
    */
    function create_thumbnail($infile,$outfile,$maxw,$maxh,$stretch FALSE) {
        
    #clearstatcache();
        
    if (!is_file($infile)) {
            
    trigger_error("Cannot open file: $infile",E_USER_WARNING);
            return 
    FALSE;
        }
        if (
    is_file($outfile)) {
            
    //trigger_error("Output file already exists: $outfile",E_USER_WARNING);
            
    return FALSE;
        }

        
    $functions = array(
            
    'image/png' => 'ImageCreateFromPng',
            
    'image/jpeg' => 'ImageCreateFromJpeg',
        );

        
    // Add GIF support if GD was compiled with it
        
    if (function_exists('ImageCreateFromGif')) { $functions['image/gif'] = 'ImageCreateFromGif'; }

        
    $size getimagesize($infile);

        
    // Check if mime type is listed above
        
    if (!$function $functions[$size['mime']]) {
            
    trigger_error("MIME Type unsupported: {$size['mime']}",E_USER_WARNING);
            return 
    FALSE;
        }

        
    // Open source image
        
    if (!$source_img $function($infile)) {
            
    trigger_error("Unable to open source file: $infile",E_USER_WARNING);
            return 
    FALSE;
        }

        
    $save_function "image" strtolower(substr(strrchr($size['mime'],'/'),1));

        
    // Scale dimensions
        
    list($neww,$newh) = scale_dimensions($size[0],$size[1],$maxw,$maxh,$stretch);
        
        
    // Create new image
        
    $new_img imagecreatetruecolor($neww,$newh);

        
    // Copy and resize image
        
    imagecopyresized($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],$size[1]);

        
    // Save output file
        
    if ($save_function == 'imagejpeg') {
            
    // Change the JPEG quality here
            
    if (!$save_function($new_img,$outfile,75)) {
                
    trigger_error("Unable to save output image",E_USER_WARNING);
                return 
    FALSE;
            }
        } else {
            if (!
    $save_function($new_img,$outfile)) {
                
    trigger_error("Unable to save output image",E_USER_WARNING);
                return 
    FALSE;
            }
        }

        
    chmod($infile,0777);
        
    // Cleanup
        
    imagedestroy($source_img);
        
    imagedestroy($new_img);

        return 
    TRUE;
    }
    // Scales dimensions
    function scale_dimensions($w,$h,$maxw,$maxh,$stretch FALSE) {
        if ((!
    $stretch) && (($w $maxw) || (!$maxw)) &&
            ((
    $h $maxh) || (!$maxh))) return array($w,$h);
        
    // Scale Height
        
    if ((!$maxw) || (($h $w) && ($maxh)) ) {
            
    $newh $maxh;
            
    $neww floor($w $newh /$h);
        }
        
    // Scale width
        
    elseif ((!$maxh) || (($w >= $h) && ($maxw))) {
            
    $neww $maxw;
            
    $newh floor($h $neww $w);
        } else
        
    // Scale neither
            
    return array($w,$h);
        return array(
    $neww,$newh);
    }
    ?>


    //To open the foldre and retrieve the images,converting to thumbnail and providing link



    <?php
    include("thumbnail.php");
    if (
    $handle opendir('.')) {
       
    $i 1;
        while (
    false !== ($file readdir($handle))) {
            
    //if ($file != "." && $file != "..") {
                
    if(substr($file,-3)=='jpg')
                   {
                     echo 
    "file" $file;
                     
    $time=time();
                     
    $infile $file;
                     
    $outfile$time $i .".jpg";
                     
    create_thumbnail($infile,$outfile,60,60,$stretch=FALSE);
                     echo 
    "<a href=$infile><img src=$outfile border=0></a>" "<br>"
                     
    $i=$i+1;
                   }
               
    //}
        
    }
        
    closedir($handle);
    }
    ?>

    If there is some errror reply me.
    Last edited by Subbulakshmi; 02-27-2007 at 10:36 PM.

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Have a look at my Simple Image Gallery and if it looks like it could be helpful to your cause you can view the code on My Site.
    Last edited by bokeh; 02-27-2007 at 07:41 AM.

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