主题: KE整合asp.net 图片上传,包含文字水印、图片水印、按比例缩放 |
作者: einsta, 发布日期: 2009-02-22 16:44:31, 浏览数: 13836 |
水印文字和水印图片要求在web.config中添加, <appSettings> <!-- 上传服务器端路径--> <add key="upPath" value="/kindeditor/attached/"/> <!-- 允许上传图片格式--> <add key="imgType" value=".jpg|.gif|.png|.bmp"/> <!-- 文字水印字符--> <add key="WaterText" value="武汉理工大学信息工程学院"/> <!-- 图片水印图片--> <add key="WaterImage" value="/kindeditor/wutinfo.gif"/> </appSettings> 首先是image.html,增加了点东西: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Image</title> <style type="text/css" rel="stylesheet"> body { font-size:12px; margin: 0px; background-color:#F0F0EE; overflow: hidden; } td.left1 { font-size:12px; width: 50px; padding: 2px; } td.right1 { font-size:12px; padding: 2px; } td.left2 { font-size:12px; width: 35px; padding: 2px; } td.right2 { font-size:12px; padding: 2px; width: 50px; } </style> <script type="text/javascript"> function changeType(obj) { if (obj.value == 1) { document.getElementById('url').style.display = 'none'; document.getElementById('imgFile').style.display = 'block'; document.getElementById('water').style.display='block'; } else { document.getElementById('url').style.display = 'block'; document.getElementById('imgFile').style.display = 'none'; document.getElementById('water').style.display='none'; } } </script> </head> <body> <form name="uploadForm" method="post" enctype="multipart/form-data" action="imgup.aspx"> <input type="hidden" id="editorId" name="id" value="" /><div id="processing" style="background-color: #F0F0EE;text-align: center; width: 330px;height: 207px;padding-top:30%;margin-top:0px;position:fixed!important;/*FF IE7*/position:absolute;/*IE6*/filter: Alpha(opacity=80);-moz-opacity:.8;opacity:0.8; font-weight:bold;display:none">RPOCESSING... <br /><img src="../skins/Loading.gif" border="0"/></div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td class="left1"> <select id="type" name="type" /> <option value="1" selected="selected">本地</option> <option value="2">远程</option> </select> </td> <td class="right1"> <input type="file" id="imgFile" name="imgFile" style="width:220px;" /> <input type="text" id="url" name="url" value="http://" maxlength="255" style="width:220px;display:none;" /> </td> </tr> <tr> <td class="left1">说明:</td> <td class="right1"> <input type="text" id="imgTitle" name="imgTitle" value="" maxlength="100" style="width:220px;" /> </td> </tr><tr id="water"> <td class="left1">水印</td> <td class="right1"> 文字水印<input type="checkbox" id="chkWater" name="chkWater" /> 图片水印<input type="checkbox" id="chkImageWater" name="chkImageWater" /> </td> </tr> </table> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td class="left2">宽度: </td> <td class="right2"> <input type="text" name="imgWidth" id="imgWidth" value="800" maxlength="4" style="width:40px;" /> </td> <td class="left2">高度: </td> <td class="right2"> <input type="text" name="imgHeight" id="imgHeight" value="0" maxlength="4" style="width:40px;" /> </td> <td class="left2">边框: </td> <td class="right2"> <input type="text" name="imgBorder" id="imgBorder" value="0" maxlength="1" style="width:40px;" /> </td> </tr> <tr> <td colspan=6>宽度为0表示默认,宽度与高度只需填写一个,系统会自动按比率缩放。 </td> </tr> </table> </form> </body> </html> 在 plugin-all.js中,找到KE.plugin['image']的exec方法, if (type == 1) { KE.$('editorId', dialogDoc).value = id; dialogDoc.getElementById('processing').style.display='block'; dialogDoc.uploadForm.submit(); return false; } 添加一行,dialogDoc.getElementById('processing').style.display='block'; imgup.aspx,建立就行,无需添加控件代码什么的。 imgup.aspx.cs //没C#的高亮选项,用C++顶一下吧 using System; using System.Drawing; using System.IO; public partial class aspx_imgUp : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //文件保存目录路径 string SavePath = getConfig("upPath"); //上传图片类型 string[] ExtStr = getConfig("imgType").Split('|'); //图片的最大大小 int MaxSize = 1024; //错误提示 string[] MsgStr = new string[3]; MsgStr[0] = "上传文件大小超过限制."; MsgStr[1] = "上传文件扩展名是不允许的扩展名."; MsgStr[2] = "上传文件失败\\n请重试."; string imgWidth = Request.Form["imgWidth"]; string imgHeight = Request.Form["imgHeight"]; string imgBorder = Request.Form["imgBorder"]; string imgTitle = Request.Form["imgTitle"]; string imgAlign = Request.Form["imgAlign"]; string imgHspace = Request.Form["imgHspace"]; string imgVspace = Request.Form["imgVspace"]; string drawWater = Request.Form["chkWater"]; //string drawText = Request.Form["imgWater"]; string drawImage = Request.Form["chkImageWater"]; System.Web.HttpPostedFile imgFile = System.Web.HttpContext.Current.Request.Files["imgFile"]; //获得文件名 string FileName = System.IO.Path.GetFileName(imgFile.FileName); if (FileName != "") { if (imgFile.ContentLength > MaxSize*1024) { Alert(MsgStr[0] + imgFile.ContentLength); } else { string fileExtension = System.IO.Path.GetExtension(FileName).ToLower(); if (CheckExt(ExtStr, fileExtension)) { //重新为文件命名,时间毫秒部分+扩展名 string imgReName = "" + DateTime.Now.ToString("yyMMddHHmmssffff", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "" + fileExtension; try { ImageUpLoadDeal img = new ImageUpLoadDeal(); //img.Width = imgWidth; //img.Height = imgHeight; img.ImgClient = imgFile; img.Width = int.Parse(imgWidth); img.Height = int.Parse(imgHeight); img.Name = imgReName; img.SavePath = SavePath; img.DrawWater = (drawWater == "on" ? true : false); img.DrawImageWater = (drawImage == "on" ? true : false); //img.WaterText = drawText; img.SaveImage(); } catch { Alert(MsgStr[2]); } string imgUrl = SavePath + imgReName; ReturnImg(imgUrl, imgWidth, imgHeight, imgBorder, imgTitle, imgAlign, imgHspace, imgVspace); } else { Alert(MsgStr[1]); } } } } protected string getConfig(string word) { return System.Configuration.ConfigurationManager.AppSettings[word].ToString(); } /// <summary> /// 提示关闭层 /// </summary> /// <param name="MsgStr"></param> private void Alert(string MsgStr) { Response.Write("<html>"); Response.Write("<head>"); Response.Write("<title>error</title>"); Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"); Response.Write("</head>"); Response.Write("<body>"); Response.Write("<script type=\"text/javascript\">alert(\"" + MsgStr + "\");location.href='imgUp.aspx';</script>"); Response.Write("</body>"); Response.Write("</html>"); Response.End(); } /// <summary> /// 检测文件类型 /// </summary> /// <param name="ExtStr"></param> /// <param name="fileExt"></param> /// <returns></returns> private bool CheckExt(string[] ExtStr, string fileExt) { for (int i = 0; i < ExtStr.Length; i++) { if (ExtStr[i] != fileExt) { return true; } } return false; } /// <summary> /// 返回图片 /// </summary> /// <param name="FileUrl"></param> /// <param name="FileWidth"></param> /// <param name="FileHeight"></param> /// <param name="FileBorder"></param> /// <param name="FileTitle"></param> /// <param name="FileAlign"></param> /// <param name="FileHspace"></param> /// <param name="FileVspace"></param> private void ReturnImg(string FileUrl, string FileWidth, string FileHeight, string FileBorder, string FileTitle, string FileAlign, string FileHspace, string FileVspace) { Response.Write("<html>"); Response.Write("<head>"); Response.Write("<title>上传成功</title>"); Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"); Response.Write("</head>"); Response.Write("<body>"); Response.Write("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert("+"'editor'"+",'" + FileUrl + "','" + FileWidth + "','" + FileHeight + "','" + FileBorder + "','" + FileTitle + "','" + FileAlign + "','" + FileHspace + "','" + FileVspace + "');</script>"); Response.Write("</body>"); Response.Write("</html>"); Response.End(); } } /// <summary> ///图片上传处理类 /// </summary> class ImageUpLoadDeal { public ImageUpLoadDeal() { // //TODO: 在此处添加构造函数逻辑 // } private int _width; private int _height; private string _imgPath; private string _name; private bool _mainAspect = false; private string _savePath; private bool _drawWater = false; private string _waterText; private string _waterImage; private bool _drawImageWater = false; private System.Web.HttpPostedFile _imgClient; #region 属性 /**/ /// <summary> /// 相框的宽度 /// </summary> public int Width { get { if (_width == 0) return 1024; else return _width; } set { _width = value; } } /**/ /// <summary> /// 相框的高度 /// </summary> public int Height { get { if (_height == 0) return 800; else return _height; } set { _height = value; } } /**/ /// <summary> /// 待处理的图片的物理路径 /// </summary> public string ImgPath { get { return _imgPath; } set { _imgPath = value; } } /// <summary> /// 保存文件名 /// </summary> public string Name { get { return _name; } set { _name = value; } } /// <summary> /// 是否强制比例 /// </summary> public bool MainAspect { get { return _mainAspect; } set { _mainAspect = value; } } /// <summary> /// 客户端文件路径 /// </summary> public System.Web.HttpPostedFile ImgClient { get { return _imgClient; } set { _imgClient = value; } } /// <summary> /// 服务器端保存路径 /// </summary> public string SavePath { get { return _savePath; } set { _savePath = value; } } /// <summary> /// 是否添加文字水印 /// </summary> public bool DrawWater { get { return _drawWater; } set { _drawWater = value; } } /// <summary> /// 文字水印字符串 /// </summary> public string WaterText { get { try { _waterText = System.Configuration.ConfigurationManager.AppSettings["WaterText"].ToString(); } catch { _waterText = System.DateTime.Now.ToString(); } return _waterText; } } public bool DrawImageWater { get { return _drawImageWater; } set { _drawImageWater = value; } } public string WaterImage { get { try { _waterImage = System.Configuration.ConfigurationManager.AppSettings["WaterImage"].ToString(); } catch { _waterImage = System.DateTime.Now.ToString(); } return _waterImage; } } #endregion static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect,bool drawwater,bool drawimage,string watertext,string waterimage) { // return the source image if it's smaller than the designated thumbnail if (source.Width < thumbWi && source.Height < thumbHi) return source; System.Drawing.Bitmap ret = null; try { int wi, hi; wi = thumbWi; hi = thumbHi; if (maintainAspect) { // maintain the aspect ratio despite the thumbnail size parameters if (source.Width > source.Height) { wi = thumbWi; hi = (int)(source.Height * ((decimal)thumbWi / source.Width)); } else { hi = thumbHi; wi = (int)(source.Width * ((decimal)thumbHi / source.Height)); } } // original code that creates lousy thumbnails // System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero); ret = new Bitmap(wi, hi); using (Graphics g = Graphics.FromImage(ret)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.BlueViolet, 0, 0, wi, hi); g.DrawImage(source, 0, 0, wi, hi); if (drawimage == true) { System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(waterimage)); g.DrawImage(copyImage, new Rectangle(wi - copyImage.Width, hi - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); } if (drawwater == true) { Font font = new Font("Arial, Helvetica, sans-serif", 18); font = new Font("Arial, Helvetica, sans-serif", 18, System.Drawing.FontStyle.Bold); Brush brush = new SolidBrush(Color.LightBlue); g.DrawString(watertext, font, brush, 10, hi - 30); //g.DrawString(watertext,font,brush, } g.Dispose(); // g.Save(); } } catch { ret = null; } return ret; } public void SaveImage() { if (System.IO.Path.GetExtension(ImgClient.FileName).Trim() == ".gif") { ImgClient.SaveAs(System.Web.HttpContext.Current.Server.MapPath(SavePath) + Name); return; } Bitmap myBitmap = new Bitmap(ImgClient.InputStream); int X, Y; X = myBitmap.Width; Y = myBitmap.Height; decimal a = (decimal)X / (decimal)Y;//原图片的比例 decimal b = (decimal)Width / (decimal)Height;//相框的比例 if (b > a) { Height = Height; Width = (int)decimal.Round(Height * a, 0, MidpointRounding.AwayFromZero); } else { Width = Width; Height = (int)decimal.Round(Width / a, 0, MidpointRounding.AwayFromZero); } System.Drawing.Image myThumbnail = CreateThumbnail(myBitmap, Width, Height, MainAspect,DrawWater,DrawImageWater,WaterText,WaterImage); //Configure JPEG Compression Engine System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); long[] quality = new long[1]; quality[0] = 75; System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[x]; break; } } myThumbnail.Save(System.Web.HttpContext.Current.Server.MapPath(SavePath) + Name, jpegICI, encoderParams); myThumbnail.Dispose(); } } |
作者: einsta, 发布日期: 2009-02-22 16:46:54 |
好像最后个没完整 using System; public partial class aspx_imgUp : System.Web.UI.Page string imgWidth = Request.Form["imgWidth"]; if (FileName != "") try }
Response.Write("<html>");
/// <summary> /// <summary> /// <summary> /// <summary> /// <summary> public bool DrawImageWater public string WaterImage #endregion
System.Drawing.Bitmap ret = null; wi = thumbWi; if (maintainAspect) if (drawimage == true) } return ret; public void SaveImage() } System.Drawing.Image myThumbnail = CreateThumbnail(myBitmap, Width, Height, MainAspect,DrawWater,DrawImageWater,WaterText,WaterImage); System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(); System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); myThumbnail.Save(System.Web.HttpContext.Current.Server.MapPath(SavePath) + Name, jpegICI, encoderParams); |
回复 |
作者: Roddy, 发布日期: 2009-02-22 16:50:23 |
可能超过了mysql text长度,可以通过回复分开发表。
|
回复 |
作者: 点着烟看海, 发布日期: 2009-03-04 15:14:20 |
请问此扩展是对应于那个版本的啊。如何配置还能再详细点吗?
|
回复 |
作者: 小朱, 发布日期: 2009-08-24 13:47:39 |
回复 |
作者: 亮亮, 发布日期: 2009-11-21 21:10:51 |
能否也给我一份啊 我邮箱luochun815@163.com
|
回复 |