为百度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
PHP 批量更新网页内容实现代码
Jan 05 PHP
关于PHPDocument 代码注释规范的总结
Jun 25 PHP
php输入流php://input使用示例(php发送图片流到服务器)
Dec 25 PHP
PHP实现Soap通讯的方法
Nov 03 PHP
php在linux下检测mysql同步状态的方法
Jan 15 PHP
PHP生成树的方法
Jul 28 PHP
PHP的命令行命令使用指南
Aug 18 PHP
php 利用array_slice函数获取随机数组或前几条数据
Sep 30 PHP
php+mysql实现的二级联动菜单效果详解
May 10 PHP
Yii2框架实现数据库常用操作总结
Feb 08 PHP
Laravel框架创建路由的方法详解
Sep 04 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
实用函数5
2007/11/08 PHP
yii框架中的Url生产问题小结
2012/01/16 PHP
PHP实现数组向任意位置插入,删除,替换数据操作示例
2019/04/05 PHP
php 使用expat方式解析xml文件操作示例
2019/11/26 PHP
简介alert()与console.log()的不同
2015/08/26 Javascript
学习javascript面向对象 javascript实现继承的方式
2016/01/04 Javascript
JavaScript中实现无缝滚动、分享到侧边栏实例代码
2016/04/06 Javascript
探讨AngularJs中ui.route的简单应用
2016/11/16 Javascript
利用Jquery实现几款漂亮实用的时间轴(附示例代码)
2017/02/15 Javascript
nodejs中全局变量的实例解析
2017/03/07 NodeJs
Node.js常用工具之util模块
2017/03/09 Javascript
JavaScript设计模式之代理模式详解
2017/06/09 Javascript
详解tween.js 中文使用指南
2018/01/05 Javascript
vue better-scroll插件使用详解
2018/01/25 Javascript
在 Linux/Unix 中不重启 Vim 而重新加载 .vimrc 文件的流程
2018/03/21 Javascript
node+koa2+mysql+bootstrap搭建一个前端论坛
2018/05/06 Javascript
从0到1搭建element后台框架优化篇(打包优化)
2019/05/12 Javascript
微信小程序开发摇一摇功能
2019/11/22 Javascript
Python构建XML树结构的方法示例
2017/06/30 Python
详解Django 中是否使用时区的区别
2018/06/14 Python
不管你的Python报什么错,用这个模块就能正常运行
2018/09/14 Python
Python批处理更改文件名os.rename的方法
2018/10/26 Python
python判断计算机是否有网络连接的实例
2018/12/15 Python
linux下安装python3和对应的pip环境教程详解
2019/07/01 Python
tensorflow指定GPU与动态分配GPU memory设置
2020/02/03 Python
python使用梯度下降算法实现一个多线性回归
2020/03/24 Python
PyCharm+Pipenv虚拟环境开发和依赖管理的教程详解
2020/04/16 Python
传统HTML页面实现模块化加载的方法
2018/10/15 HTML / CSS
Raleigh兰令自行车美国官网:英国凤头牌自行车
2018/01/08 全球购物
什么是用户模式(User Mode)与内核模式(Kernel Mode) ?
2015/09/07 面试题
个人贷款承诺书
2014/03/28 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
新郎结婚感言
2015/07/31 职场文书
Oracle数据库中通用的函数实例详解
2022/03/25 Oracle
DIY胆机必读:各国电子管评价
2022/04/06 无线电
Mysql中常用的join连接方式
2022/05/11 MySQL