不用插件 WordPress图片自动加水印纯代码实现

之前写过一篇 WordPress图片自动加水印插件汉化版(链接见文末),但如果你觉得安装 WordPress插件有点浪费资源,那么你也可以不用插件,而使用下面的方法手动创建一个文件代码来自动添加图片水印(其实和插件的原理是一样的)。

WordPress图片自动加水印纯代码

WordPress上传图片自动添加水印纯代码方法

第一步:在主题目录里面新建 class文件夹,然后把事先准备好的 image.php 文件放进去;

你可以新建一个名为 image.php的文件将下面的代码放进去保存,也可以下载我已准备好的 image.php文件直接使用;

image.php文件中代码如下:

  1. <?php
  2. /**
  3. * WordPress上传图片添加水印 By jianlove.com
  4. */
  5. class image {
  6. var $w_pct = 100;
  7. var $w_quality = 80;
  8. var $w_minwidth = 300;
  9. var $w_minheight = 300;
  10. var $w_path = ‘/data/watermark/’;
  11. var $watermark_enable;
  12. public function __construct($watermark_enable = 0)
  13. {
  14. $this->watermark_enable = $watermark_enable;
  15. }
  16. public function set($w_img$w_pos$w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100)
  17. {
  18. $this->w_img = $w_img;
  19. $this->w_pos = $w_pos;
  20. $this->w_minwidth = $w_minwidth;
  21. $this->w_minheight = $w_minheight;
  22. $this->w_quality = $w_quality;
  23. $this->w_pct = $w_pct;
  24. }
  25. public function info($img)
  26. {
  27. $imageinfo = getimagesize($img);
  28. if($imageinfo === false) return false;
  29. $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
  30. $imagesize = filesize($img);
  31. $info = array(
  32. ‘width’=>$imageinfo[0],
  33. ‘height’=>$imageinfo[1],
  34. ‘type’=>$imagetype,
  35. ‘size’=>$imagesize,
  36. ‘mime’=>$imageinfo[‘mime’]
  37. );
  38. return $info;
  39. }
  40. public function watermark($source$target = $w_pos = $w_img = $w_text = ‘watermark’,$w_font = 8, $w_color = ‘#ff0000’)
  41. {
  42. $source = ABSPATH.str_replace(home_url().’/’, $source);
  43. $w_pos = $w_pos ? $w_pos : $this->w_pos;
  44. $w_img = $w_img ? $w_img : $this->w_img;
  45. if(!$this->watermark_enable || !$this->check($source)) return false;
  46. if(!$target$target = $source;
  47. $cache_w_img = THEME_FILES.$this->w_path.$w_img;
  48. $source_info = getimagesize($source);
  49. $source_w = $source_info[0];
  50. $source_h = $source_info[1];
  51. if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
  52. switch($source_info[2]) {
  53. case 1 :
  54. $source_img = imagecreatefromgif($source);
  55. break;
  56. case 2 :
  57. $source_img = imagecreatefromjpeg($source);
  58. break;
  59. case 3 :
  60. $source_img = imagecreatefrompng($source);
  61. break;
  62. default :
  63. return false;
  64. }
  65. if(!emptyempty($w_img) && file_exists($cache_w_img)) {
  66. $ifwaterimage = 1;
  67. $water_info = getimagesize($cache_w_img);
  68. $width = $water_info[0];
  69. $height = $water_info[1];
  70. switch($water_info[2]) {
  71. case 1 :
  72. $water_img = imagecreatefromgif($cache_w_img);
  73. break;
  74. case 2 :
  75. $water_img = imagecreatefromjpeg($cache_w_img);
  76. break;
  77. case 3 :
  78. $water_img = imagecreatefrompng($cache_w_img);
  79. break;
  80. default :
  81. return;
  82. }
  83. else {
  84. $ifwaterimage = 0;
  85. $temp = imagettfbbox(ceil($w_font*2.5), 0, THEME_FILES.$this->w_path.’elephant.ttf’, $w_text);
  86. $width = $temp[2] – $temp[6];
  87. $height = $temp[3] – $temp[7];
  88. unset($temp);
  89. }
  90. switch($w_pos) {
  91. case 1:
  92. $wx = 5;
  93. $wy = 5;
  94. break;
  95. case 2:
  96. $wx = ($source_w – $width) / 2;
  97. $wy = 0;
  98. break;
  99. case 3:
  100. $wx = $source_w – $width;
  101. $wy = 0;
  102. break;
  103. case 4:
  104. $wx = 0;
  105. $wy = ($source_h – $height) / 2;
  106. break;
  107. case 5:
  108. $wx = ($source_w – $width) / 2;
  109. $wy = ($source_h – $height) / 2;
  110. break;
  111. case 6:
  112. $wx = $source_w – $width;
  113. $wy = ($source_h – $height) / 2;
  114. break;
  115. case 7:
  116. $wx = 0;
  117. $wy = $source_h – $height;
  118. break;
  119. case 8:
  120. $wx = ($source_w – $width) / 2;
  121. $wy = $source_h – $height;
  122. break;
  123. case 9:
  124. $wx = $source_w – $width;
  125. $wy = $source_h – $height;
  126. break;
  127. case 10:
  128. $wx = rand(0,($source_w – $width));
  129. $wy = rand(0,($source_h – $height));
  130. break;
  131. default:
  132. $wx = rand(0,($source_w – $width));
  133. $wy = rand(0,($source_h – $height));
  134. break;
  135. }
  136. if($ifwaterimage) {
  137. if($water_info[2] == 3) {
  138. imagecopy($source_img$water_img$wx$wy, 0, 0, $width$height);
  139. else {
  140. imagecopymerge($source_img$water_img$wx$wy, 0, 0, $width$height$this->w_pct);
  141. }
  142. else {
  143. if(!emptyempty($w_color) && (strlen($w_color)==7)) {
  144. $r = hexdec(substr($w_color,1,2));
  145. $g = hexdec(substr($w_color,3,2));
  146. $b = hexdec(substr($w_color,5));
  147. else {
  148. return;
  149. }
  150. imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  151. }
  152. switch($source_info[2]) {
  153. case 1 :
  154. imagegif($source_img$target);
  155. break;
  156. case 2 :
  157. imagejpeg($source_img$target$this->w_quality);
  158. break;
  159. case 3 :
  160. imagepng($source_img$target);
  161. break;
  162. default :
  163. return;
  164. }
  165. if(isset($water_info)) {
  166. unset($water_info);
  167. }
  168. if(isset($water_img)) {
  169. imagedestroy($water_img);
  170. }
  171. unset($source_info);
  172. imagedestroy($source_img);
  173. return true;
  174. }
  175. private function check($image)
  176. {
  177. return extension_loaded(‘gd’) && preg_match(“/\.(jpg|jpeg|gif|png)/i”$image$m) && file_exists($image) && function_exists(‘imagecreatefrom’.($m[1] == ‘jpg’ ? ‘jpeg’ : $m[1]));
  178. }
  179. }

 

直接下载:

 

  下载地址:image.zip (百度网盘提取码:d6sb)

第二步打开当前主题的 functions.php 文件,添加下面的代码:

  1. /**
  2. * WordPress上传图片添加水印 By jianlove.com
  3. */
  4. function uimoban_watermark($attachment_ID)
  5. {
  6. $attachment = get_post($attachment_ID);
  7. switch($attachment->post_mime_type){
  8. case ‘image/jpeg’:
  9. case ‘image/png’:
  10. case ‘image/gif’:
  11. require THEME_FILES . ‘/class/image.php’;
  12. $image = new image(true);
  13. $image->set(‘watermark.png’, 1);
  14. $image->watermark($attachment->guid);
  15. break;
  16. default:return ;
  17. }
  18. }
  19. add_action(‘add_attachment’, ‘uimoban_watermark’);

注意:第13行中 watermark.png指定的是水印图片,和第一步代码中的 watermark.png应该保持一致,当然你也可以自行修改。“1”代表水印位置。

至此你就实现了不用插件还能自动给 WordPress上传的图片添加水印的功能。


如果你不想这么麻烦,当然也有简单后台直接安装插件的方法:多功能 WordPress 图片水印插件:image-watermark(汉化版)

本文转自:https://www.jianlove.com/image-watermark-code-309.html