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


Posted in PHP onApril 16, 2015

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

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

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

//图片加水印
public function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = '99danji',$w_font = 8, $w_color = '#ff0000') {
  $this->w_img = 'watermark.png';
  $this->w_pos = 9;
  $this->w_minwidth = 400;
  $this->w_minheight = 200;
  $this->w_quality = 80;
  $this->w_pct = 85;
 
  $w_pos = $w_pos ? $w_pos : $this->w_pos;
  $w_img = $w_img ? $w_img : $this->w_img;
  //if(!$this->watermark_enable || !$this->check($source)) return false;
  if(!$target) $target = $source;
  //$w_img = PHPCMS_PATH.$w_img;
  //define('WWW_PATH', dirname(dirname(dirname(__FILE__)));
  $w_img = '../../../images/water/'.$w_img;
  $source_info = getimagesize($source);
  $source_w  = $source_info[0];
  $source_h  = $source_info[1];
  //if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
  switch($source_info[2]) {
    case 1 :
      $source_img = imagecreatefromgif($source);
      break;
    case 2 :
      $source_img = imagecreatefromjpeg($source);
      break;
    case 3 :
      $source_img = imagecreatefrompng($source);
      break;
    default :
      return false;
  }
  if(!empty($w_img) && file_exists($w_img)) {
    $ifwaterimage = 1;
    $water_info  = getimagesize($w_img);
    $width    = $water_info[0];
    $height    = $water_info[1];
    switch($water_info[2]) {
      case 1 :
        $water_img = imagecreatefromgif($w_img);
        break;
      case 2 :
        $water_img = imagecreatefromjpeg($w_img);
        break;
      case 3 :
        $water_img = imagecreatefrompng($w_img);
        break;
      default :
        return;
    }
  } else {    
    $ifwaterimage = 0;
    $temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
    $width = $temp[2] - $temp[6];
    $height = $temp[3] - $temp[7];
    unset($temp);
  }
  switch($w_pos) {
    case 1:
      $wx = 5;
      $wy = 5;
      break;
    case 2:
      $wx = ($source_w - $width) / 2;
      $wy = 0;
      break;
    case 3:
      $wx = $source_w - $width;
      $wy = 0;
      break;
    case 4:
      $wx = 0;
      $wy = ($source_h - $height) / 2;
      break;
    case 5:
      $wx = ($source_w - $width) / 2;
      $wy = ($source_h - $height) / 2;
      break;
    case 6:
      $wx = $source_w - $width;
   $wy = ($source_h - $height) / 2;
      break;
    case 7:
      $wx = 0;
      $wy = $source_h - $height;
      break;
    case 8:
      $wx = ($source_w - $width) / 2;
      $wy = $source_h - $height;
      break;
    case 9:
      $wx = $source_w - $width;
      $wy = $source_h - $height;
      break;
    case 10:
      $wx = rand(0,($source_w - $width));
      $wy = rand(0,($source_h - $height));
      break;       
    default:
      $wx = rand(0,($source_w - $width));
      $wy = rand(0,($source_h - $height));
      break;
  }
  if($ifwaterimage) {
    if($water_info[2] == 3) {
      imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
    } else {
      imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
    }
  } else {
    if(!empty($w_color) && (strlen($w_color)==7)) {
      $r = hexdec(substr($w_color,1,2));
      $g = hexdec(substr($w_color,3,2));
      $b = hexdec(substr($w_color,5));
    } else {
      return;
    }
    imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  }
  
  switch($source_info[2]) {
    case 1 :
      imagegif($source_img, $target);
      break;
    case 2 :
      imagejpeg($source_img, $target, $this->w_quality);
      break;
    case 3 :
      imagepng($source_img, $target);
      break;
    default :
      return;
  }
 
  if(isset($water_info)) {
    unset($water_info);
  }
  if(isset($water_img)) {
    imagedestroy($water_img);
  }
  unset($source_info);
  imagedestroy($source_img);
  return true;
}
 
public function check($image) {
  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]));
}

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

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

if ($this->watermark) {

    $this->watermark($this->filePath,$this->filePath);

}

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

