你的位置:首页 > 网页设计

[网页设计]为百度UEDITOR编辑器上传图片添加水印


form:http://www.uphtm.com/php/255.html

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

对比我修改的部分,由于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);

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

form:http://www.uphtm.com/php/255.html