为百度UE编辑器上传图片添加水印功能

前些日子把phpcms的内置编辑器改成了百度UE编辑器,非常好用,但是有个地方不是很满意,就是没法给上传的图片加水印了,经过一番研究终于实现了出来,分享给大家。

UEditor编辑器上传图片是自动提取的,但是图片没有水印功能,下面小编和各位一起来看看。

UEditor编辑器没有上传图片加水印的功能,需要进行二次开发,本例是在PHPCMS系统中对百度编辑器进行二次开发,添加上传图片加水印功能。

首先打开UEditor编辑器文件目录的php文件夹,打开Uploader.class.php,把PHPCMS添加水印的方法复制过来,加到这个类所有成员方法最后面,然后进行修改如下:

  1. //图片加水印
  2. public function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = '99danji',$w_font = 8, $w_color = '#ff0000') {
  3. $this->w_img = 'watermark.png';
  4. $this->w_pos = 9;
  5. $this->w_minwidth = 400;
  6. $this->w_minheight = 200;
  7. $this->w_quality = 80;
  8. $this->w_pct = 85;
  9. $w_pos = $w_pos ? $w_pos : $this->w_pos;
  10. $w_img = $w_img ? $w_img : $this->w_img;
  11. //if(!$this->watermark_enable || !$this->check($source)) return false;
  12. if(!$target) $target = $source;
  13. //$w_img = PHPCMS_PATH.$w_img;
  14. //define('WWW_PATH', dirname(dirname(dirname(__FILE__)));
  15. $w_img = '../../../images/water/'.$w_img;
  16. $source_info = getimagesize($source);
  17. $source_w = $source_info[0];
  18. $source_h = $source_info[1];
  19. //if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
  20. switch($source_info[2]) {
  21. case 1 :
  22. $source_img = imagecreatefromgif($source);
  23. break;
  24. case 2 :
  25. $source_img = imagecreatefromjpeg($source);
  26. break;
  27. case 3 :
  28. $source_img = imagecreatefrompng($source);
  29. break;
  30. default :
  31. return false;
  32. }
  33. if(!emptyempty($w_img) && file_exists($w_img)) {
  34. $ifwaterimage = 1;
  35. $water_info = getimagesize($w_img);
  36. $width = $water_info[0];
  37. $height = $water_info[1];
  38. switch($water_info[2]) {
  39. case 1 :
  40. $water_img = imagecreatefromgif($w_img);
  41. break;
  42. case 2 :
  43. $water_img = imagecreatefromjpeg($w_img);
  44. break;
  45. case 3 :
  46. $water_img = imagecreatefrompng($w_img);
  47. break;
  48. default :
  49. return;
  50. }
  51. } else {
  52. $ifwaterimage = 0;
  53. $temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
  54. $width = $temp[2] - $temp[6];
  55. $height = $temp[3] - $temp[7];
  56. unset($temp);
  57. }
  58. switch($w_pos) {
  59. case 1:
  60. $wx = 5;
  61. $wy = 5;
  62. break;
  63. case 2:
  64. $wx = ($source_w - $width) / 2;
  65. $wy = 0;
  66. break;
  67. case 3:
  68. $wx = $source_w - $width;
  69. $wy = 0;
  70. break;
  71. case 4:
  72. $wx = 0;
  73. $wy = ($source_h - $height) / 2;
  74. break;
  75. case 5:
  76. $wx = ($source_w - $width) / 2;
  77. $wy = ($source_h - $height) / 2;
  78. break;
  79. case 6:
  80. $wx = $source_w - $width;
  81. $wy = ($source_h - $height) / 2;
  82. break;
  83. case 7:
  84. $wx = 0;
  85. $wy = $source_h - $height;
  86. break;
  87. case 8:
  88. $wx = ($source_w - $width) / 2;
  89. $wy = $source_h - $height;
  90. break;
  91. case 9:
  92. $wx = $source_w - $width;
  93. $wy = $source_h - $height;
  94. break;
  95. case 10:
  96. $wx = rand(0,($source_w - $width));
  97. $wy = rand(0,($source_h - $height));
  98. break;
  99. default:
  100. $wx = rand(0,($source_w - $width));
  101. $wy = rand(0,($source_h - $height));
  102. break;
  103. }
  104. if($ifwaterimage) {
  105. if($water_info[2] == 3) {
  106. imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
  107. } else {
  108. imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  109. }
  110. } else {
  111. if(!emptyempty($w_color) && (strlen($w_color)==7)) {
  112. $r = hexdec(substr($w_color,1,2));
  113. $g = hexdec(substr($w_color,3,2));
  114. $b = hexdec(substr($w_color,5));
  115. } else {
  116. return;
  117. }
  118. imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  119. }
  120. switch($source_info[2]) {
  121. case 1 :
  122. imagegif($source_img, $target);
  123. break;
  124. case 2 :
  125. imagejpeg($source_img, $target, $this->w_quality);
  126. break;
  127. case 3 :
  128. imagepng($source_img, $target);
  129. break;
  130. default :
  131. return;
  132. }
  133. if(isset($water_info)) {
  134. unset($water_info);
  135. }
  136. if(isset($water_img)) {
  137. imagedestroy($water_img);
  138. }
  139. unset($source_info);
  140. imagedestroy($source_img);
  141. return true;
  142. }
  143. public function check($image) {
  144. 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]));
  145. }

对比我修改的部分,由于phpcms水印可以在后台管理设置,phpcms自带的水印方法通过读取配置文件获取路径,和读取数据库设置获取参数设置,那么这些地方需要手动进行设置。

对了,在upFile方法还要添加一段函数:

  1. if ($this->watermark) {
  2. $this->watermark($this->filePath,$this->filePath);
  3. }

然后打开UEditor百度编辑器php目录下的action_upload.php文件,加上是否添加水印的参数:

  1. /* 上传配置 */
  2. $base64 = "upload";
  3. switch (htmlspecialchars($_GET['action'])) {
  4. case 'uploadimage':
  5. $config = array(
  6. "pathFormat" => $CONFIG['imagePathFormat'],
  7. "maxSize" => $CONFIG['imageMaxSize'],
  8. "allowFiles" => $CONFIG['imageAllowFiles']
  9. );
  10. $fieldName = $CONFIG['imageFieldName'];
  11. $watermark = true;
  12. break;

然后在后面还有一句要改成:

  1. /* 生成上传实例对象并完成上传 */
  2. $up = new Uploader($fieldName, $config, $base64, $watermark);

这样就大功告成了,本文主要是提供思路和参考。