WEB PHP Guys... creating a thumbnail from an image

Discussion in 'OT Technology' started by brds, Sep 10, 2008.

  1. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    I have a site that has about 60 images (png, jpeg, jpg, and gif files)... i want to display the top 5 viewed, 5 newest images, and 5 random images on the homepage. Only problem is is that some images are like 3 megs... which causes the homepage to take too long to load.

    To fix the problem, i'm trying to create a script that creates thumbnails. I will enter the url of each image into a textarea. upon submission, the urls will be exploded into an array. A for loop for then go through the array, pick out the extension and then, based on the extension, perform the correct image conversions...

    Only problem is is that i dont know exactly what to do to convert the image, resize it to a max length or width of 100, and save it into a new directory.

    If somebody could help me out, it'd be :bowdown:

    Here's what i got so far:
    Code:
    <?php
    	if (!isset($_POST['makeimages']))
    	{
    		echo "
    			<form action='test.php' method='POST'>
    				<textarea name='urls' cols='100' rows='10'></textarea>
    				<input type='hidden' name='makeimages' value='1' />
    				<br />
    				<input type='submit' value='make thumbnails' />
    			</from>
    		";
    	}
    	else 
    	{
    		$images = array();
    		$images = explode("\n", $_POST['urls']);
    		
    		for ($i = 0; $i < count($images); $i++)
    		{
    			$dimensions = getimagesize($images[$i]);
    			$length = $dimensions[0];
    			$width = $dimensions[1];
    			
    			if ($length > 100 || $width > 100)
    			{
    				$ending = substr($images[$i], -3);
    				
    				// Make thumbnail for jpeg or jpg
    				if (($ending == "jpg") || ($ending = "peg"))
    				{
    					$img = imagecreatefromjpeg($images[$i]);
    					$imagestate = getimagesize($img);
    					$imgl = $imagestate[0];
    					$imgh = $imagestate[1];
    				}
    				// Make thumbnail for gif
    				elseif ($ending == "gif")
    				{
    					
    				}
    				// make thumbnail for png
    				elseif ($ending == "png")
    				{
    					
    				}
    			}
    		}
    	}
    ?>
    
     
  2. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    You could google php gd to learn about resizing.

    If you wanted to make it even easier, you could use something like phpthumb.
    http://phpthumb.sourceforge.net/
    With phpthumb all you do is make a url string something like
    Code:
    http://yoursite.com/phpthumb.php?src=http://whatever.com/file.jpg&width=100
    
    When stuck in a link or image tag, that will output the image in the 2nd half of the url resampled at the width/height you specify in the url arguments. As long as you get caching turned on with phpthumb it works pretty well. The Demo page on their site doesn't have the best quality images in all cases, but in your case you'd be downsizing, so as long as you don't crank the compression way up it will look fine. I've used this on several projects to make thumbnails and they look great, it allows us to easily change thumbnail sizes when the design changes.

    It takes care of resampling the height above to the proper aspect ratio. If you have an image that's height is greater than the width, you'd need to do the math with getimagesize beforehand to figure out what to set the width to above so that height stays under 100, or just change the last argument above to height=100
     
    Last edited: Sep 10, 2008
  3. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
  4. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
  5. Drowned

    Drowned NameThatPornstar.com

    Joined:
    Mar 26, 2001
    Messages:
    8,534
    Likes Received:
    2
    For jbgallery I do something similar. I upload all new images to a dir, and call a php that processes all the new images:

    Here is the function I use to create the thumbnails (checks for file extension ;) )

    Code:
    function file_extension($filename) { return end(explode(".", $filename)); }
    
    function create_thumbnail($source_file, $destination_file, $max_dimension)
    {
        list($img_width,$img_height) = getimagesize($source_file); // Get the original dimentions
        $aspect_ratio = $img_width / $img_height;
        
        if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) ) // If either dimension is too big...
        {
            if ( $img_width > $img_height ) // For wide images...
            {
                $new_width = $max_dimension;
                $new_height = $new_width / $aspect_ratio;
            }
            elseif ( $img_width < $img_height ) // For tall images...
            {
                $new_height = $max_dimension;
                $new_width = $new_height * $aspect_ratio;
            }
            elseif ( $img_width == $img_height ) // For square images...
            {
                $new_width = $max_dimension;
                $new_height = $max_dimension;
            }
            else { echo "Error reading image size."; return FALSE; }
        }
        else { $new_width = $img_width; $new_height = $img_height; } // If it's already smaller, don't change the size.
        
        // Make sure these are integers.
        $new_width = intval($new_width);
        $new_height = intval($new_height);
        
        $thumbnail = imagecreatetruecolor($new_width,$new_height); // Creates a new image in memory.
        // The following block retrieves the source file.  It assumes the filename extensions match the file's format.
        if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
        if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) )
        { $img_source = imagecreatefromjpeg($source_file); }
        if ( strpos($source_file,".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
        if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
        // Here we resample and create the new jpeg.
        imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
        imagejpeg($thumbnail, $destination_file, 100);
        // Finally, we destroy the two images in memory.
        imagedestroy($img_source);
        imagedestroy($thumbnail);
    } 
    
    To use the function you just specify the original file, destination folder and max width/height.

    Code:
    create_thumbnail($file, "thumbs/" . $file, 100);
    So now that I have the function I just loop through the directory with:

    Code:
    $path = "/your/path/to/large/images";
    $dh = opendir($path);
    
    while (($file = readdir($dh)) !== false) {
    
    <-- thumbnail code goes here -->
    
    }
    closedir($dh);
    
    In my loop I check that the filename doesn't exist in the database, create the thumbnails I need and add the watermark to the large images.

    Hope that helps :bigthumb:
     
  6. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
  7. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    :h5: i'll play around with that... thanks!

    EDIT: I'm getting a funky error for each picture...

     
    Last edited: Sep 10, 2008
  8. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    The rest of it:

     
  9. Drowned

    Drowned NameThatPornstar.com

    Joined:
    Mar 26, 2001
    Messages:
    8,534
    Likes Received:
    2
    Same first error for all the images:

    failed to open stream: No such file or directory in /home/MYUSERNAME/public_html/v2/library.php on line 6

    It seems it can't find any of the images or doesn't have permission to access them :dunno:
     
  10. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,578
    Likes Received:
    3
    Im assuming you changed MYUSERNAME to your actual username/path on the system.. and that was edited for forum posting?
     
  11. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    rgr
     

Share This Page