Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    The images will be held in a directory, I want to produce a thumbnail of each of them to make them the same size.

    Clicking on them takes you to the full size version.

    Any tutorials someone can recommend?

  2.  

  3. #2
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    You need PHP's image extension to resize images. Once you have compiled it and/or enabled it in the PHP.ini file, you can resize either on the fly or when they are uploaded. I would recommend you save the thumbnail at upload time rather than each time a user requests the page as it is a CPU expensive process.

    The following is an upload script which makes 4 versions of an image of variaying quality and size and saves each one.
    PHP Code:
    <html>
        <head>
            <title>Upload Images</title>
        </head>
        <body>
            <h1>Please be patient - uploading images may take some time</h1>
    <?php
        ini_set
    ('max_execution_time''3600');

        require_once 
    'db.php';

        
    $db = & new SiteDB;

        
    //check for uploaded files
        
    if (! isset($_FILES['path'])) {
            die(
    'No input');
        }

        
    $file_array create_file_array($_FILES['path']);
        
        
    //for each file
        
    $file_count count($file_array);

        
    ob_start();

        for (
    $x 0$x $file_count$x++) {
            echo(
    "<p>Uploading: {$file_array[$x]['name']} ");
            
    ob_flush();
                
            if (
    $file_array[$x]['error'] != UPLOAD_ERR_OK) {
                echo(
    'upload error');
                continue;
            }

            
    // check this is an image
            
    if (! ($imginfo getimagesize($file_array[$x]['tmp_name']))) {
                echo(
    'unsupported image type');
                continue;
            } elseif (! (
    $imginfo['mime'] == 'image/jpeg')) {
                echo(
    'unsupported image type');
                continue;
            }
            
            if (
    $imginfo[0] > $imginfo[1]) {
                
    $largest $imginfo[0];
            } else {
                
    $largest $imginfo[1];
            }
            
            
    // get file info
            
    if ($_POST['name'][$x]) {
                
    $name add_slashes_safe($_POST['name'][$x]);
            } else {
                
    $name add_slashes_safe(substr($file_array[$x]['name'], 0strrpos($file_array[$x]['name'], '.')));
            }

            
    $type add_slashes_safe($imginfo['mime']);

            
    // create a query
            
    $query "INSERT INTO img_images (img_name, img_type) VALUES ('$name','$type')";
            
            if (! 
    $db->query($query)) {
                die(
    'Query error "' $db->error() .'"');
            }

            
    $id $db->insert_id();
            
    $new_file "img/img_$id.jpg";
        
            
    //copy the file to a permanent location using the image id as the file name
            
    if (! move_uploaded_file($file_array[$x]['tmp_name'], $new_file)) {
                
    $query "DELETE FROM img_images WHERE img_id=$id";
          
                if (! 
    $db->query($query)) {
                    die(
    'Query error "' $db->error() .'"');
                }
                
                echo(
    'error storing image');
                continue;  
            }        
            
            
    /* create a thumbnail and stor it in the DB */
            
    echo ('... creating thumbnail ');
            
    ob_flush();  
            
    $db_image addslashes(create_thumbnail(200200$new_file50));
              
            
    //update the image in the DB with the file and the thumbnail
            
    $query "UPDATE img_images SET img_path='$new_file',img_thumb='$db_image' WHERE img_id=$id LIMIT 1";

            if (! 
    $db->query($query)) {
                die(
    'Query error "' $db->error() .'"');
            }

            
    /* create a small image */
            
    if ($largest >= 500) {
                echo (
    '... creating small image ');
                
    ob_flush();  
                
    touch("{$new_file}_small.jpg");
                
    create_image(500,500,$new_file,55,"{$new_file}_small.jpg");
            }

            
    /* create a medium size image */
            
    if ($largest >= 800) {
                echo (
    '... creating medium image ');
                
    ob_flush();  
                
    touch("{$new_file}_medium.jpg");
                
    create_image(800,800,$new_file,55,"{$new_file}_medium.jpg");
            }

            
    /* create large image */
            
    if ($largest >= 1500) {
                echo (
    '... creating large image ');
                
    ob_flush();  
                
    touch("{$new_file}_large.jpg");
                
    create_image(1500,1500,$new_file,55,"{$new_file}_large.jpg");        
            }

            
    /* save the image of actual size */
            
    echo ('... creating actual size image ');
            
    ob_flush();  
            
    touch("{$new_file}_actual.jpg");
            
            
    $image imagecreatefromjpeg($new_file);
            
    imagejpeg($image"{$new_file}_actual.jpg" 55);        
            
            echo(
    ' - <b>success</b></p>');
            
    ob_flush();
        }

        function 
    add_slashes_safe($string) {
            if (! 
    get_magic_quotes_gpc()) {
                return 
    addslashes($string);
            } else {
                return 
    $string;
            }
        }
        
        function 
    create_image($max_width$max_height$filename$quality 100$new_file=null)
        {

            
    // Set a maximum height and width
            
    $width $max_height;
            
    $height $max_width;

            
    // Get new dimensions
            
    list($width_orig$height_orig) = getimagesize($filename);

            if (
    $width && ($width_orig $height_orig)) {
               
    $width = ($height $height_orig) * $width_orig;
            } else {
               
    $height = ($width $width_orig) * $height_orig;
            }

            
            
    // Resample
            
    $image_p imagecreatetruecolor($width$height);
            
    $image imagecreatefromjpeg($filename);
            
    imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

            
            
    // Output
            
    imagejpeg($image_p$new_file$quality);
        }
        
        function 
    create_thumbnail($max_width$max_height$filename$quality 100)
        {

            
    // Set a maximum height and width
            
    $width $max_height;
            
    $height $max_width;

            
    // Get new dimensions
            
    list($width_orig$height_orig) = getimagesize($filename);

            if (
    $width && ($width_orig $height_orig)) {
               
    $width = ($height $height_orig) * $width_orig;
            } else {
               
    $height = ($width $width_orig) * $height_orig;
            }

            
            
    // Resample
            
    $image_p imagecreatetruecolor($width$height);
            
    $image imagecreatefromjpeg($filename);
            
    imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

            
            
    // Output
            
            // start an output buffer
            
    ob_start();
            
            
    imagejpeg($image_pnull$quality);

            
    $img ob_get_contents();

            
    //clear buffer
            
    ob_end_clean();

            
    //return image
            
    return $img;
        }   
        
        function 
    create_file_array($filearray
        {
           
            foreach (
    $filearray as $key => $value) {
                
    $num count($value);

                for (
    $count 0$count $num$count++) {
                    
    $newarray[$count][$key] = $value[$count];
                }
            }
             
            return 
    $newarray;   
        } 

        
    // vim: expandtab softtabstop=4 tabstop=4 shiftwidth=4
    ?>
        <h2>Uploads complete - <a href="display_images.php">photos</a></h2>
        </body>
    </html>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:16 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com