图片加水印

来源:互联网 发布:淘宝联盟没有佣金原因 编辑:程序博客网 时间:2024/05/29 17:27

<?php
class image{
    //是否开启水印
    public $watermark_on=1;
    //水印图片
    public $wate_image="02.jpg";
    //水印的位置
    public $pos=1;
    //水印的透明度
    public $pct=50;
    //图像的 压缩比
    public $quality=80;
    //水印文字内容
    public $text="牧田网http://www.wangmutian.com";
    //水印文字大小
    public $text_size=12;
    //水印文字颜色
    public $text_color="#000000";
    //水印文字的字体
    public $font="font/123.TTF";
    
    /**
     * 环境验证
     * @param $img 图像路径
     */
    private function check($img){
        $type=array(".jpg",".jpeg",".png",".gif");
        $img_type=strtolower(strrchr($img,"."));
       
        if(extension_loaded('gd') && file_exists($img) && in_array($img_type, $type)){
            return true;
        }else{
            return false;
        }
       
    }
    
    /**
     * 水印处理
     */
    public function watermark($img,$out_img='',$water_img='',$pos='',$text=''){
        //验证原图像
        if(!$this->check($img)|| !$this->watermark_on) return false;
        
        //验证水印图像
        $waterimg_on =$this->check($water_img)?1:0;
        //var_dump($this->check($water_img)); exit();
        //判断另存图像
        $out_img=$out_img?$out_img:$img;
        //水印位置
        $pos=$pos?$pos:$this->pos;
        //水印文字
        $text=$text?$text:$this->text;
        //验证水印图像
        $water_img=$water_img?$water_img:$this->wate_image;
        
        //获取原图片的宽和高
        $img_info=getimagesize($img);
        $img_w=$img_info[0];  //0代表宽度  1代表高度 2代表类型
        $img_h=$img_info[1];
        // 获得水印信息
       
        if($waterimg_on){
            $w_info=getimagesize($water_img);
            $w_w=$w_info[0];
            $w_h=$w_info[1];
            switch ($w_info[2]){
                case 1:
                    $w_img=imagecreatefromgif($water_img);
                break;
                case 2:
                    $w_img=imagecreatefromjpeg($water_img);
                break;
                case 3:
                    $w_img=imagecreatefrompng($water_img);
                break;    
                
            }
        }else{            
            if(empty($text) || strlen($this->text_color)!=7) return false;
            
            //获得文字水印的宽高
            
            $text_info=imagettfbbox($this->text_size, 0, $this->font, $text);
            
            $w_w=$text_info[2]-$text_info[6];
            $w_h=$text_info[3]-$text_info[7];
        }
        
        
        //水印位置处理方法
        switch ($pos){
            case 1:
                $x=25;
                $y=25;
                break;  
            case 2:
                $x=($img_w-$w_w)/2;
                $y=25;
                break;
            case 3:
                $x=$img_w-$w_w;
                $y=25;
                break;
            case 4:
                $x=25;
                $y=($img_h-$w_h)/2;
                break;    
            case 5:
                $x=($img_w-$w_w)/2;
                $y=($img_h-$w_h)/2;
                break;    
            case 6:
                $x=$img_w-$w_w;
                $y=($img_h-$w_h)/2;
                break;
            case 7:
                $x=25;
                $y=$img_h-$w_h;
                break;
            case 8:
                $x=($img_w-$w_w)/2;
                $y=$img_h-$w_h;
                break;
            case 9:
                $x=$img_w-$w_w;
                $y=$img_h-$w_h;
                break;
             default:
                $x=mt_rand(25, $img_w-$w_w);
                $y=mt_rand(25, $img_h-$w_h);
        }
        //建立原图的资源
        if($img_h<$w_h || $img_w<$w_w) return false;
        switch ($img_info[2]){
            case 1:
                $res_img=imagecreatefromgif($img);
                break;
            case 2:
                $res_img=imagecreatefromjpeg($img);
                break;
            case 3:
                $res_img=imagecreatefrompng($img);
                break;
        }
        
        if($waterimg_on){  //以图片的方式写
            imagecopymerge($res_img, $w_img, $x, $y,0, 0, $w_w, $w_h, $this->pct);
        }else{   //已文字方式写
            $r=hexdec(substr($this->text_color, 1,2));
            $g=hexdec(substr($this->text_color, 3,2));
            $b=hexdec(substr($this->text_color, 5,2));
            $color=imagecolorallocate($res_img, $r, $g, $b);
            imagettftext($res_img, $this->text_size, 0, $x, $y, $color, $this->font, iconv('gbk', 'utf-8', $text));
        }
        
        
        switch ($img_info[2]){
            case 1:
                imagegif($res_img,$out_img);
                break;
            case 2:
                imagejpeg($res_img,$out_img);
                break;
            case 3:
                imagegif($res_img,$out_img);
                break;
        }
        if(isset($res_img)) imagedestroy($res_img);
        if (isset($w_img)) imagedestroy($w_img);
        return false;
    }
    
    
}
$image=new image();
$type=$image->watermark("13.jpg","21.jpg","11111111",0);

0 0