百度编辑器本身是没有为上传图片添加水印的功能,想要在上传的时候添加图片水印,也很简单。以 ueditor 1.2.6 为例,打开php目录下面的imageUp.php文件,查找“$info = $up->getFileInfo();”,在这句代码的下面加入以下代码:
/* 添加水印 start */
$water_img = "watermark.png"; //水印文件(替这里换成你要的水印)
$img_min_w = 350; //添加水印需要图片最小达到的宽度(px),小于这个宽度时则不添加水印
$img_min_h = 60; //添加水印需要图片最小达到的高度(px)
$new_img = $ori_img = $info["url"];
$original = getimagesize($ori_img); //得到图片的信息,可以print_r($original)发现它就是一个数组
if($original[0]>$img_min_w&&$original[1]>$img_min_h){ //当图片宽度大于300px才添加水印
$watermark = getimagesize($water_img);
$s_original = imgCreateFrom($ori_img, $original[2]); //$original[2]是图片类型,其中1表示gif、2表示jpg、3表示png
$s_watermark = imgCreateFrom($water_img, $watermark[2]);
$posX = $original[0] - $watermark[0]-6; //X坐标(右下角)
$poxY = $original[1] - $watermark[1]-3; //Y坐标(右下角)
//打水印
imagecopy($s_original, $s_watermark, $posX, $poxY, 0, 0, $watermark[0], $watermark[1]);
imagejpeg($s_original, $new_img); //生成新的图片
}
//根据文件类型 创建一个新图象
function imgCreateFrom($img_src, $val){
switch($val){
case 1 : $img = imagecreatefromgif($img_src);
break;
case 2 : $img = imagecreatefromjpeg($img_src);
break;
case 3 : $img = imagecreatefrompng($img_src);
break;
}
return $img;
}
/* 添加水印 end */
然后保存,就可以了。不过要注意的是:$water_img = "watermark.png";//水印文件(替这里换成你要的水印)。
也可以加文字水印
$text1 = ‘中文11‘;
//echo $text1;
//$text1 = iconv("GB2312","UTF-8//IGNORE",$text1); //防止中文乱码
//echo ‘after‘.$text1;
$size = 20; //文字大小
$font = $_SERVER[‘DOCUMENT_ROOT‘]."/themes/Static/images/simfang.ttf"; //字体
$angle = 0; //旋转角度,逆时针
$wmtext->GetWpText($text1);
$wmtext->GetFtSize($size);
$wmtext->GetFtType($font);
$wmtext->GetTtAngle($angle);
$wmtext->AddWpText($info["url"]);// 给图片添加文字水印*/
class WaterMark
{
//类开始
public $text, $color, $size, $font, $angle, $px, $py, $im;
//要添加的文字
public function GetWpText($text)
{
$this->text = $text;
}
//添加文字的颜色
public function GetFtColor($color)
{
$this->color = $color;
}
//添加文字的字体
public function GetFtType($font)
{
$this->font = $font;
}
//添加文字的大小
public function GetFtSize($size)
{
$this->size = $size;
}
//文字旋转的角度
public function GetTtAngle($angle)
{
$this->angle = $angle;
}
//添加文字的位置
public function GetTtPosit()
{
//echo ‘here‘.strlen($this->text);
$this->px = imagesx($this->im) - strlen($this->text)*10;
$this->py = imagesy($this->im) - 20;
}
static function getImageInfo($img) {
$imageInfo = getimagesize($img);
if ($imageInfo !== false) {
$imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
$imageSize = filesize($img);
$info = array(
"width" => $imageInfo[0],
"height" => $imageInfo[1],
"type" => $imageType,
"size" => $imageSize,
"mime" => $imageInfo[‘mime‘]
);
return $info;
} else {
return false;
}
}
//添加文字水印
public function AddWpText($pict)
{
//$ext = exif_imagetype($pict);
$sInfo = self::getImageInfo($pict);
switch ($sInfo[‘type‘]) {
case 1:
$picext = "gif";
$this->im = imagecreatefromgif($pict);
break;
case "jpeg":
$picext = "jpeg";
$this->im = imagecreatefromjpeg($pict);
break;
case 3:
$picext = "png";
$this->im = imagecreatefrompng($pict);
break;
default:
$this->Errmsg("不支持的文件格式!");
break;
}
//$this->picext = $picext;
$this->GetTtPosit();
$im = $this->im;
$size = $this->size;
$angle= $this->angle;
$px = $this->px;
$py = $this->py;
$color= $this->color;
$font = $this->font;
$text = $this->text;
$color= imagecolorallocate($im, 255, 0, 0);
//echo $picext.$im.‘===‘.$pict;
//echo $size.‘ ‘.$angle.‘ ‘.px.‘ ‘.$py.‘ ‘.$color.‘ ‘.$font.‘ ‘.$text.‘end‘;
imagettftext($im, $size, $angle, $px, $py, $color, $font, $text);
switch ($picext) {
case "gif":
imagegif($im, $pict);
break;
case "jpeg":
imagejpeg($im, $pict, 100);
break;
case "png":
imagealphablending($im, false);
imagesavealpha($im, true);
imagepng($im, $pict);
break;
}
imagedestroy($im);
}
//错误信息提示
public function Errmsg($msg)
{
echo "<script language=‘javascript‘>alert(‘".$msg."‘);</script>";
}
//类结束
}
原文:http://blog.csdn.net/u010367506/article/details/28095537