KindEditor编辑器上传图片添加水印功能
<?php
/**
* KindEditor PHP
*
* 本PHP程序是演示程序,建议不要直接在实际项目中使用。
* 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
*
*
*关于加水印的地方有几个地方要设置一下啊!!!!!!! 具体在下部分 自己找一下。
*
*/
require_once ‘JSON.php’;
$php_path = dirname(__FILE__) . ‘/’;
$php_url = dirname($_SERVER['PHP_SELF']) . ‘/’;
//文件保存目录路径
$save_path = $php_path . ‘../../attached/’;
//文件保存目录URL
$save_url = $php_url . ‘../../attached/’;
//定义允许上传的文件扩展名
$ext_arr = array(‘gif’, ‘jpg’, ‘jpeg’, ‘png’, ‘bmp’);
//最大文件大小
$max_size = 1000000;
$save_path = realpath($save_path) . ‘/’;
//有上传文件时
if (empty($_FILES) === false) {
//原文件名
$file_name = $_FILES['imgFile']['name'];
//服务器上临时文件名
$tmp_name = $_FILES['imgFile']['tmp_name'];
//文件大小
$file_size = $_FILES['imgFile']['size'];
$image_size = getimagesize($tmp_name);
//检查文件名
if (!$file_name) {
alert(“请选择文件。”);
}
//检查目录
if (@is_dir($save_path) === false) {
alert(“上传目录不存在。”);
}
//检查目录写权限
if (@is_writable($save_path) === false) {
alert(“上传目录没有写权限。”);
}
//检查是否已上传
if (@is_uploaded_file($tmp_name) === false) {
alert(“临时文件可能不是上传文件。”);
}
//检查文件大小
if ($file_size > $max_size) {
alert(“上传文件大小超过限制。”);
}
//获得文件扩展名
$temp_arr = explode(“.”, $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//检查扩展名
if (in_array($file_ext, $ext_arr) === false) {
alert(“上传文件扩展名是不允许的扩展名。”);
}
//创建文件夹
$ymd = date(“Ymd”);
$save_path .= $ymd . “/”;
$save_url .= $ymd . “/”;
if (!file_exists($save_path)) {
mkdir($save_path);
}
//新文件名
$new_file_name = date(“YmdHis”) . ‘_’ . rand(10000, 99999) . ‘.’ . $file_ext;
//移动文件
$file_path = $save_path . $new_file_name;
if (move_uploaded_file($tmp_name, $file_path) === false) {
alert(“上传文件失败。”);
}
//@chmod($file_path, 0644);
$file_url = $save_url . $new_file_name;
$watermark=1; //是否附加水印(1为加水印,其他为不加水印);
$watertype=2; //水印类型(1为文字,2为图片)
$waterstring=”http://www.vedeng.com/”; //水印字符串
$waterimg=”http://bj.com/ved/mark.gif”; //水印图片,试了相对路不行,只好用绝对路径。
if($watermark==1)
{
$myfilename=”../../attached/”.$ymd.”/”.$new_file_name;
$iinfo=getimagesize($myfilename,$iinfo);
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
$white=imagecolorallocate($nimage,255,255,255);
$black=imagecolorallocate($nimage,0,0,0);
$red=imagecolorallocate($nimage,255,0,0);
imagefill($nimage,0,0,$white);
switch ($iinfo[2])
{
case 1:
$simage =imagecreatefromgif($myfilename);
break;
case 2:
$simage =imagecreatefromjpeg($myfilename);
break;
case 3:
$simage =imagecreatefrompng($myfilename);
break;
case 6:
$simage =imagecreatefromwbmp($myfilename);
break;
default:
die(“不支持的文件类型”);
exit;
}
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
switch($watertype)
{
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
break;
case 2: //加水印图片
$simage1 =imagecreatefromgif($waterimg);
imagecopymerge($nimage,$simage1,$image_size[0]-150,$image_size[1]-50,0,0,150,50,60); //设置水印的位置
imagedestroy($simage1);
break;
}
switch ($iinfo[2])
{
case 1:
//imagegif($nimage, $destination);
imagejpeg($nimage, $myfilename);
break;
case 2:
imagejpeg($nimage, $myfilename);
break;
case 3:
imagepng($nimage, $myfilename);
break;
case 6:
imagewbmp($nimage,$myfilename);
//imagejpeg($nimage, $destination);
break;
}
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
header(‘Content-type: text/html; charset=UTF-8′);
$json = new Services_JSON();
echo $json->encode(array(‘error’ => 0, ‘url’ => $file_url));
exit;
}
function alert($msg) {
header(‘Content-type: text/html; charset=UTF-8′);
$json = new Services_JSON();
echo $json->encode(array(‘error’ => 1, ‘message’ => $msg));
exit;
}
?>
Tags: KindEditor 图片水印 水印功能
MFWv04 , [url=http://gnjzzvswhiyt.com/]gnjzzvswhiyt[/url], [link=http://hymydxvicbzt.com/]hymydxvicbzt[/link], http://pvtgyftulflx.com/
kkYP7o ffixhgqvtroy
THX that’s a great asewnr!
Thanky Thanky for all this good inoframtoin!
tA8Jzc , [url=http://nkwomgojwwgf.com/]nkwomgojwwgf[/url], [link=http://mxeiohvsbbqm.com/]mxeiohvsbbqm[/link], http://qynpbqyubgdi.com/
4okFJs kagekwnyjbtt
Always a good job right here. Keep roillng on through.
If you’re reading this, you’re all set, panrder!
Boy that relaly helps me the heck out.
This was so hpeulfl and easy! Do you have any articles on rehab?
Gee whiz, and I tohhugt this would be hard to find out.
Back in school, I’m doing so much lenaring.
3vTDuE dnjwzjqdfadd
WJho2d , [url=http://tcrtrqdndalz.com/]tcrtrqdndalz[/url], [link=http://mtmmpgukuoli.com/]mtmmpgukuoli[/link], http://vxktavovicnt.com/