首页 > 代码库 > php 给图片加水印

php 给图片加水印

昨天下午同事问我一个php的问题,就是给图片加水印,php我也一知半解,网上资料找了一通,自己就写了一个加水印的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
<?php
class WaterGener{
 
 
    private $default_text="Just for test";
    private $default_waterpic="girl.jpg";
    private $default_qulity=75;
    //默认使用的字体
    private $font = ‘simhei.ttf‘; //定义字体
 
    //默认的padding 的值
    private $padding=5;
 
    /**
    构造函数
    **/
    function __construct(){
 
    }
 
    //获取图片类型
    private function getImage($path){
        if(!empty($path) && file_exists($path)) {
            $water_info = @getimagesize($path);
            $water_im;
            switch($water_info[2])   {    //取得水印图片的格式 
                 case 1:$water_im = @imagecreatefromgif($path);break;
                 case 2:$water_im = @imagecreatefromjpeg($path);break;
                 case 3:$water_im = @imagecreatefrompng($path);break;
                 default:return 1;
             }
             return $water_im;
        }
        return -1;
    }
 
 
    public function buildWaterImage($picture,$logo="",$savePath="demo.jpg"){
        //需要判断图片的类型,水印图片的类型
        if(!empty($path) && file_exists($path))return -1;
 
        $logoImage =$this->getImage($logo===""?$this->default_waterpic:$logo);
 
        $photoImage =$this->getImage($picture);
        if($photoImage==-1){
            echo "没有找到图片";
            return;
        }
        imagealphablending($photoImage, true);
        $logo_size = getimagesize($logo);
        $logoW = $logo_size[0];
        $logoH = $logo_size[1];
        $picture_size = getimagesize($picture);
        ImageCopy($photoImage, $logoImage, $picture_size[0]-$logoW-$this->padding, $picture_size[1]-$logoH-$this->padding, 0, 0, $logoW, $logoH);
        ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser or file
        ImageDestroy($photoImage);
        ImageDestroy($logoImage);
        echo "success...";
    }
 
    public function buildWaterText($picture,$text="",$savePath="demo.jpg"){
        //需要判断
        $photoImage = $this->getImage($picture);
        ImageAlphaBlending($photoImage, true);
        $picture_size = getimagesize($picture);
        $textcolor = imagecolorallocate($photoImage, 255,255, 255);
        //解决乱码问题
        //$text = iconv("GB2312", "UTF-8", $text); //将中文字转换为UTF8
        imagettftext($photoImage, 20, 0, $this->padding, $picture_size[1]-($this->padding*4), $textcolor, $this->font, $text);//将文字写到图片中
        //imagestring($photoImage, 5, 0, 0,$text, $textcolor);
        ImageJPEG($photoImage,$savePath,$this->default_qulity); // output to browser
        ImageDestroy($photoImage);
    }
 
}
?>

上面代码有一些纰漏:

1,水印生成的位置,比如说可以是中间,右上角,右下角,等等。需要完善。

2,如果是加文字水印,文字水印的位置怎么计算?计算每个文字的宽度和高度?这个还有待完善。

3,异常的处理。php里面处理异常,我貌似还没有接触过。

上面代码调用方式:

1
2
3
4
5
6
<?php
include_once "water.class.php";
$water=new WaterGener();
$water->buildWaterImage("girl.jpg","logo.gif","demo1.jpg");
$water->buildWaterText("girl.jpg","开源中国");
?>