首页  >  文章  >  后端开发  >  php图片添加水印示例

php图片添加水印示例

WBOY
WBOY原创
2016-07-25 09:12:55882浏览

例子,php为图片添加水印的代码。

  1. /*
  2. //示例
  3. $image = new Gimage();
  4. $image->limit = 600;//长宽限制
  5. $image->wm_text=”www.linuxlaptop.cn”;//水印文字
  6. $image->wm_fontfile=”font/xsuni.ttf”;//字体文件
  7. $image->wm_color=”#ff0000″;
  8. $image->save_file = “ltcn.jpg”;//保存到xx文件
  9. $image->create(“linuxlaptop.jpg”);//从xx文件创建
  10. */
  11. /*
  12. +------------------------------
  13. | 生成缩略图&加水印的图片类
  14. +------------------------------
  15. */
  16. Class Gimage{
  17. var $input_type = ""; //输入图片的格式
  18. var $output_type = "jpg"; //输出图片的格式
  19. var $limit = 0; //图片大小限制
  20. var $filename = ""; //输入图片的文件名(也可以直接是图片数据)
  21. var $jpeg_quality = 90; //jpeg图片质量
  22. var $save_file = ''; //输出文件名
  23. var $wm_text = ""; //水印文字( 不支持中文:'( )
  24. var $wm_size = 12; //水印文字大小
  25. var $wm_angle = 0; //水印文字角度
  26. var $wm_x = 30; //水印x坐标
  27. var $wm_y = 30; //水印y坐标
  28. var $wm_color = "#cccccc"; //水印颜色
  29. var $wm_fontfile = "geodesic.ttf";//水印字体文件
  30. function create($filename="")
  31. {
  32. if ($filename) $this->filename = $filename;
  33. if (!$this->input_type) $this->get_type();
  34. if (!$this->output_type) $this->output_type = $this->input_type;
  35. if ($this->input_type == "jpg") $this->input_type = "jpeg";
  36. if ($this->output_type == "jpg") $this->output_type = "jpeg";
  37. switch ($this->input_type){
  38. case 'gif':
  39. $src_img=ImageCreateFromGIF($this->filename);
  40. break;
  41. case 'jpeg':
  42. $src_img=ImageCreateFromJPEG($this->filename);
  43. break;
  44. case 'png':
  45. $src_img=ImageCreateFromPNG($this->filename);
  46. break;
  47. default:
  48. $src_img=ImageCreateFromString($this->filename);
  49. break;
  50. }
  51. $src_w=ImageSX($src_img);
  52. $src_h=ImageSY($src_img);
  53. if ($src_w>=$src_h){
  54. if ($src_w>$this->limit){
  55. $new_w=$this->limit;
  56. $new_h=($this->limit / $src_w)*$src_h;
  57. }
  58. }
  59. else{
  60. if ($src_h>$this->limit){
  61. $new_h=$this->limit;
  62. $new_w=($this->limit / $src_h)*$src_w;
  63. }
  64. }
  65. if ($new_h){
  66. $dst_img=imagecreatetruecolor($new_w,$new_h);
  67. imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
  68. }
  69. else{
  70. $dst_img = $src_img;
  71. }
  72. if ($this->wm_text)
  73. {
  74. if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_color, $color))
  75. {
  76. $red = hexdec($color[1]);
  77. $green = hexdec($color[2]);
  78. $blue = hexdec($color[3]);
  79. }
  80. $wm_color = imagecolorallocatealpha($dst_img, $red, $green, $blue, 90);
  81. imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text);
  82. }
  83. if ($this->save_file)
  84. {
  85. switch ($this->output_type){
  86. case 'gif':
  87. $src_img=ImagePNG($dst_img, $this->save_file);
  88. break;
  89. case 'jpeg':
  90. $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  91. break;
  92. case 'png':
  93. $src_img=ImagePNG($dst_img, $this->save_file);
  94. break;
  95. default:
  96. $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  97. break;
  98. }
  99. }
  100. else
  101. {
  102. header("Content-type: image/{$this->output_type}");
  103. switch ($this->output_type){
  104. case 'gif':
  105. $src_img=ImagePNG($dst_img);
  106. break;
  107. case 'jpeg':
  108. $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  109. break;
  110. case 'png':
  111. $src_img=ImagePNG($dst_img);
  112. break;
  113. default:
  114. $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  115. break;
  116. }
  117. }
  118. imagedestroy($dst_img);
  119. }
  120. function get_type()//获取图像文件类型
  121. {
  122. $name_array = explode(".",$this->filename);
  123. if (preg_match("/\.(jpg|jpeg|gif|png)$/", $this->filename, $matches))
  124. {
  125. $this->input_type = strtolower($matches[1]);
  126. }
  127. else
  128. {
  129. $this->input_type = "string";
  130. }
  131. }
  132. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn