Create thumbnail using php
One of most searches in php is how to create thumbnail images on the fly using PHP. Since this requires the GD library, you will need an installation of PHP with at least GD 2.0.1 enabled.
The code below creates a function named resizeImage that will get three parameters. The first parameter is correspondingly the path to the directory that contains original images. The second is the image extension The third and fourth parameter is the width and height you want for the thumbnail images.
From the below method you can create thumb of JPG, PNG,GIF.
function resizeImage($remoteImage,$ext,$maxwidth,$maxheight)
       {
       $imagepath=$remoteImage;
       $imagedata=getimagesize($imagepath);
       $imgwidth=$imagedata[0];
       $imgheight=$imagedata[1];
    Â
         $shrink=1;
        if($imgwidth > $maxwidth)
           {
               $shrink=$maxwidth/$imgwidth;
           }
    Â
        if($shrink!=1)
           {
             $output_height=$shrink * $imgheight;
             $output_width=$maxwidth;
           }else{
             $output_height=$imgheight;
             $output_width=$imgwidth;
           }
        if( $output_height > $maxheight )
           {
               $shrink = $maxheight / $output_height ;
               $output_width = $shrink * $output_width;
               $output_height = $maxheight;
           }
    Â
         switch($ext){
         case “.gif”:
           $src_image = @imagecreatefromgif($imagepath);
           $dest_image = @imagecreatetruecolor ($output_width, $output_height);
           imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);
           imagegif($dest_image, $imagepath, 100);
           break;
         case “.jpg”:
           $src_image = @imagecreatefromjpeg($imagepath);
           $dest_image = @imagecreatetruecolor ($output_width, $output_height);
           imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);
           imagejpeg($dest_image, $imagepath, 100);
           break;
         case “.jpeg”:
           $src_image = @imagecreatefromjpeg($imagepath);
           $dest_image = @imagecreatetruecolor ($output_width, $output_height);
           imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);
           imagejpeg($dest_image, $imagepath, 100);
           break;
         case “.png”:
           $src_image = @imagecreatefrompng($imagepath);
           $dest_image = @imagecreatetruecolor ($output_width, $output_height);
           imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $output_width, $output_height, $imgwidth, $imgheight);
           imagepng($dest_image, $imagepath, 100);
           break;
         }
    Â
       }
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader .

[...] On the cakeForge snippets section there are a lot of scripts for creating thumbnails of images. Blognol.com has written a post about just that thing. If you don’t like any of the CakeForge snippets, head over to create thumbnails using cakePHP. [...]