1.引入Ueditor
1 <!-- 实例化编辑器 --> 2 <script type="text/javascript"> 3 window.UEDITOR_HOME_URL ="/Data/ueditor/"; 4 window.onload=function(){ 5 window.UEDITOR_CONFIG.initialFrameHeight=300; 6 //window.UEDITOR_CONFIG.initialFrameWidth=1000; 7 UE.getEditor('content'); 8 } 9 </script> 10 <!-- 配置文件 --> 11 <script type="text/javascript" charset="utf-8" src="/Data/ueditor/ueditor.config.js"></script> 12 <!-- 编辑器源码文件 --> 13 <script type="text/javascript" charset="utf-8" src="/Data/ueditor/ueditor.all.js"> </script>
2.加入水印处理类 (随便找了一个)
1 <?php 2 /** 3 * 加水印类,支持文字图片水印的透明度设置、水印图片背景透明。 4 * 日期:2011-09-27 5 * 作者:www.jb51.net 6 * 使用: 7 * $obj = new WaterMask($imgFileName); //实例化对象 8 * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印 9 * $obj->$transparent = 45; //水印透明度 10 * $obj->$waterStr = 'www.jb51.net'; //水印文字 11 * $obj->$fontSize = 16; //文字字体大小 12 * $obj->$fontColor = array(255,0255); //水印文字颜色(RGB) 13 * $obj->$fontFile = = 'AHGBold.ttf'; //字体文件 14 * $obj->output(); //输出水印图片文件覆盖到输入的图片文件 15 */ 16 class WaterMask { 17 public $waterType = 1; //水印类型:0为文字水印、1为图片水印 18 public $pos = 0; //水印位置 19 public $transparent = 45; //水印透明度 20 public $waterStr = 'www.jb51.net'; //水印文字 21 public $fontSize = 16; //文字字体大小 22 public $fontColor = array( 23 255, 24 0, 25 255 26 ); //水印文字颜色(RGB) 27 public $fontFile = 'AHGBold.ttf'; //字体文件 28 public $waterImg = 'logo.png'; //水印图片 29 private $srcImg = ''; //需要添加水印的图片 30 private $im = ''; //图片句柄 31 private $water_im = ''; //水印图片句柄 32 private $srcImg_info = ''; //图片信息 33 private $waterImg_info = ''; //水印图片信息 34 private $str_w = ''; //水印文字宽度 35 private $str_h = ''; //水印文字高度 36 private $x = ''; //水印X坐标 37 private $y = ''; //水印y坐标 38 function __construct($img) { //析构函数 39 $this->srcImg = file_exists($img) ? $img : die('"' . $img . '" 源文件不存在!'); 40 } 41 private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。 42 $this->srcImg_info = getimagesize($this->srcImg); 43 switch ($this->srcImg_info[2]) { 44 case 3: 45 $this->im = imagecreatefrompng($this->srcImg); 46 break 1; 47 case 2: 48 $this->im = imagecreatefromjpeg($this->srcImg); 49 break 1; 50 case 1: 51 $this->im = imagecreatefromgif($this->srcImg); 52 break 1; 53 default: 54 die('原图片(' . $this->srcImg . ')格式不对,只支持PNG、JPEG、GIF。'); 55 } 56 } 57 private function waterimginfo() { //获取水印图片的信息,并载入图片。 58 $this->waterImg_info = getimagesize($this->waterImg); 59 switch ($this->waterImg_info[2]) { 60 case 3: 61 $this->water_im = imagecreatefrompng($this->waterImg); 62 break 1; 63 case 2: 64 $this->water_im = imagecreatefromjpeg($this->waterImg); 65 break 1; 66 case 1: 67 $this->water_im = imagecreatefromgif($this->waterImg); 68 break 1; 69 default: 70 die('水印图片(' . $this->waterImg . ')格式不对,只支持PNG、JPEG、GIF。'); 71 } 72 } 73 private function waterpos() { //水印位置算法 74 switch ($this->pos) { 75 case 0: //随机位置 76 $this->x = rand(0, $this->srcImg_info[0] - $this->waterImg_info[0]); 77 $this->y = rand(0, $this->srcImg_info[1] - $this->waterImg_info[1]); 78 break 1; 79 case 1: //上左 80 $this->x = 0; 81 $this->y = 0; 82 break 1; 83 case 2: //上中 84 $this->x = ($this->srcImg_info[0] - $this->waterImg_info[0]) / 2; 85 $this->y = 0; 86 break 1; 87 case 3: //上右 88 $this->x = $this->srcImg_info[0] - $this->waterImg_info[0]; 89 $this->y = 0; 90 break 1; 91 case 4: //中左 92 $this->x = 0; 93 $this->y = ($this->srcImg_info[1] - $this->waterImg_info[1]) / 2; 94 break 1; 95 case 5: //中中 96 $this->x = ($this->srcImg_info[0] - $this->waterImg_info[0]) / 2; 97 $this->y = ($this->srcImg_info[1] - $this->waterImg_info[1]) / 2; 98 break 1; 99 case 6: //中右 100 $this->x = $this->srcImg_info[0] - $this->waterImg_info[0]; 101 $this->y = ($this->srcImg_info[1] - $this->waterImg_info[1]) / 2; 102 break 1; 103 case 7: //下左 104 $this->x = 0; 105 $this->y = $this->srcImg_info[1] - $this->waterImg_info[1]; 106 break 1; 107 case 8: //下中 108 $this->x = ($this->srcImg_info[0] - $this->waterImg_info[0]) / 2; 109 $this->y = $this->srcImg_info[1] - $this->waterImg_info[1]; 110 break 1; 111 default: //下右 112 $this->x = $this->srcImg_info[0] - $this->waterImg_info[0]; 113 $this->y = $this->srcImg_info[1] - $this->waterImg_info[1]; 114 break 1; 115 } 116 } 117 private function waterimg() { 118 if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]) { 119 die('水印比原图大!'); 120 } 121 $this->waterpos(); 122 $cut = imagecreatetruecolor($this->waterImg_info[0], $this->waterImg_info[1]); 123 imagecopy($cut, $this->im, 0, 0, $this->x, $this->y, $this->waterImg_info[0], $this->waterImg_info[1]); 124 $pct = $this->transparent; 125 imagecopy($cut, $this->water_im, 0, 0, 0, 0, $this->waterImg_info[0], $this->waterImg_info[1]); 126 imagecopymerge($this->im, $cut, $this->x, $this->y, 0, 0, $this->waterImg_info[0], $this->waterImg_info[1], $pct); 127 } 128 private function waterstr() { 129 $rect = imagettfbbox($this->fontSize, 0, $this->fontFile, $this->waterStr); 130 $w = abs($rect[2] - $rect[6]); 131 $h = abs($rect[3] - $rect[7]); 132 $fontHeight = $this->fontSize; 133 $this->water_im = imagecreatetruecolor($w, $h); 134 imagealphablending($this->water_im, false); 135 imagesavealpha($this->water_im, true); 136 $white_alpha = imagecolorallocatealpha($this->water_im, 255, 255, 255, 127); 137 imagefill($this->water_im, 0, 0, $white_alpha); 138 $color = imagecolorallocate($this->water_im, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]); 139 imagettftext($this->water_im, $this->fontSize, 0, 0, $this->fontSize, $color, $this->fontFile, $this->waterStr); 140 $this->waterImg_info = array( 141 0 => $w, 142 1 => $h 143 ); 144 $this->waterimg(); 145 } 146 function output() { 147 $this->imginfo(); 148 if ($this->waterType == 0) { 149 $this->waterstr(); 150 } else { 151 $this->waterimginfo(); 152 $this->waterimg(); 153 } 154 switch ($this->srcImg_info[2]) { 155 case 3: 156 imagepng($this->im, $this->srcImg); 157 break 1; 158 case 2: 159 imagejpeg($this->im, $this->srcImg); 160 break 1; 161 case 1: 162 imagegif($this->im, $this->srcImg); 163 break 1; 164 default: 165 die('添加水印失败!'); 166 break; 167 } 168 imagedestroy($this->im); 169 imagedestroy($this->water_im); 170 } 171 } 172 ?>
3.在项目的Applacation 中添加config.json 文件 进行对添加水印配置:
1 /* 前后端通信相关的配置,注释只允许使用多行方式 */ 2 { 3 "waterType":1,/*类型:0为文字水印、1为图片水印*/ 4 "waterImg":"http://127.0.0.8/data/Logo/Logo.png",/*水印图片*/ 5 "waterStr":"www.smartop.xyz",/*水印文字*/ 6 "fontSize":16,/*文字字体大小*/ 7 "fontColor":[0,0,0],/*水印文字颜色(RGB)*/ 8 "fontFile":"./../../Font/ebrimabd.ttf",/*字体文件*/ 9 "transparent":45,/*水印透明度*/ 10 "pos":7,/*水印位置支持 1 2 3 4 5 6 7 8 9 九宫格*/ 11 "imagePathFormat": "/upload/{yyyy}{mm}{dd}/{time}{rand:6}" /* 上传保存路径,可以自定义保存路径和文件名格式 */ 12 /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */ 13 /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */ 14 /* {time} 会替换成时间戳 */ 15 /* {yyyy} 会替换成四位年份 */ 16 /* {yy} 会替换成两位年份 */ 17 /* {mm} 会替换成两位月份 */ 18 /* {dd} 会替换成两位日期 */ 19 /* {hh} 会替换成两位小时 */ 20 /* {ii} 会替换成两位分钟 */ 21 /* {ss} 会替换成两位秒 */ 22 /* 非法字符 : * ? " < > | */ 23 /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */ 24 }
4.修改controller.php入口文件
将自己的config.json自己的配置加入到配置里面
1 $CONFIG = json_decode(preg_replace("//*[sS]+?*//", "", file_get_contents("config.json")), true); 2 $SELFCONFIG = json_decode(preg_replace("//*[sS]+?*//", "", file_get_contents("./../../../../Application/Conf/config.json")), true); 3 $CONFIG = array_merge($CONFIG,$SELFCONFIG);
5.配置:action_upload.php 将自定义配置项写入:
1 "waterType" => $CONFIG['waterType'], 2 "waterImg" => $CONFIG['waterImg'], 3 "waterStr" => $CONFIG['waterStr'], 4 "fontSize" => $CONFIG['fontSize'], 5 "fontColor" => $CONFIG['fontColor'], 6 "fontFile" => $CONFIG['fontFile'], 7 "transparent" => $CONFIG['transparent'], 8 "pos" => $CONFIG['pos'],
6.配置:Uploader.class.php 增加一个水印处理的私有方法:
1 /** 2 * 添加水印 3 */ 4 private function water(){ 5 require_once('Water.class.php'); 6 $obj = new WaterMask($this->filePath); //实例化对象 7 if($this->config['waterType']){ 8 $obj->waterType = $this->config['waterType']; 9 $obj->waterImg = $this->config['waterImg']; //水印图片 10 }else{ 11 $obj->waterType = $this->config['waterType']; 12 $obj->waterStr = $this->config['waterStr'];//水印文字 13 $obj->fontSize = $this->config['fontSize']; //文字字体大小 14 $obj->fontColor = $this->config['fontColor']; //水印文字颜色(RGB) 15 $obj->fontFile = $this->config['fontFile']; //字体文件 16 } 17 $obj->transparent = $this->config['transparent']; //水印透明度 18 $obj->pos = $this->config['pos']; //水印位置 19 $obj->output(); //输出水印图片文件覆盖到输入的图片文件 20 }