Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sherry Ann Hernandez 320 posts 344 karma points
    Jun 27, 2011 @ 16:05
    Sherry Ann Hernandez
    0

    Customizing the DAMP

    Hi Jeroen,

    First, thanks for this plugin. It was really helpful for me to understand also the custom data type.

    Anyway, I'll be using this plugin in my project but I needed to add some customization as my client wants to resize the image being uploaded and also the image thumbnail. So base on the image type being uploaded the thumbnail can be scale to 103px or 60px accordingly and the image is also scale to 400px.

    My problem is when you call this line, umbraco is already generating a thumbnail and it is uploading the image to the server right?

    uploadField.DataEditor.Save();

    How can I call my code for scaling this image before it actually upload the image?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jun 27, 2011 @ 16:27
    Jeroen Breuer
    1

    Hello,

    If you go to the upload datatype in the developer section there you can choose what size the thumbnails should be.

    As for resizing the image I do this in the before save event:

    #region Create resized image
    
    //Get the umbracoFile and resized properties. Property propUmbracoFile = media.getProperty("umbracoFile"); Property propResized = media.getProperty("resized");
    if (propResized != null && propUmbracoFile != null && propUmbracoFile.Value != null && !string.IsNullOrEmpty(propUmbracoFile.Value.ToString())) { //Get the image path. string umbracoFile = propUmbracoFile.Value.ToString(); //Remove the first slash from the path. umbracoFile = umbracoFile.Remove(0, 1); //Get the server path. string serverPath = HttpContext.Current.Server.MapPath("~"); //Get some strings needed for building the paths. string umbracoFilePath = Path.Combine(serverPath, umbracoFile.Replace("/", "\\")); string fileName = Path.GetFileName(umbracoFile); string extension = Path.GetExtension(umbracoFile); string shortFileName = fileName.Replace(extension, string.Empty); string mediaPath = umbracoFile.Substring(0, umbracoFile.LastIndexOf("/")); //Resize the image to 1000x750 and save it. //Normally we resize to 800x600, but we needed larger images this time. string format1000x750File = string.Concat(mediaPath, "/", shortFileName, "_resized", extension); string format1000x750Path = Path.Combine(serverPath, format1000x750File.Replace("/", "\\")); if (ResizeRequired(media, 1000, 750)) { //Get the image. byte[] file = File.ReadAllBytes(umbracoFilePath); ResizeImage(file, format1000x750Path, 1000, 750, "HW"); } else { // copy the base file instead File.Copy(umbracoFilePath, format1000x750Path, true); } //Store the paths inside the properties. propResized.Value = string.Concat("/", format1000x750File); } #endregion

    private bool ResizeRequired(Media media, int maxWidth, int maxHeight) { var width = media.GetPropertyValue("umbracoWidth").ToInt(); var height = media.GetPropertyValue("umbracoHeight").ToInt(); return (width != null && height != null && (width > maxWidth || height > maxHeight)); }

    public void ResizeImage(byte[] file, string FilePathToSaveTo, int width, int height, string mode) { //Convert the byte array to an image. MemoryStream ms = new MemoryStream(file); System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(ms); //Create the thumbnail. ResizeImage(FullsizeImage, FilePathToSaveTo, width, height, mode); }

    public void ResizeImage(System.Drawing.Image originalImage, string FilePathToSaveTo, int width, int height, string mode) { int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case "HW": if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { toheight = (int)((Decimal)originalImage.Height / (Decimal)((Decimal)originalImage.Width / (Decimal)towidth)); towidth = width; } else { towidth = (int)((Decimal)originalImage.Width / (Decimal)((Decimal)originalImage.Height / (Decimal)toheight)); toheight = height; } break; case "W": toheight = originalImage.Height * width / originalImage.Width; break; case "H": towidth = originalImage.Width * height / originalImage.Height; break; case "Cut": 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: throw new Exception("No valid resize mode was passed"); } System.Drawing.Imaging.ImageCodecInfo[] icf = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters encps = new System.Drawing.Imaging.EncoderParameters(1); encps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(FilePathToSaveTo, icf[1], encps); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }

    Btw the above code isn't related to DAMP.

    Jeroen

  • Sherry Ann Hernandez 320 posts 344 karma points
    Jun 29, 2011 @ 07:36
    Sherry Ann Hernandez
    0

    My client has a specific logic on how to resize the image. Depending on the size of the image that is how we scale them. if it's portrait the height should be 103 and if it's a landscape the max size of the width should be 230.

    But how can I insert other image image validation (like maximum size and 1:2 criteria, fixed width and height for the banner image, minimum size of 400px) to DAMP?

    The image resizing, I can do this using the event handler is that how you do it? I didn't know that. :D

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jun 29, 2011 @ 08:11
    Jeroen Breuer
    0

    Yes I use the event handler to resize images, but that's not done by DAMP. It's a custom event I use on most websites I'm working on. DAMP works very well in combination with the ImageCropper datatype, but perhaps ImageGen is better for you.

    Jeroen

  • Sherry Ann Hernandez 320 posts 344 karma points
    Jun 29, 2011 @ 12:10
    Sherry Ann Hernandez
    0

    Hi Jeroen,

    Another question. How do you get this Property propUmbracoFile = media.getProperty("umbracoFile"); from the before_save  event of media? is it through the sender.media?


     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jun 29, 2011 @ 12:12
    Jeroen Breuer
    0

    Sorry in my sample I use media, but in the event it should be sender.getProperty("umbracoFile") because sender is a Media-object.

    So it would look like this:

    Property propUmbracoFile = sender.getProperty("umbracoFile");

    Jeroen

  • Sherry Ann Hernandez 320 posts 344 karma points
    Jun 29, 2011 @ 12:26
    Sherry Ann Hernandez
    0

    Another question, I'm resizing the image depending on the image type that I select in the damp datatype (I customize it to add an options for this). Is there a way for me to get this using the event handler?

    Sorry, if I have a lot of questions.

  • Sherry Ann Hernandez 320 posts 344 karma points
    Jun 29, 2011 @ 14:34
    Sherry Ann Hernandez
    0

    Sorted this out. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft