首页 > 后端开发 > php教程 > php图片水印添加,压缩,剪切类的封装

php图片水印添加,压缩,剪切类的封装

WBOY
发布: 2016-07-30 13:31:23
原创
929 人浏览过

  php对图片文件的操作主要是利用GD库扩展。当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码。当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有了封装成类的想法。

  操作图片主要历经四个步骤:

  1. 打开图片
  2. 操作图片
  3. 输出图片
  4. 销毁图片

  1,3,4三个步骤每次都要写,每次又都差不多。真正需要变通的只有操作图片的这一步骤了。操作图片又往往通过1或多个主要的GD函数来完成。

  本文封装类里面的四种方法,文字水印(imagettftext()),图片水印(imagecopymerge()),图片压缩,图片剪切(imagecopyresampled()),其余的常用GD函数便不赘述。直接上代码:

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

92

93

94

95

96

97

98

<span>php

 

</span><span>class</span><span> Image

{   

    </span><span>private</span><span> $info;

 

    </span><span>private</span><span> $image;

    </span><span>public</span><span> $type;

    </span><span>public</span><span> function __construct($src)

    {

 

        $</span><span>this</span>->info=<span>getimagesize($src);

        $</span><span>this</span>->type=image_type_to_extension($<span>this</span>->info[<span>'</span><span>2</span><span>'</span>],<span>false</span><span>);

        $fun</span>=<span>"</span><span>imagecreatefrom{$this->type}</span><span>"</span><span>;

        $</span><span>this</span>->image=<span>$fun($src);

    }

    </span><span>/*</span><span>*

     * 文字水印

     * @param  [type]  $font     字体

     * @param  [type]  $content  内容

     * @param  [type]  $size     文字大小

     * @param  [type]  $col      文字颜色(四元数组)

     * @param  array   $location 位置

     * @param  integer $angle    倾斜角度

     * @return [type]          

     </span><span>*/</span><span>public</span> function fontMark($font,$content,$size,$col,$location,$angle=<span>0</span><span>){

        $col</span>=imagecolorallocatealpha($<span>this</span>->image, $col[<span>'</span><span>0</span><span>'</span>], $col[<span>'</span><span>1</span><span>'</span>], $col[<span>'</span><span>2</span><span>'</span>],$col[<span>'</span><span>3</span><span>'</span><span>]);

 

        imagettftext($</span><span>this</span>->image, $size, $angle, $location[<span>'</span><span>0</span><span>'</span>], $location[<span>'</span><span>1</span><span>'</span><span>], $col,$font,$content);

    }

     

    </span><span>/*</span><span>*

     * 图片水印

     * @param  [type] $imageMark 水印图片地址

     * @param  [type] $dst       水印图片在原图片中的位置

     * @param  [type] $pct       透明度

     * @return [type]           

     </span><span>*/</span><span>public</span><span> function imageMark($imageMark,$dst,$pct){

        $info2</span>=<span>getimagesize($imageMark);

        $type</span>=image_type_to_extension($info2[<span>'</span><span>2</span><span>'</span>],<span>false</span><span>);

        $func2</span>=<span>"</span><span>imagecreatefrom</span><span>"</span><span>.$type;

        $water</span>=<span>$func2($imageMark);

 

        imagecopymerge($</span><span>this</span>->image, $water, $dst[<span>0</span>], $dst[<span>1</span>], <span>0</span>, <span>0</span>, $info2[<span>'</span><span>0</span><span>'</span>], $info2[<span>'</span><span>1</span><span>'</span><span>], $pct);

        imagedestroy($water);

 

    }

    </span><span>/*</span><span>*

     * 压缩图片

     * @param  [type] $thumbSize 压缩图片大小

     * @return [type]            [description]

     </span><span>*/</span><span>public</span><span> function thumb($thumbSize){

        $imageThumb</span>=imagecreatetruecolor($thumbSize[<span>0</span>], $thumbSize[<span>1</span><span>]);

         

        imagecopyresampled($imageThumb, $</span><span>this</span>->image, <span>0</span>, <span>0</span>, <span>0</span>, <span>0</span>, $thumbSize[<span>0</span>], $thumbSize[<span>1</span>], $<span>this</span>->info[<span>'</span><span>0</span><span>'</span>], $<span>this</span>->info[<span>'</span><span>1</span><span>'</span><span>]);

        imagedestroy($</span><span>this</span>-><span>image);

        $</span><span>this</span>->image=<span>$imageThumb;

    }

    </span><span>/*</span><span>*

    * 裁剪图片

     * @param  [type] $cutSize  裁剪大小

     * @param  [type] $location 裁剪位置

     * @return [type]           [description]

     </span><span>*/</span><span>public</span><span> function cut($cutSize,$location){

         $imageCut</span>=imagecreatetruecolor($cutSize[<span>0</span>],$cutSize[<span>1</span><span>]);

 

         imagecopyresampled($imageCut, $</span><span>this</span>->image, <span>0</span>, <span>0</span>, $location[<span>0</span>], $location[<span>1</span>],$cutSize[<span>0</span>],$cutSize[<span>1</span>],$cutSize[<span>0</span>],$cutSize[<span>1</span><span>]);

         imagedestroy($</span><span>this</span>-><span>image);

         $</span><span>this</span>->image=<span>$imageCut;

     }

    </span><span>/*</span><span>*

     * 展现图片

     * @return [type] [description]

     </span><span>*/</span><span>public</span><span> function show(){

        header(</span><span>"</span><span>content-type:</span><span>"</span>.$<span>this</span>->info[<span>'</span><span>mime</span><span>'</span><span>]);

 

        $funn</span>=<span>"</span><span>image</span><span>"</span>.$<span>this</span>-><span>type;

 

        $funn($</span><span>this</span>-><span>image);

    }

    </span><span>/*</span><span>*

     * 保存图片

 * @param  [type] $newname 新图片名

 * @return [type]          [description]

 </span><span>*/</span><span>public</span><span> function save($newname){

         header(</span><span>"</span><span>content-type:</span><span>"</span>.$<span>this</span>->info[<span>'</span><span>mime</span><span>'</span><span>]);

 

         $funn</span>=<span>"</span><span>image</span><span>"</span>.$<span>this</span>-><span>type;

 

         $funn($</span><span>this</span>->image,$newname.<span>'</span><span>.</span><span>'</span>.$<span>this</span>-><span>type);

     }

     </span><span>public</span><span> function __destruct(){

         imagedestroy($</span><span>this</span>-><span>image);

     }

 

 }

 

 </span>?>

登录后复制

  如果还需要其他操作,只需要再往这个类里面添加就好啦~~

以上就介绍了php图片水印添加,压缩,剪切类的封装,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板