/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
  case 'uploadimage':
    $config = array(
      "pathFormat" => $CONFIG['imagePathFormat'],
      "maxSize" => $CONFIG['imageMaxSize'],
      "allowFiles" => $CONFIG['imageAllowFiles']
    );
    $fieldName = $CONFIG['imageFieldName'];
    $watermark = true;
    break;

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

/* 生成上传实例对象并完成上传 */

$up = new Uploader($fieldName, $config, $base64, $watermark);

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

以上所述就是本文的全部内容了,希望大家能够喜欢,能够对大家使用ue编辑器有所帮助。

PHP 相关文章推荐
PHP开发大型项目的一点经验
Oct 09 PHP
在字符串中把网址改成超级链接
Oct 09 PHP
php5新改动之短标记启用方法
Sep 11 PHP
PHP下使用CURL方式POST数据至API接口的代码
Feb 14 PHP
PHP中feof()函数实例测试
Aug 23 PHP
PHP中的类型提示(type hinting)功能介绍
Jul 01 PHP
php实现在站点里面添加邮件发送的功能
Apr 28 PHP
php实现的统计字数函数定义与使用示例
Jul 26 PHP
php实现的中秋博饼游戏之掷骰子并输出结果功能详解
Nov 06 PHP
PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析
May 11 PHP
PHP实现打包zip并下载功能
Jun 12 PHP
PHP7生产环境队列Beanstalkd用法详解
May 19 PHP
php+ajax实现的点击浏览量加1
Apr 16 #PHP
PHP中curl_setopt函数用法实例分析
Apr 16 #PHP
微信公众平台开发实现2048游戏的方法
Apr 15 #PHP
PHP获取毫秒级时间戳的方法
Apr 15 #PHP
基于php的微信公众平台开发入门实例
Apr 15 #PHP
微信公众平台实现获取用户OpenID的方法
Apr 15 #PHP
php实现格式化多行文本为Js可用格式
Apr 15 #PHP
You might like
调频问题解答
2021/03/01 无线电
MySQL相关说明
2007/01/15 PHP
PHP+FLASH实现上传文件进度条相关文件 下载
2007/07/21 PHP
PHP查看当前变量类型的方法
2015/07/31 PHP
php基于PDO实现功能强大的MYSQL封装类实例
2017/02/27 PHP
php往mysql中批量插入数据实例教程
2018/12/12 PHP
PHP CURL使用详解
2019/03/21 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
2019/11/26 PHP
基于PHP实现邮箱验证激活过程详解
2020/10/28 PHP
用javascript连接access数据库的方法
2006/11/17 Javascript
jQuery 行背景颜色的交替显示(隔行变色)实现代码
2009/12/13 Javascript
JavaScript调用客户端Java程序的方法
2015/07/27 Javascript
Bootstrap菜单按钮及导航实例解析
2016/09/09 Javascript
动态JavaScript所造成一些你不知道的危害
2016/09/25 Javascript
详解axios在vue中的简单配置与使用
2017/05/10 Javascript
vue组件Prop传递数据的实现示例
2017/08/17 Javascript
详解JS数值Number类型
2018/02/07 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
python写xml文件的操作实例
2014/10/05 Python
python实现TCP服务器端与客户端的方法详解
2015/04/30 Python
python如何实现excel数据添加到mongodb
2015/07/30 Python
Python Socket编程详细介绍
2017/03/23 Python
ubuntu安装mysql pycharm sublime
2018/02/20 Python
Python中staticmethod和classmethod的作用与区别
2018/10/11 Python
python使用PIL模块获取图片像素点的方法
2019/01/08 Python
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
2019/09/16 Python
浅谈python元素如何去重,去重后如何保持原来元素的顺序不变
2020/02/28 Python
Python爬虫实现百度翻译功能过程详解
2020/05/29 Python
Python如何读写字节数据
2020/08/05 Python
2014年幼儿园重阳节活动方案
2014/09/16 职场文书
公司保洁员岗位职责
2015/02/13 职场文书
2015上半年个人工作总结
2015/07/27 职场文书
大学生十八大感想
2015/08/11 职场文书
java代码实现空间切割
2022/01/18 Java/Android
SpringCloud Feign请求头删除修改的操作代码
2022/03/20 Java/Android
Nginx如何配置根据路径转发详解
2022/07/23 Servers