php 生成缩略图

一个利用php函数,生成简单缩略图的例子
<?php
/**
*生成缩略图
**/
function ResizeImage($im,$maxwidth,$maxheight,$name){  
         //取得当前图片大小 
         $width = imagesx($im);  
         $height = imagesy($im);  
         //生成缩略图的大小 
         if(($width > $maxwidth) || ($height > $maxheight)){  
             $widthratio = $maxwidth/$width;      
            $heightratio = $maxheight/$height;   
             if($widthratio < $heightratio){  
                 $ratio = $widthratio;  
             }else{  
                 $ratio = $heightratio;  
             }  
             $newwidth = $width * $ratio;  
             $newheight = $height * $ratio;  
 
             $newim = imagecreatetruecolor($newwidth, $newheight);  
             imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);   
             $b = imagejpeg($newim,$name.".jpg");  
            // echo $b?'true':'false';
             imagedestroy($newim);  
         }else{  
             imagejpeg($im,$name.".jpg");  
         }  
}
$im = imagecreatefromjpeg("abc.jpg");
ResizeImage($im,50,50,'abc1');
//imagecreatetruecolor函数不能生成gif图片
?>

你可能感兴趣的:(PHP)