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 2008r2 表分区拆分问题
问题描述: 对tab1表实行自动天分区,每天通过sqlserver代理-作业00:00点自动生成分区: 因有次停了数据库服务,sqlserver 代理服务忘记开启,导致当天00:00没有生成新分区: ...
- 3139:[HNOI2013]比赛 - BZOJ
题目描述 Description 沫沫非常喜欢看足球赛,但因为沉迷于射箭游戏,错过了最近的一次足球联赛.此次联赛共N只队伍参加,比赛规则如下: (1) 每两支球队之间踢一场比赛. (2) 若平局,两支 ...
- mysql memory
mysql memory engine 创建: mysql> create table mt engine = memory select * from information_schema.t ...
- plsql 使用技巧
问题1: 每次打开plsql 布局都被恢复还原了. 你可以调整好一个布局,然后window ->Save Lay out 即可
- sigleSchool 存储过程例1
CREATE OR REPLACE PROCEDURE SINGLSCHOOL( PICIID IN VARCHAR2, SCHOOLID IN NUMBER, SCHETYPE IN number, ...
- POJ2406 Power Strings KMP算法
给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的 譬 ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- HDU 4143 A Simple Problem(枚举)
题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...
- hdu 4704 Sum
思路:对于给定的n,s(i)即将n分解为i个数的组合数,也就是在n-1个位置插入i-1个板即C(n-1,i-1); ∑S=2^(n-1); phi(1000000007)=1000000006; 对于 ...
- ubuntu12.10+NDK r9 编译 ffmpeg 的一些参考资料Perhaps you should add the directory containing `libssl.pc'
首先入门级的 编译宝典: https://trac.ffmpeg.org/wiki/CompilationGuide/Android http://www.roman10.net/how-to-bui ...