The processing of the shrink graph is after uploading the image to the server, synchronous generation of two different size of the reduction rate for the front-end call, just start to meet the demand, slowly with the front-end display of diversification, shrinkage chart has not been able to display the front end of the demand, so consider doing a real time to generate picture shrinkage chart services.
Every time you call to generate a thumbnail graph, do not cache is a bit wasteful, so in the generation of shrinkage at the same time the cache to the hard disk, the efficiency of a lot more.
Looked from the internet before someone with Nginx + LUA to achieve, efficiency that is nothing to say, but time is pressing, oneself also have no time to study, so temporarily first with Aps.net Mvc4 to realize one, later have time, and then slowly modify.
With your familiar. NET performance may be a little bit, but the implementation is fast, guaranteed to be online in the extreme time, and more powerful.
The idea is very simple, that is, according to the request, to determine whether the required shrinkage map is already on the hard disk, if there is a direct return, no download the original image, and generate a thumbnail map to the local, returned to the client.
Paste the code fragment directly below:
<summary>///generate picture shrinkage map Action///</summary>///<param name= "P" > Original image url</param>///<para M name= "id" > Picture size and the type of build shrink graph </param>///<returns></returns> [ValidateInput (false)] public ActionResult Index (string p, string id) {if (string).
IsNullOrEmpty (p)) {return new Httpstatuscoderesult (404); String oPath = Regex.Replace (P, @ "http[s]?:/ /(.*?)
/","/", regexoptions.ignorecase); Int?
Owidth = N, oheight = 200;
int cutmode = 3;
String Ppath;
String Odir; if (!string. IsNullOrEmpty (ID)) {string[] ss = ID.
Split (new char[] {' _ '}, Stringsplitoptions.removeemptyentries); if (ss.
Length < 2) {return new Httpstatuscoderesult (404); } if (ss. Length > 2) {cutmode = Int.
Parse (ss[2]); OPath = Opath.insert (Opath.lastindexof ('/') + 1, String.
Format ("{0}_{1}_{2}_", ss[0], ss[1], cutmode)); owidth = Int.
Parse (Ss[0]); oheight = Int.
Parse (Ss[1]);
} Ppath = Server.MapPath (OPath); Odir= Path.getdirectoryname (Ppath); if (!
System.IO.File.Exists (Ppath)) {byte[] imagebytes = Filehelper.downloadfile (p); if (!
Directory.Exists (Odir)) {directory.createdirectory (odir); } filehelper.makethumbnail (Filehelper.byttoimg (imagebytes), Owidth.value, Oheight.value, (ThumbnailMode) CutMode,
Ppath, True); Return File (Ppath, Filehelper.getcontenttypebyextension (Path.getextension (Ppath).
ToLower ()));
}
helper methods:
public class Filehelper {///<summary>///picture suffix and contenttype corresponding dictionary///</summary> static dictionary<s
Tring, string> extensioncontenttypedic; Static Filehelper () {if (extensioncontenttypedic = = null) {//.jpg ",". png ",". gif ",". JPEG Extensioncontenttype
Dic = new dictionary<string, string> ();
Extensioncontenttypedic.add (". jpg", "image/jpeg");
Extensioncontenttypedic.add (". png", "image/png");
Extensioncontenttypedic.add (". gif", "image/gif");
Extensioncontenttypedic.add (". jpeg", "image/jpeg"); }///<summary>///gets extension///</summary>///<param name= "extension" according to suffix name ></param>/ <returns></returns> public static string getcontenttypebyextension (string extension) {if Extensioncon
Tenttypedic.containskey (extension)) {return extensioncontenttypedic[extension];
return null; ///<summary >///Converts an Image object into a binary stream///</summary >///<param name= "image"; </param >///<returns > </returns > public static byte[] ImageToByteArray (image image) {Memorys
Tream ImageStream = new MemoryStream (); Bitmap bmp = new Bitmap (image. Width, image.
Height);
Graphics g = graphics.fromimage (BMP); G.drawimage (Image, new System.Drawing.Rectangle (0, 0, image.) Width, image.
Height)); try {bmp. Save (ImageStream, image.
Rawformat); catch (Exception e) {bmp.
Save (ImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
} byte[] byteimg = Imagestream.getbuffer (); Bmp.
Dispose ();
G.dispose ();
Imagestream.close ();
return byteimg; ///<summary>///byte flow change to Picture///</summary>///<param name= "byt" > stream of bytes to convert </param>///&L T;returns> Convert Image Object </returns> public static image Byttoimg (byte[] byt) {MemoryStream ms = new Memorystre
AM (byt);
Image img = image.fromstream (ms); Ms.
Close ();
return img; ///<summary>///Generate shrinkage Chart///</summary>///<paRam Name= "Originalimage" > Original picture image</param>///<param name= "width" > Scaling chart width </param>///<param Name= "Height" > Shrinkage Chart High </param>///<param name= "mode" > the way to generate a shrinkage chart </param>///<param name= " Thumbnailpath "> Shrink map Store address </param> public static image Makethumbnail (image originalimage, int width, int height, T
Humbnailmode mode, String thumbnailpath, bool Issave = true) {int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;
Switch (mode) {case thumbnailmode.hw://specifies width scaling (possibly deformed) break;
Case thumbnailmode.w://specified wide, high proportionally toheight = Originalimage.height * width/originalimage.width;
Break
Case thumbnailmode.h://specified high, wide proportional towidth = Originalimage.width * height/originalimage.height;
Break Case thumbnailmode.cut://Specify width cut (not deformed) if (double) originalimage.width/double originalimage.height > (double) tow
Idth/(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 picture System.Drawing.Image bitmap = new System.Drawing.Bitmap (towidth, toheight);
Create a new artboard Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Setting high quality interpolation method g.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree g.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill the g.clear with a transparent background color (color.transparent); Draws the specified portion of the original picture at the specified location and at the specified size g.drawimage (originalimage, New Rectangle (0, 0, Towidth, toheight), New Rectangle (x, Y, ow, O
h), GraphicsUnit.Pixel);
if (!issave) {return bitmap; try {//save thumbnail//bitmap in jpg format. Save (Thumbnailpath, bitmap.
Rawformat); Bitmap. Save (ThumbnaIlpath, Imageformat.jpeg);
return bitmap;
catch (System.Exception e) {throw e;
finally {originalimage.dispose (); Bitmap.
Dispose ();
G.dispose ();
return null; ///<summary>///Download specified file///</summary>///<param name= "Remoteurl" ></param>///<para
M name= "ss" ></param> public static byte[] DownLoadFile (string remoteurl) {WebClient WC = new WebClient (); try {return WC.
Downloaddata (Remoteurl);
catch (Exception e) {throw new Exception ("Download file failed"); }} public enum Thumbnailmode {///<summary>///Specify width scaling (possible variants)///</summary> HW,///<summary >///designation High, wide proportional///</summary> H,///<summary>///specified width, high proportional///</summary> W,///<summary
>///Specifies width cut (not deformed)///</summary> cuts,}
Access mode:
Http://www.souji8.com/home/index/{width}_{height}_{thummode}?p={imageurl}
{IMAGEURL}: Destination picture address
{Thummode}: 1: Specify width by scale, 2: Specify wide, high by proportional, 3: Specify width cut (not deformed)
{width}: expected picture width
{height}: expected picture height
The above is the entire content of this article, I hope to help you learn.