Results 1 to 4 of 4

Thread: how to create multiple thumbnails from one image

  1. #1
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to create multiple thumbnails from one image

    hi all

    this is my script which creates one thumbnail from uploaded image

    But now instead of one thumbnail

    i want to generate 3 different size thumbnails

    200x200
    500x500
    800x800

    how to generate them ??

    Code:
    <?php chmod('images/', 0777);
    require_once("functions.php");  
    require_once("path.php");  
    
    if(isset($_FILES['prod_image'])) 
    {           
    	if(preg_match('/[.](jpg)|(pjpeg)|(gif)|(png)$/', 
    
    $_FILES['prod_image']['name']))
    	{ 
    		$path=$_FILES['prod_image']['name'];
    	    $filename = $_FILES['prod_image']['name'];
            $source = $_FILES['prod_image']['tmp_name'];   
    		$target = $path_to_image_directory . $filename;   
            move_uploaded_file($source, $target);   
            createThumbnail($filename); 
    	}
    }  
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form action="" method="post" enctype="multipart/form-data" name="form1" 
    
    id="form1">
    <input name="prod_image" type="file" id="prod_image" size="60" /> <br /><br 
    
    />   
    <input type="submit" value="upload" name="submit" />
    </form>
    </body>
    </html>
    this is functions.php
    Code:
    <?
    function createThumbnail($filename) {   
           
       	require 'path.php';   
    	 
           
        if(preg_match('/[.](jpg)$/', $filename)) {   
            $im = imagecreatefromjpeg($path_to_image_directory . $filename);   
        } else if (preg_match('/[.](gif)$/', $filename)) {   
            $im = imagecreatefromgif($path_to_image_directory . $filename);   
        } else if (preg_match('/[.](png)$/', $filename)) {   
            $im = imagecreatefrompng($path_to_image_directory . $filename);   
        }   
           
        $ox = imagesx($im);   
        $oy = imagesy($im);   
           
        $nx = $final_width_of_image;   
        $ny = floor($oy * ($final_width_of_image / $ox));   
           
        $nm = imagecreatetruecolor($nx, $ny);   
           
    
    	imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);     
           
        if(!file_exists($path_to_thumbs_directory)) {   
          if(!mkdir($path_to_thumbs_directory)) {   
               die("There was a problem. Please try again!");   
          }    
           }   
      
        imagejpeg($nm, $path_to_thumbs_directory . $filename,70);   
    	
    	$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" 
    
    alt="image" />';   
    $tn .= '<br />Congratulations. Your file has been successfully uploaded, and 
    
    a thumbnail has been created.'; 
    echo "<p align=center>". $tn  . "</p>";
    }  
    ?>
    this is path.php
    Code:
    <?
    $final_width_of_image = 200;   
    $path_to_image_directory = 'images/';   
    $path_to_thumbs_directory = 'images/thumbs/';  
    ?>

  2. #2
    Join Date
    Sep 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Working on my side.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, I think I would setup a way to get a new filename, to distinguish each of the thumbnails, then use arrays and foreach:

    functions.php :

    PHP Code:
    <?php
    function createThumbnail($filename) {   
           
           require 
    'path.php';   
         
           
        if(
    preg_match('/[.](jpg)$/'$filename)) {   
            
    $im imagecreatefromjpeg($path_to_image_directory $filename);   
        } else if (
    preg_match('/[.](gif)$/'$filename)) {   
            
    $im imagecreatefromgif($path_to_image_directory $filename);   
        } else if (
    preg_match('/[.](png)$/'$filename)) {   
            
    $im imagecreatefrompng($path_to_image_directory $filename);   
        }   
           
        
    $ox imagesx($im);   
        
    $oy imagesy($im);   
        
    $tn '';
    foreach (
    $final_width_of_image as $nx){
        
    $ny floor($oy * ($nx $ox));   
           
        
    $nm imagecreatetruecolor($nx$ny);   
           

        
    imagecopyresampled($nm$im0,0,0,0,$nx,$ny,$ox,$oy);     
           
        if(!
    file_exists($path_to_thumbs_directory)) {   
          if(!
    mkdir($path_to_thumbs_directory)) {   
               die(
    "There was a problem. Please try again!");   
          }    
           }   
      
        
    imagejpeg($nm$path_to_thumbs_directory $nx 'x' $ny $filename,70);   
        
        
    $tn[] = '<img src="' $path_to_thumbs_directory $nx 'x' $ny $filename '" 

    alt="image" />'
    ;   
    }
    $tn implode('<br />'$tn);
    $tn .= '<br />Congratulations. Your file has been successfully uploaded, and 
    3 thumbnails have been created.'

    echo 
    "<p align=center>"$tn  "</p>";
    }  
    ?>
    path.php:

    PHP Code:
    <?php //path
    $final_width_of_image = Array(200500800);
    $path_to_image_directory 'images/';   
    $path_to_thumbs_directory 'images/thumbs/';  
    ?>
    I'd be tempted to be a bit more precise about the output file's name and (especially) extension and type, but that was inherent in the original code, and appears to work. Also rather than have to remember all the weird filenames, I'd make individual folders in the thumbnail directory by the widths, that way the filename could be preserved throughout, only the path would vary (so the 200px wide thumb would be in thumbs/200/ and so on for the other sizes. Taking that all together, I'd do functions.php like so:

    PHP Code:
    <?php
    function createThumbnail($filename) {
        require 
    'path.php';
        
    $type exif_imagetype($filename); // get image type as integer
        
    if ($type) {
            
    $type image_type_to_mime_type($type); // get mime type as text, ex: image/jpeg
            
    $type2 explode('/'$type); // with next line, get just the text of image type
            
    $type2 $type2[1]; // ex: jpeg
            
    if(!preg_match("/^(jpeg)|(gif)|(png)$/"$type2)){die('unsupported image type');}
            
    $im call_user_func("imagecreatefrom$type2"$filename); // use image type text to create its standard function to create image object
        
    } else {die('cannot read filetype');}
        
    $bname basename($filename);
        
    $filename preg_replace("/\.[^\.]*$/"''$bname);
        
    $ext str_replace($filename''$bname);
        
    $ox imagesx($im);
        
    $oy imagesy($im);
        if(!
    file_exists($path_to_image_directory)) {
            if(!
    mkdir($path_to_image_directory)) {
                die(
    "There was a problem. Please try again!");
            }
        }
        if(!
    file_exists($path_to_thumbs_directory)) {
            if(!
    mkdir($path_to_thumbs_directory)) {
                die(
    "There was a problem. Please try again!");
            }
        }
        foreach (
    $final_width_of_image as $nx){
            
    $ny floor($oy * ($nx $ox));
            
    $nm imagecreatetruecolor($nx$ny);
            
    imagecopyresampled($nm$im0,0,0,0,$nx,$ny,$ox,$oy);
            if(!
    file_exists($path_to_thumbs_directory $nx '/')) {
                if(!
    mkdir($path_to_thumbs_directory $nx '/')) {
                    die(
    "There was a problem. Please try again!");
                }
            }
            
    $args = Array($nm$path_to_thumbs_directory $nx '/' $filename $ext);
            if(
    $type2 === 'jpeg'){$args[] = 70;} // image quality 0 worse 100 best
            
    else if($type2 === 'png'){$args[] = 9;} // compression 0 least 9 most
            
    call_user_func_array("image$type2"$args);
            
    imagedestroy($nm);
            
    $tn[] = '<img style="vertical-align: top;" src="' $path_to_thumbs_directory $nx '/' $filename $ext '" alt="image" />';
        }
        
    imagedestroy($im);
        
    $tn implode(' '$tn);
        
    $tn 'Congratulations. Your file has been successfully uploaded, and 3 thumbnails have been created:<br />' $tn;
        echo 
    "<p align=center>"$tn  "</p>";
    }
    ?>
    Also adds imagedestroy to save memory, makes sure the images folder is there, and can accept an image (path and filename) from anywhere on the server. One thing I'm not clear on though is - do you want all the thumbnails to be jpg regardless of their original format? My latest code (last code block) preserves the original format and extension, the code you had (and my first functions.php code) made only jpeg images but also preserved the original extension, which would result in .png and .gif extensions for jpeg format images (could be a problem). If you do want all jpeg format thumbnails, we could get those with the correct .jpg extension and pass the thumbnail filename(s) along as needed with the new .jpg extension as applicable. Something to think about.
    Last edited by jscheuer1; 10-05-2016 at 01:17 AM. Reason: add improved functions code, later - improve that
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks john for the detailed solution

    vineet

Similar Threads

  1. Using thumbnails for playing multiple YouTube videos
    By molendijk in forum Submit a DHTML or CSS code
    Replies: 4
    Last Post: 01-15-2015, 02:58 PM
  2. Replies: 13
    Last Post: 10-16-2012, 02:31 AM
  3. Create thumbnails for images
    By Sliight in forum Graphics
    Replies: 8
    Last Post: 04-20-2010, 12:11 PM
  4. Multiple Divs, one set of Thumbnails
    By gkornbluth in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 04-01-2010, 08:26 PM
  5. Replies: 0
    Last Post: 04-08-2009, 07:23 PM

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
  •