PHPCMS V9整合百度编辑器Ueditor上传图片添加水印功能(实测版)

UEditor编辑器是一款不错的免费编辑器, 功能越来越强大, 用户体验良好, 但是有些功能需要自己定制一下, 如上传图片加水印功能。
下面的一些代码经过本人实测可行, 先奉献给大家:

一: UEditor编辑器文件目录的php文件夹,打开Uploader.class.php,如我的文件是../ueditor/php/Uploader.class.php, 复制PHPCMS V9系统里加水印的方法, 添加到底部, 修改如下:
/**
* 图片打水印
* @return bool
*/
public function waterMark($source, $target = ”, $w_pos = ”, $w_img = ”, $w_text = ‘bluestep.cc’, $w_font = 8, $w_color = ‘#ff0000’) {
$this->w_img = ‘mark.png’;
$this->w_pos = 9;
$this->w_minwidth = 300;
$this->w_minheight = 150;
$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 = $_SERVER[‘DOCUMENT_ROOT’].’/statics/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, $_SERVER[‘DOCUMENT_ROOT’].’/phpcms/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;
}

二: 修改__construct函数如下:
/**
* 构造函数
* @param string $fileField 表单名称
* @param array $config 配置项
* @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
*/
public function __construct ($fileField, $config, $type = “upload”, $water) {
$this->fileField = $fileField;
$this->config = $config;
$this->type = $type;
$this->water = $water;
if ($type == “remote”) {
$this->saveRemote();
} else if($type == “base64”) {
$this->upBase64();
} else {
$this->upFile();
}

$this->stateMap[‘ERROR_TYPE_NOT_ALLOWED’] = iconv(‘unicode’, ‘utf-8’, $this->stateMap[‘ERROR_TYPE_NOT_ALLOWED’]);
}

三: 在upFile方法后面加如下代码:
//打水印
if ($this->water) {
$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);

亲测成功!

郑重声明:

1 本资源来源于互联网,资源的版权归资源原作者所持有,受《中华人民共和国著作权法》等相关法律保护。

2 由于无法和原作者取得联系,所以上传的部分资源无法先通过原作者的同意就分享给大家了,如本资源侵犯了您(原作者)的权益,请联系我们(微信号 xiaohaimei1989),我们会立马删除您的资源,并向您表达诚挚的歉意!

3 本站是一个公益型网站,分享资源的目的在于传播知识,分享知识,收取一点点打赏的辛苦费是用于网站的日常运营开支,并非用于商业用途。

4 本站资源只提供学习和参考研究使用,使用过后请在第一时间内删除。本站不承担资源被单位或个人商用带来的法律责任。

发表评论