PHP图片操作类

1,生成缩图

 

代码
<?php
///生成縮圖
//
/$resizeimage=new resizeimage($filesaved,$dstimg,250,250);
class resizeimage 

    
var $type
    
var $width
    
var $height
    
var $resize_width
    
var $resize_height
    
var $srcimg
    
var $dstimg
    
var $im;
    
var $desiredRatio;

    
function resizeimage($srcimg,$dstimg, $wid, $hei
    { 
        
$this->srcimg = $srcimg
        
$this->dstimg = $dstimg
        
//圖片的類型 
        $this->type = substr(strrchr($this->srcimg,"."),1); 
        
//初始化圖像 
        if($this->type=="jpg"
            
$this->im = imagecreatefromjpeg($this->srcimg); 
        
if($this->type=="gif"
            
$this->im = imagecreatefromgif($this->srcimg); 
        
if($this->type=="png"
            
$this->im = imagecreatefrompng($this->srcimg);
            
        
//目標圖像寬和高
        $this->width = imagesx($this->im); 
        
$this->height = imagesy($this->im);
        
//計算縮圖的寬和高
        //若需要生產的縮圖的寬和高都大於源圖的寬高時都不需要縮小

        if( ($this->width<=$wid&& ($this->height<=$hei) ){
            
$this->resize_width=$this->width;
            
$this->resize_height=$this->height;
        }
else{
            
//若寬的縮小比例小於高的縮小比例時,則成生的縮圖的寬為$wid
            if( ($wid/$this->width) < ($hei/$this->height) ){
                
$this->desiredRatio=$wid/$this->width;
                
$this->resize_width=$wid;
                
$this->resize_height=(int)($this->height*$this->desiredRatio);
            
//若寬的縮小比例大於高的縮小比例時,則生成的縮圖的高為$hei
            }else{
                
$this->desiredRatio=$hei/$this->height;
                
$this->resize_height=$hei;
                
$this->resize_width=(int)($this->width*$this->desiredRatio);
            }
        }
        
//生成圖像
        $this->newimg(); 
        
//销毁一图像
        ImageDestroy ($this->im); 
    } 
    
function newimg() 
    { 
        
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
        imagecopyresampled(
$newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->height) ; 
        ImageJpeg (
$newimg,$this->dstimg); 
    }    

?>

 

 

posted @ 2010-07-27 16:47  Athrun  阅读(382)  评论(0编辑  收藏  举报