php实现图片任意比例缩放

在php项目中,或多或少的会碰到 处理图片的问题. 为了实现对图片的批量自动处理,用php写了一个类,经过测试,可以对图片进行指定尺寸的缩放处理.
需要注意的:
1 需要安装php-gd库 .
2 需要确保 save_to 的目录具有写权限

类程序如下:

class Picture
{
    private $_image_type;
    private $_image_width;
    private $_image_height;
    private $_filename;
    private $_save_to;
    /**
     * Init params of this picture
     * param  $file filename include path like(/home/test/test.jpg)
     * param  $file_suffix file_size  
     * param  $swidth
     * param  $sheight 
     * param  $save_to save_file_to  (be sure to have operation)
     * param 
     * return bool
     */
public function __construct( $file , $file_suffix , $new_width ,$new_height , $save_to="./")
    {
        if( !file_exists($file) )
              return false;

        $this->_filename = $file;
        $this->_save_to = $save_to;       
        $this->_changeimagesize( $new_width , $new_height , $file_suffix );
    }
 private function _changeimagesize( $new_width , $new_height , $file_suffix )
    {
          
        list( $width , $height , $this->_image_type ) = getimagesize( $this->_filename );
        $image_handler = imagecreatetruecolor( $new_width , $new_height );
          
        //1=gif , 2=jpg , 3=png
        if( $this->_image_type == 2 )
        {
            $image = imagecreatefromjpeg( $this->_filename );
            $type='jpeg';
        }else if ( $this->_image_type == 1 )
        {
            $image = imagecreatefromgif( $this->_filename );
            $type='gif';
        }else if ( $this->_image_type == 3 )
        {
            $image = imagecreatefrompng( $this->_filename );
            $type='png';
        }else
        {
            return false;
        }
       
        if( !headers_sent() ) header('Content-type:image/'.$type);
          
        imagecopyresampled( $image_handler , $image , 0 , 0 ,0 , 0 ,$new_width , $new_height , $width , $height );
 if( $this->_image_type == 2 )
        {
            imagejpeg( $image_handler , $save_to.$file_suffix , 100 );  
        }else if ( $this->_image_type == 3  )
        {
            imagepng( $image_handler , $save_to.$file_suffix , 100 );  
        }else if ( $this->_image_type == 1 )
        {
            imagegif( $image_handler , $save_to.$file_suffix , 100 );  
        }
          
        if( !is_file($save_to.$file_suffix) ) return false;
    }

}                                    

使用方法如下:

include("change_pic.php");
                                                                                                                                 
// $file , $file_suffix , $new_width ,$new_height , $save_to="./"
$obj = new Picture( 'girl.jpg' , 'newchange.jpg' , 160 , 120 ,"./image/");
if( !$obj ) 
{
      echo 'Change image size error';
      return false;
}

Posted in php分享 | Tags:

0 Comments.

Leave a Reply

[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Get Adobe Flash playerPlugin by wpburn.com wordpress themes