Blue Ridge Software LLC
  Encryption
Services
  Webmaster
Services
  Programming   Software   About Us

Program Snippets

  PHP - Generate a Thumbnail Image (Add To Favorites)
//Function to generate thumbnail
/* Function accepts these options:
$filename , e.g. "image.gif"
$max_width and $max_height of the resulting thumbnail
$image_dir_in --> Where the current large image is
$image_dir_out --> Where the thumbnail will be stored

*/
function make_thumb($filename,$max_width,$max_height,$image_dir_in,$image_dir_out){   
	$comp_name = $image_dir_in ."/$filename";
	$thumb_dir = $image_dir_out;
    if( !file_exists( $thumb_dir ."/$filename" )) { 
        if ( strstr ( $filename, '.gif' ) ) { 
            $im = imagecreatefromgif( $path . $comp_name); 
        } 
        elseif ( preg_match( '/(.jpg|.jpeg)/i', $comp_name )) 
        { 
            $im = imagecreatefromjpeg( $path . $comp_name); 
        } 
        elseif ( preg_match( '/.png/i', $comp_name )) 
        { 
            $im = imagecreatefrompng( $path . $comp_name);                     
        }         

        $white = imagecolorallocate($im, 255, 255, 255); 
        $black = imagecolorallocate($im, 0,0,0); 
        $y = 50 * imagesy($im) / 100; 
         
        $widths = imagesx($im); 
        $heights = imagesy($im); 

        if ( $widths > $max_width ) { 
            $img_width  = $max_width; 
            $img_height = ceil( ( $heights * ( ( $max_width * 100 ) / $widths ) ) / 100 ); 
            $heights = $img_height; 
            $widths  = $img_width; 
        } else  { 
            $img_height = $heights; 
        } 
         
        if ( $heights > $max_height )  { 
            $img_height  = $max_height; 
            $img_width   = ceil( ( $widths * ( ( $max_height * 100 ) / $heights ) ) / 100 ); 
        } else { 
            $img_width = $widths; 
        } 
         
        $newwidth = $img_width; 
        $newheight = $img_height; 
         
        $sourcedata = getimagesize ( $path . $comp_name ); 
         
        $image = imagecreatetruecolor($newwidth, $newheight); 
         
        imagecopyresampled($image,$im,0,0,0,0,$newwidth,$newheight,$sourcedata[0],$sourcedata[1]); 

        $a = imagejpeg($image, $thumb_dir ."/$filename"); 
         
    }
}
© 2013- Blue Ridge Software LLC