Kindeditor upload pictures automatically compress too large pictures

Source: Internet
Author: User
Tags bmp image tojson

Kindeditor upload pictures automatically will be too large (pixels and image length width) of the picture to compress, only in the Kindeditor upload_json.ashx inside modify!

<%@ WebHandler language= "C #" class= "Upload"%>

/**
* Kindeditor ASP.
*
* This ASP. NET program is a demo program and is not recommended to be used directly in the actual project.
* If you are sure to use this program directly, please carefully confirm the relevant safety settings before use.
*
*/

Using System;
Using System.Collections;
Using System.Web;
Using System.IO;
Using System.Globalization;
Using Litjson;
Using System.Drawing;
Using System.Drawing.Imaging;

public class Upload:ihttphandler
{
Private HttpContext context;

public void ProcessRequest (HttpContext context)
{
String Aspxurl = context. Request.Path.Substring (0, context. Request.Path.LastIndexOf ("/") + 1);

File Save directory path
String Savepath = ".. /attached/";

File Save Directory URL
String Saveurl = Aspxurl + "... /attached/";

Define the file extensions that are allowed to be uploaded
Hashtable exttable = new Hashtable ();
Exttable.add ("image", "gif,jpg,jpeg,png,bmp");
Exttable.add ("Flash", "swf,flv");
Exttable.add ("flv", "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");

Maximum file size
Long maxSize = 900000000000;
This.context = context;

Httppostedfile Imgfile = context. request.files["Imgfile"];
if (Imgfile = = null)
{
ShowError ("Please select File. ");
}

String Dirpath = context. Server.MapPath (Savepath);
if (! Directory.Exists (Dirpath))
{
ShowError ("The upload directory does not exist. ");
}

String DirName = context. request.querystring["dir"];
if (String.IsNullOrEmpty (DirName)) {
DirName = "image";
}
if (!exttable.containskey (DirName)) {
ShowError ("The directory name is incorrect. ");
}

String fileName = imgfile.filename;
String Fileext = path.getextension (fileName). ToLower ();

if (Imgfile.inputstream = = NULL | | imgFile.InputStream.Length > MaxSize)
{
ShowError ("Upload file size exceeds limit. ");
}

if (String.IsNullOrEmpty (fileext) | | Array.indexof (((String) exttable[dirname]). Split (', '), fileext.substring (1). ToLower ()) = =-1)
{
ShowError ("The upload file name extension is not allowed. \ n allows only "+ ((String) exttable[dirname]) +" format. ");
}

Create a folder
Dirpath + = DirName + "/";
Saveurl + = DirName + "/";
if (! Directory.Exists (Dirpath)) {
Directory.CreateDirectory (Dirpath);
}
String ymd = DateTime.Now.ToString ("YyyyMMdd", datetimeformatinfo.invariantinfo);
Dirpath + = Ymd + "/";
Saveurl + = Ymd + "/";
if (! Directory.Exists (Dirpath)) {
Directory.CreateDirectory (Dirpath);
}

String NewFileName = DateTime.Now.ToString ("Yyyymmddhhmmss_ffff", datetimeformatinfo.invariantinfo) + Fileext;
String FilePath = Dirpath + newfilename;

Imgfile.saveas (FilePath);

String FILEURL = Saveurl + newfilename;

Hashtable hash = new Hashtable ();
hash["error"] = 0;
hash["url"] = FILEURL;

String AA = "Gif,jpg,jpeg,png,bmp";
if (Array.indexof (AA. Split (', '), fileext.substring (1). ToLower ())! =-1)
{
Image imggg = Image.FromFile (FilePath);
if (IMGGG. Width > 850)
{
Imggg. Dispose ();
Makethumbnail (FilePath, FilePath, 850, "W", Fileext);
}

if (Imgfile.inputstream = = NULL | | imgFile.InputStream.Length > 500*1024)
{
Makethumbnail (FilePath, FilePath, 850, "W", Fileext);
}



}


Context. Response.AddHeader ("Content-type", "text/html; Charset=utf-8 ");
Context. Response.Write (Jsonmapper.tojson (hash));
Context. Response.End ();
}

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;
}
}
<summary>
Generate thumbnail images
</summary>
<param name= "Originalimagepath" > Source map Path </param>
<param name= "Thumbnailpath" > Thumbnail path </param>
<param name= "width" > Thumbnail width </param>
<param name= "height" > thumbnail height </param>
<param name= "mode" > How to Generate thumbnails: HW Specifies aspect scaling (possibly deformed); W specify wide, high proportionally H specify high, wide proportionally cut specify aspect cut (not deformed) </param>
<param name= "mode" > the format to be saved for thumbnails (gif,jpg,bmp,png) is empty or unknown type is considered jpg</param>
public void Makethumbnail (string originalimagepath, string thumbnailpath, int width, int height, string mode, string image Type)
{
Image originalimage = Image.FromFile (Originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;
Switch (mode)
{
Case "HW"://Specify aspect scaling (possibly deformed)
Break
Case "W"://Specify width, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://Specify high, wide proportionally
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "Cut"://Designation Aspect cut (not deformed)
if (double) originalimage.width/(double) originalimage.height > (double) towidth/(double) toheight)
{
Oh = originalimage.height;
ow = Originalimage.height * towidth/toheight;
y = 0;
x = (Originalimage.width-ow)/2;
}
Else
{
ow = Originalimage.width;
Oh = originalimage.width * height/towidth;
x = 0;
y = (Originalimage.height-oh)/2;
}
Break
Default
Break
}
Create a new BMP image
Image bitmap = new System.Drawing.Bitmap (towidth, toheight);
Create a new artboard
Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Setting high-quality interpolation methods
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smoothness
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill it with a transparent background color
G.clear (color.transparent);
Draws the specified portion of the original picture at the specified position and at the specified size
G.drawimage (Originalimage, New Rectangle (0, 0, Towidth, toheight),
New Rectangle (x, Y, Ow, OH),
GraphicsUnit.Pixel);
Try
{
Save thumbnails in JPG format
Switch (Imagetype.tolower ())
{
Case ". gif":
Originalimage.dispose ();
File.delete (Originalimagepath);
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
Case ". jpg":
Originalimage.dispose ();
File.delete (Originalimagepath);
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
Case ". bmp":
Originalimage.dispose ();
File.delete (Originalimagepath);
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
Case ". png":
Originalimage.dispose ();
File.delete (Originalimagepath);
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
Default
Originalimage.dispose ();
File.delete (Originalimagepath);
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Break
}
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Bitmap. Dispose ();
G.dispose ();
}
}
}

Reprinted from: http://download.csdn.net/detail/qq_16218837/7575655

Kindeditor upload pictures automatically compress too large pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.