PHP图片添加水印

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

一共有3个文件:
1、functions.php
2、water.config.php
3、water.class.php
 
代码如下: 
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
 
/*
 *
 */
//获得数据库模型的对象
function M($table){
        return new Model($table);
}
//打印信息
function p($msg){
    echo "<pre>";
    print_r($msg);
    echo "</pre>";
}
//提示错误信息
function error($msg){
        echo "<div align='center' style='border:solid 3px #dcdcdc;padding:20px;margin:0 auto;'>
        $msg
        </div>";
        exit;
}
//自动加载类文件
function __autoload($className){
        if(strlen($className)>7 && substr($className,-7) == 'Control'){
                $classFile = PATH_APP.'/control/'.$className.'.php';
        }else{
                $classFile = PATH_XL.'/libs/bin/'.$className.'.class.php';
        }
        if(!is_file($classFile)){
                exit($classFile."文件不存在");
        }
        include $classFile;
}
//加载、设置配置项
function C($name=null,$value=null){
        static $config =array();
        if(is_array($name)){
                $config = array_merge($config,array_change_key_case($name));
        }
        if(is_string($name)){
                $name = strtolower($name);
                if(!is_null($value)){
                        $congfig[$name]=$value;
                        return true;
                }
                return isset($config[$name])?$config[$name]:null;
        }
        if(is_null($name)){
                return $config;
        }
}
//加载、设置字体
function L($name=null,$value=null){
        static $lang = array();
        if(is_array($name)){
                $lang  = array_merge($lang,array_change_key_case($name));
        }
        if(is_string($name)){
                $name = strtolower($name);
                if(!is_null($value)){
                        $lang[$name]=$value;
                        return true;
                }
                return isset($lang[$name])?$lang[$name]:null;
        }
        if(is_null($name)){
                return $lang;
        }
}
//获得文件、文件夹的大小
function getsize($path,$type=null){
        if(is_file($path)) return filesize($path);
        $type = is_null($type)?"*":"*.$type";
        $files = glob($path.'/'.$type);
        $size = 0;
        foreach($files as $f){
                $size+=is_dir($f)?getsize($f):filesize($f);
        }
        return $size;
}
?>

water.config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
return array(
    //水印处理配置项
    'water_img' => 'water.jpg',//水印图片
    'water_pos' => '9',//水印位置  取值范围:1-9
    /* 1-9的位置分别为
        ↖  ↑  ↗
        ←  ·  →
        ↙  ↓  ↘
    */
    'water_pct' => 60,//水印透明度  取值范围:0-100  (值越大、水印越清晰)
);
?>

water.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
include "functions.php";//加载小工具函数
C(include "water.config.php");//读取水印类配置项
//水印处理类
class waterd{
    public $water_pos='';//水印位置
    public $water_pct='';//水印透明度
    private $res;//图像资源
    //构造函数
    function __construct($pos=null,$pct=null){
        $this->water_pos = is_null($pos)?C('water_pos'):$pos;
        $this->water_pct = is_null($pct)?C('water_pct'):$pct;
    }
    //添加水印方法
    public function water($img){
        //检测
        if(!$this->check($img)){
            return false;
        }
        //水印图片资源
        $water_res = $this -> getres(C('water_img'));
        //原始图片资源
        $img_res = $this -> getres($img);
        //水印位置
        $posArr = $this -> getpos($img_res,$water_res);
        imagecopymerge($img_res,$water_res,$posArr[0],$posArr[1],0,0,imagesx($water_res),imagesy($water_res),$this->water_pct);
        $info = getimagesize($img);
        //打印图片信息 测试时可开启当前打印 p($info);
        $func = str_replace('/','',$info['mime']);
        $func($img_res,$img);
    }
    //检测
    private function check($img){
        return is_file($img) && extension_loaded('GD') &&  getimagesize($img);
    }
    //获得图片资源
    private function getres($img){
        $info = getimagesize($img);
        $type = trim(image_type_to_extension($info[2]),'.');
        $func = 'imagecreatefrom'.$type;
        return $func($img);
    }
    //获得水印位置
    private function getpos($img_res,$water_res){
        $img_x = imagesx($img_res);//原图宽度
        $img_y = imagesy($img_res);//原图宽度
        $water_x = imagesx($water_res);//水印宽度
        $water_y = imagesy($water_res);//水印宽度
        $pos = $this -> water_pos;//水印位置
        $x=15;$y=15;
        switch($pos){
            case 1:
                break;
            case 2:
                $x = ($img_x-$water_x)/2;
                break;
            case 3:
                $x = $img_x-$water_x-15;
                break;
            case 4:
                $y = ($img_y-$water_y)/2;
                break;
            case 5:
                $x = ($img_x-$water_x)/2;
                $y = ($img_y-$water_y)/2;
                break;
            case 6:
                $x = $img_x-$water_x-15;
                $y = ($img_y-$water_y)/2;
                break;
            case 7:
                $y = $img_y-$water_y-15;
                break;
            case 8:
                $x = ($img_x-$water_x)/2;
                $y = $img_y-$water_y-15;
                break;
            case 9:
                $x = $img_x-$water_x-15;
                $y = $img_y-$water_y-15;
                break;
        }
        return array($x,$y);
    }
 
}
//new一个对象然后 调用 水印的方法 water()即可、传进要添加水印的图片即可
$x = new waterd();
$x->water('img.jpg');
 
?>