Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Web;
- using umbraco;
- using umbraco.cms.businesslogic.media;
- namespace Decode.Umb
- {
- public partial class Save : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- // if the user exited the editor close the window
- if (Request.QueryString["close"] == "1") {
- closeWindow();
- return;
- }
- // get media id and media key
- if (Request.QueryString["id"] == null) return;
- string[] qry = Request.QueryString["id"].Split('|');
- int mediaId; if (!int.TryParse(qry[0], out mediaId)) return;
- string mediaKey = qry[1];
- // get media
- Media m = new Media(mediaId);
- if (m == null) return;
- // verify our hashed key matches the media's
- if (Decode.Umb.Hash.GetMediaMd5Sum(m) != mediaKey) {
- handleError();
- return;
- }
- if (Request.Files.Count > 0) {
- if (Request.Files["image"].ContentLength > 0) {
- // settings
- HttpPostedFile file = Request.Files["image"];
- string imageType = Request.Form["type"];
- string mediaPath = (GlobalSettings.FullpathToRoot + Path.GetDirectoryName(m.getProperty("umbracoFile").Value.ToString()).Replace("/", "\\") + "\\").Replace("\\\\", "\\");
- string mediaVirUrl = Path.GetDirectoryName(m.getProperty("umbracoFile").Value.ToString()).Replace("\\", "/") + "/";
- string mediaName = Path.GetFileNameWithoutExtension(m.getProperty("umbracoFile").Value.ToString()) + "." + imageType;
- string savePath = mediaPath + mediaName;
- string thumbnailPath = mediaPath + mediaName.Replace("." + imageType, "_thumb.jpg");
- // verify the returned file is an image file
- if (imageType != "jpg" && imageType != "png" && imageType != "gif" && imageType != "bmp") {
- handleError("The selected image type can not be saved.");
- return;
- }
- // save image
- file.SaveAs(savePath);
- // get the saved image for thumbnail use
- System.Drawing.Image image = Image.FromStream(file.InputStream);
- // create the thumbnail
- generateThumbnail(image, 100, image.Width, image.Height, thumbnailPath);
- // update media data
- m.getProperty("umbracoFile").Value = mediaVirUrl + mediaName;
- m.getProperty("umbracoHeight").Value = image.Height.ToString();
- m.getProperty("umbracoWidth").Value = image.Width.ToString();
- m.getProperty("umbracoBytes").Value = file.ContentLength.ToString();
- m.getProperty("umbracoExtension").Value = imageType;
- // reload umbraco media page and close this window
- ClientScript.RegisterStartupScript(this.GetType(), "finish", "opener.location.reload();window.close();", true);
- }
- }
- closeWindow();
- }
- private void handleError()
- {
- handleError("Ups... An unknown error occurred. The image could not be saved.");
- }
- private void handleError(string text)
- {
- string jscript = String.Format("opener.location.reload();alert('{0}');window.close();", text);
- ClientScript.RegisterStartupScript(this.GetType(), "error", jscript, true);
- }
- private void closeWindow()
- {
- ClientScript.RegisterStartupScript(this.GetType(), "error", "opener.location.reload();window.close();", true);
- }
- private void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string thumbnailFileName)
- {
- // Generate thumbnail
- float fx = (float)fileWidth / (float)maxWidthHeight;
- float fy = (float)fileHeight / (float)maxWidthHeight;
- // must fit in thumbnail size
- float f = Math.Max(fx, fy); //if (f < 1) f = 1;
- int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);
- // fixes for empty width or height
- if (widthTh == 0)
- widthTh = 1;
- if (heightTh == 0)
- heightTh = 1;
- // Create new image with best quality settings
- Bitmap bp = new Bitmap(widthTh, heightTh);
- Graphics g = Graphics.FromImage(bp);
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.PixelOffsetMode = PixelOffsetMode.HighQuality;
- // Copy the old image to the new and resized
- Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
- g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
- // Copy metadata
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
- ImageCodecInfo codec = null;
- for (int i = 0; i < codecs.Length; i++) {
- if (codecs[i].MimeType.Equals("image/jpeg"))
- codec = codecs[i];
- }
- // Set compresion ratio to 90%
- EncoderParameters ep = new EncoderParameters();
- ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
- // Save the new image
- bp.Save(thumbnailFileName, codec, ep);
- bp.Dispose();
- g.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement