kindeditor编辑器图片水印
//upload_pic.ashx源码
<%@ webhandler Language="C#" class="edit_html_upload_pic" %>
using System;
using System.Collections;
using System.Web;
using System.IO;
using System.Globalization;
using LitJson;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
public class edit_html_upload_pic : IHttpHandler, IRequiresSessionState
{
private HttpContext context; public void ProcessRequest(HttpContext context)
{
String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1); //每个用户文件夹独立IRequiresSessionState
String sessionUploadPath = String.Empty;
if (HttpContext.Current.Session["edit_upload_folder"] != null)
{
sessionUploadPath = HttpContext.Current.Session["edit_upload_folder"].ToString() + "/";
} //文件保存目录路径
String savePath = "../../" + getConfigAppSettings("uploadFolder") + "/" + sessionUploadPath;
//文件保存目录URL
String saveUrl = aspxUrl + "../../" + getConfigAppSettings("uploadFolder") + "/" + sessionUploadPath; //定义允许上传的文件扩展名
Hashtable extTable = new Hashtable();
extTable.Add("image", "gif,jpg,jpeg,png,bmp");
extTable.Add("flash", "swf,flv");
extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); //最大文件大小
int maxSize = 2000000;//2m
this.context = context; HttpPostedFile imgFile = context.Request.Files["imgFile"];
if (imgFile == null)
{
showError("请选择文件。");
} String dirPath = context.Server.MapPath(savePath);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} String dirName = context.Request.QueryString["dir"] != null ? context.Request.QueryString["dir"] : "image";
String fileName = imgFile.FileName;
String fileExt = Path.GetExtension(fileName).ToLower(); if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
{
showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
}
if (dirName == "image")
{
if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
{
showError("上传文件大小超过限制。");
}
if (!isPic(imgFile))
{
showError("上传文件不合法!");
}
} //创建文件夹
//dirPath += dirName + "/";
//saveUrl += dirName + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
String ymd = DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + "/";
saveUrl += ymd + "/";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
} //edit_upload_fileName
//string sessionUploadFileName = string.Empty;
//if (HttpContext.Current.Session["edit_upload_fileName"] != null)
//{
// sessionUploadFileName = HttpContext.Current.Session["edit_upload_fileName"].ToString();
//}
//String newFileName = sessionUploadFileName + DateTime.Now.ToString("yyyyMMddHHmmssffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
//String filePath = dirPath + newFileName;
//imgFile.SaveAs(filePath);
//String fileUrl = saveUrl + newFileName; //水印start
String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo);
String filePath = dirPath + newFileName + fileExt;
imgFile.SaveAs(filePath);
String fileUrl = saveUrl + newFileName + fileExt;
//添加文字水印
Image image = System.Drawing.Image.FromFile(filePath);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 14);
Brush b = new SolidBrush(Color.Red);
string addText = "www.114390.com";
g.DrawString(addText, f, b, 10, 10);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = dirPath + newFileName + "_new" + fileExt;
image.Save(newPath);
image.Dispose();
if (File.Exists(filePath))
{
File.Delete(filePath);
}
fileUrl = saveUrl + newFileName + "_new" + fileExt;
//水印end Hashtable hash = new Hashtable();
hash["error"] = 0;
hash["url"] = fileUrl;
context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
context.Response.Write(JsonMapper.ToJson(hash));
context.Response.End();
}
private string getConfigAppSettings(string keyName)
{
if (!string.IsNullOrEmpty(keyName))
{
return System.Configuration.ConfigurationManager.AppSettings[keyName];
}
return "";
}
private void showError(string message)
{
Hashtable hash = new Hashtable();
hash["error"] = 1;
hash["message"] = message;
context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
context.Response.Write(JsonMapper.ToJson(hash));
context.Response.End();
} public bool IsReusable
{
get
{
return true;
}
}
public bool isPic(HttpPostedFile imgFile)
{
int fileLen=imgFile.ContentLength;
byte[] imgArray = new byte[fileLen];
imgFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
BinaryReader br = new BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch
{ }
br.Close();
ms.Close();
//
try
{
//读取上传的文件是否是图片对象,异常则删除图片
System.Drawing.Image uploadedImg = System.Drawing.Image.FromStream(imgFile.InputStream);
if (uploadedImg.Width < 1 || uploadedImg.Height < 1)
{
return false;
}
uploadedImg.Dispose();
}
catch
{
return false;
}
//
FileExtension[] fileEx = { FileExtension.GIF, FileExtension.BMP, FileExtension.JPG, FileExtension.PNG};
foreach (FileExtension fe in fileEx)
{
try
{
if (Int32.Parse(fileclass) == (int)fe)
{
if (truePic(System.Text.Encoding.ASCII.GetString(imgArray)))
{
return true;
}
return false;
}
}
catch
{ } }
return false;
}
//判断上传文件中是否包含关键字
public bool truePic(string str)
{
string sStr = ".getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=|exec |insert |select |delete |update |truncate |declare |iframe |Response|Request(| Eval|Eval |Eval(|%Eval|script |using";
string[] ck = sStr.Split('|');
string strsql = str;
for (int i = 0; i < ck.Length; i++)
{
if (str.IndexOf(ck[i]) != -1 || str.ToUpper().IndexOf(ck[i].ToUpper()) != -1)
{
return false;
}
}
return true;
}
//文件类型
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
BMP = 6677,
SWF = 6787,
SWF2 = 7087,
RAR = 8297,
ZIP = 8075,
DOC = 208207,
DOCX = 8075,
XLS = 208207,
XLS2 = 198243,
XLSX = 8075,
//_7Z = 55122,
// 255216 jpg;
// 7173 gif;
// 6677 bmp,
// 13780 png;
// 6787 swf
// 7790 exe dll,
// 8297 rar
// 8075 zip
// 55122 7z
// 6063 xml
// 6033 html
// 239187 aspx
// 117115 cs
// 119105 js
// 102100 txt
// 255254 sql
/*
DOC = 208207,
DOCX = 8075,
XLS = 208207,
XLSX = 8075,
JS = 239187,
TXT = 7067,
MP3 = 7368,
WMA = 4838,
MID = 7784,
*/
}
}
kindeditor编辑器图片水印的更多相关文章
- kindeditor编辑器和图片上传独立分开的配置细节
关于kindeditor编辑器上传按钮的异步加载最关键的部署问题,它的上传图片的组件都已经封装得很好了的,只需要监听到页面按钮的点击事件给编辑器对象传递一些对应的初始化参数即可显示图片上传的弹窗实现异 ...
- Django项目开发,XSS攻击,图片防盗链,图片验证码,kindeditor编辑器
目录 一.Django项目开发 1. 项目开发流程 2. auth模块的补充 (1)django的admin可视化管理页面 (2)将admin可视化管理页面的模型表显示成中文 (3)auth模块的用户 ...
- 如何在一个页面添加多个不同的kindeditor编辑器
kindeditor官方下载地址:http://kindeditor.net/down.php (入门必看)kindeditor官方文档:http://kindeditor.net/doc.ph ...
- KindEditor编辑器在ASP.NET中的使用
KindEditor编辑器在ASP.NET中的使用 最近做的项目中都有用到富文本编辑器,一直在寻找最后用的富文本编辑器,之前用过CKEditor,也用过UEditor,这次打算用 一下KindEdit ...
- 如何自定义kindeditor编辑器的工具栏items即去除不必要的工具栏或者保留部分工具栏
kindeditor编辑器的工具栏主要是指编辑器输入框上方的那些可以操作的菜单,默认情况下编辑器是给予了所有的工具栏.针对不同的用户,不同的项目,不同的环境,可能就需要保留部分工具栏.那么我们应该如何 ...
- kindeditor编辑器
一 简单使用方法 1. 把所有文件上传到程序所在目录下,例如:http://你的域名/editor/. 2. 在此目录下创建attached文件夹,并把权限改成777. 3. 要添加编辑器的地方加入以 ...
- 详细介绍如何使用kindEditor编辑器
今天群里的朋友问我能不能写个kindEditor编辑器的使用教程,说是弄了半天没有搞定.由于PHP啦后台正好用了这个编辑器,我有写经验,正好教他的同时写出来分享给大家. kindEditor编辑器是一 ...
- ThinPHP第二十八天(F函数和file_put_contents区别|PHP生成PHP文件,Kindeditor编辑器使用方法)
1.F(name,data,path)函数和file_put_contents(file,str)区别 F函数直接生成<?php ?>格式的php文件了,将data加入到<?php和 ...
- kindeditor修改图片上传路径-使用webapi上传图片到图片服务器
kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...
随机推荐
- sqlserver insert into select
Insert into [fenxi].[dbo].[analysisresult]( [dayofweek] ,[quarter] ,[reporttime] ,[type] ,[value]) s ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- sysconf和pathconf使用
问题描述: 查看系统运行时的限制值 问题解决: 执行效果: 源代码:
- struts2多文件上传(带进度条)demo+说明
利用plupload插件实现多文件上传,实现图片: 在jsp写入js代码: z<%@ page language="java" contentType="text/ ...
- javascript数学计算
◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...
- POJ2480 Longge's problem gcd&&phi
题意简洁明了.做这题主要是温习一下phi的求法.令gcd(i,n)=k,实际上我们只需要求出有多少个i使得gcd(i,n)=k就可以了,然后就转化成了求phi(n/k)的和,但是n很大,我们不可能预处 ...
- HDU 4509 湫湫系列故事——减肥记II(暴力模拟即可)
看了题目后,没自己做,直接看别人题解了,这里转一下. 看了之后,突然想起scanf还可以按照自己写的格式输入数据啊,差点连这个都忘记了啊. 注意输入中时间可能有重复的. http://www.cnbl ...
- chmod命令详细用法
指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...
- 【POJ3243】拓展BSGS(附hash版)
上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...
- Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjav
在eclipse上运行hadoop报错:Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.ha ...