圖片處理類,實現圖片處理,包括添加水印和生成縮略圖

/**
 *===================================================================
 * image.class.php 圖片處理類,實現圖片處理,包括添加水印和生成縮略圖
 * 2013年3月25日22:10:38
 *===================================================================
 */
class Image{
    private $thumbPrefix = 'thumb_'; //縮略圖前綴
    private $waterPrefix = 'water_'; //水印圖片前綴

    //圖片類型和對應建立畫布資源的函數名
    private $from = array(
        'image/gif'  => 'imagecreatefromgif',
        'image/png'  => 'imagecreatefrompng',
        'image/jpeg' => 'imagecreatefromjpeg'
    );

    //圖片類型和對應生成圖片的函數名
    private $to = array(
        'image/gif'  => 'imagegif',
        'image/png'  => 'imagepng',
        'image/jpeg' => 'imagejpeg'
    );


    //構造函數
    public function __construct(){

    }
    /**
     * 添加水印功能
     * @access public
     * @param $image string 目標圖片
     * @param $water string 水印圖片
     * @param $postion number 添加水印位置,默認9,右下角
     * @param $path string 水印圖片存放路徑,默認爲空,表示在當前目錄
     * @return
     */
    public function watermark($image,$water,$postion=9,$path=''){
        //獲取源圖和水印圖片信息
        $dst_info = getimagesize($image);
        $water_info = getimagesize($water);
        $dst_w = $dst_info[0];
        $dst_h = $dst_info[1];
        $src_w = $water_info[0];
        $src_h = $water_info[1];

        //獲取各圖片對應的建立函數名
        $dst_create_fname = $this->from[$dst_info['mime']];
        $src_create_fname = $this->from[$water_info['mime']];

        //使用可變函數來建立畫布資源
        $dst_img = $dst_create_fname($image);
        $src_img = $src_create_fname($water);

        //水印位置
        switch ($postion) {
            //左上
            case 1:
                $dst_x = 0;
                $dst_y = 0;
                break;
            //中上
            case 2:
                $dst_x = ($dst_w - $src_w)/2;
                $dst_y = 0;
                break;
            //右上
            case 3:
                $dst_x = $dst_w - $src_w;
                $dst_y = 0;
                break;
            //中左
            case 4:
                $dst_x = 0;
                $dst_y = ($dst_h - $src_h)/2;
                break;
            //中中
            case 5:
                $dst_x = ($dst_w - $src_w)/2;
                $dst_y = ($dst_h - $src_h)/2;
                break;
            //中右
            case 6:
                $dst_x = $dst_w - $src_w;
                $dst_y = ($dst_h - $src_h)/2;
                break;
            //下左
            case 7:
                $dst_x = 0;
                $dst_y = $dst_h - $src_h;
                break;
            //下中
            case 8:
                $dst_x = ($dst_w - $src_w)/2;
                $dst_y = $dst_h - $src_h;
                break;
            //下右
            case 9:
                $dst_x = $dst_w - $src_w;
                $dst_y = $dst_h - $src_h;
                break;
            //隨機
            case 0:
                $dst_x = rand(0,$dst_w - $src_w);
                $dst_y = rand(0,$dst_h - $src_h);
                break;
            default:
                # code...
                break;
        }
        
        //將水印圖片添加到目標圖標上
        imagecopy($dst_img, $src_img, $dst_x, $dst_y, 0, 0, $src_w, $src_h);

        //生成帶水印的圖片
        $waterfile = $path.$this->waterPrefix.basename($image);
        $generate_fname = $this->to[$dst_info['mime']];
        
        if ($generate_fname($dst_img,$waterfile)){
            return $waterfile;
        } else {
            return false;
        }


    }

    /**
     * 生成縮略圖,等比例縮放,有補白效果
     * @access public
     * @param $image string 目標圖片,
     * @param $max_width number 縮略圖最大寬度
     * @param $max_height number  縮略圖最大高度
     * @return 成功返回縮略圖名稱,失敗返回false
     */
    public function thumbnail($image,$max_width,$max_height,$path=''){
        //獲取圖片信息
        $info = getimagesize($image);
        $src_width = $info[0];
        $src_height = $info[1];
        //echo $src_width,$src_height;
        //經過計算比例,獲得縮略圖的大小
        if ($src_width / $max_width > $src_height / $max_height) {
            # 此時應該以寬爲準
            $dst_width = $max_width;
            $dst_height = ($max_width / $src_width) * $src_height;
        } else {
            # 此時應該以高爲準
            $dst_height = $max_height;
            $dst_width = ($max_height / $src_height) * $src_width;
        }
        //使用可變函數建立源圖資源
        $src_create_fname = $this->from[$info['mime']];
        $src_img = $src_create_fname($image);
        //建立縮略圖資源,大小爲$max_width x $max_height;
        $dst_img = imagecreatetruecolor($max_width, $max_height);
        //填充白色背景
        imagefill($dst_img, 0, 0, imagecolorallocate($dst_img, 255, 255, 255));
        //計算縮略圖在畫布上的位置,保證比例不等時圖片能居中
        $dst_x = ($max_width - $dst_width)/2;
        $dst_y = ($max_height - $dst_height)/2;
        //將按比例將縮略圖從新採樣,調整其位置
        imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
        $thumbfile = $this->thumbPrefix . pathinfo($image,PATHINFO_BASENAME);

        $generate_fname = $this->to[$info['mime']];
        if ($generate_fname($dst_img,$path . $thumbfile)) {
            # 成功返回縮略圖名稱,注意返回的名稱,不一樣地方上傳方案會有不一樣的路徑
            return date('Ymd') . '/' . $thumbfile;
        } else {
            # 失敗返回false
            return false;
        }
    }
}

//調用實例
//$img = new image;
//$img->thumbnail('a.jpg',200,200,'D:/amp/www/shopcz/');
//$img->watermark('a.jpg','sina.png',9,'D:/amp/www/shopcz/');php

相關文章
相關標籤/搜索