图像处理类

<?php
//图像处理类
class Image {
    private $file;                //图片地址
    private $width;                //图片长度
    private $height;            //图片长度
    private $type;                //图片类型
    private $img;                //原图的资源句柄
    private $new;                //新图的资源句柄
    
    //构造方法,初始化
    public function __construct($_file) {//echo $_file; echo $_SERVER["DOCUMENT_ROOT"];exit;//当前运行脚本所在文档根目录
        $this->file = $_SERVER["DOCUMENT_ROOT"].$_file;//图片处理必须为硬路径
        list($this->width, $this->height, $this->type) = getimagesize($this->file);//echo $this->type;exit;
        $this->img = $this->getFromImg($this->file, $this->type);
    }

    //cke专用图像处理
    public function ckeImg($new_width = 0,$new_height = 0) {
        list($_water_width,$_water_height,$_water_type) = getimagesize(MARK);
        $_water = $this->getFromImg(MARK,$_water_type);
        
        if (empty($new_width) && empty($new_height)) {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        if (!is_numeric($new_width) || !is_numeric($new_height)) {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        if ($this->width > $new_width) { //通过指定长度,获取等比例的高度
            $new_height = ($new_width / $this->width) * $this->height;
        } else {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        if ($this->height > $new_height) { //通过指定高度,获取等比例长度
            $new_width = ($new_height / $this->height) * $this->width;
        } else {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        $_water_x = $new_width - $_water_width - 5;
        $_water_y = $new_height - $_water_height - 5;
        
        
        $this->new = imagecreatetruecolor($new_width,$new_height);//resource imagecreatetruecolor( int x_size, int y_size )返回一个图像标识符,代表一幅大小为 x_size 和 y_size 的黑色图像
        imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
        if ($new_width > $_water_width && $new_height > $_water_height) {//图小与水印的长和宽则不显示
            imagecopy($this->new,$_water,$_water_x,$_water_y,0,0,$_water_width,$_water_height);
            //imagecopy ( resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
        }
        imagedestroy($_water);//bool imagedestroy ( resource image )imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符
    }

    //缩略图(固定长高容器,图像等比例,扩容填充,裁剪)[固定了大小,不失真,不变形]
    public function thumb($new_width = 0,$new_height = 0) {
        
        if (empty($new_width) && empty($new_height)) {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        if (!is_numeric($new_width) || !is_numeric($new_height)) {
            $new_width = $this->width;
            $new_height = $this->height;
        }
        
        //创建一个容器
        $_n_w = $new_width;
        $_n_h = $new_height;
        
        //创建裁剪点
        $_cut_width = 0;
        $_cut_height = 0;
        
        if ($this->width < $this->height) {//$new_width=$new_height=200 
            $new_width = ($new_height / $this->height) * $this->width;//$this_width=400 $this_height=500  $new_width=200/500*400=160
        } else {
            $new_height = ($new_width / $this->width) * $this->height;//$this_width=500 $this_height=400  $new_height=200/500*400=160
        }
    
        
        
        
        if ($new_width < $_n_w) { //如果新高度小于新容器高度  160<200
            $r = $_n_w / $new_width; //按长度求出等比例因子   $r=200/160=5/4
            $new_width *= $r; //扩展填充后的长度             $new_width=160*5/4=200         
            $new_height *= $r; //扩展填充后的高度            $new_height=200*5/4= 250
            $_cut_height = ($new_height - $_n_h) / 2; //求出裁剪点的高度 $_cut_height=(250-200)/2=25  
        }
        
        if ($new_height < $_n_h) { //如果新高度小于容器高度    160<200
            $r = $_n_h / $new_height; //按高度求出等比例因子   $r=200/160=5/4
            $new_width *= $r; //扩展填充后的长度               $new_width = 200*5/4=250
            $new_height *= $r; //扩展填充后的高度              $new_height = 160*5/4=200
            $_cut_width = ($new_width - $_n_w) / 2; //求出裁剪点的长度  $_cut_width = (250-200)/2=25  
        }
            
        
        $this->new = imagecreatetruecolor($_n_w,$_n_h);//resource imagecreatetruecolor( int x_size, int y_size )返回一个图像标识符,代表一幅大小为 x_size 和 y_size 的黑色图像
        imagecopyresampled($this->new,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);//imagecopyresampled($this->new,$this->img,0,0,0,25,160,200,)
        //bool imagecopyresampled(resource dst_image,resource src_image,int dst_x,int dst_y, int src_x, int src_y, int dst_w,int dst_h, int src_w, int src_h)
        //将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。如果成功则返回 TRUE,失败则返回 FALSE。
        //dst_image,src_image分别是目标图像和源图像的标识符
    }
    
    //加载图片,各种类型,返回图片的资源句柄
    private function getFromImg($_file, $_type) {//echo $this->type;exit;
    //getimagesize返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,
        switch ($_type) {
            case 1 :
                $img = imagecreatefromgif($_file);//resource imagecreatefromgif(string filename)从GIF文件或URL新建一图像
                break;
            case 2 :
                $img = imagecreatefromjpeg($_file);
                break;
            case 3 : 
                $img = imagecreatefrompng($_file);
                break;
            default:
                Tool::alertBack('警告:此图片类型本系统不支持!');
        }
        return $img;
    }
    
    //图像输出
    public function out() {
        imagepng($this->new,$this->file);//bool imagepng(resource image[,string filename]) 以PNG格式将图像输出到浏览器或文件,如果用filename给出了文件名则将其输出到该文件
        imagedestroy($this->img);
        imagedestroy($this->new);
    }
}
?>

 

posted @ 2013-01-06 20:37  quba  阅读(190)  评论(0)    收藏  举报