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
    Feb 28, 2013 @ 09:32
    Sherry Ann Hernandez
    0

    Overriding the application base to resize image

    I'm using the ff method to resize the image

     Media.BeforeSave += new Media.SaveEventHandler(Media_BeforeSave);
    
    
    
    void Media_BeforeSave(Media sender, SaveEventArgs e)
            {
                Property propUmbracoFile = sender.getProperty("umbracoFile");

                if (propUmbracoFile != null && propUmbracoFile.Value != null && !string.IsNullOrEmpty(propUmbracoFile.Value.ToString()))
                {
                    string imageFile = sender.getProperty("umbracoFile").Value.ToString();
                    string tempExtension = Path.GetExtension(imageFile).ToLower();

                    if ((tempExtension == "jpg") || (tempExtension == "jpeg") || (tempExtension == "gif") || (tempExtension == "png"))
                    {
                        int imageHeight = Convert.ToInt32(sender.getProperty("umbracoHeight").Value);
                        int imageWidth = Convert.ToInt32(sender.getProperty("umbracoWidth").Value);

                        //Remove the first slash from the path.   
                        imageFile = imageFile.Remove(0, 1);

                        //Get the server path.   
                        string serverPath = HttpContext.Current.Server.MapPath("~");
                        int imageSelection = Convert.ToInt32(sender.getProperty("imageType").Value);

                        //Get some strings needed for building the paths.   
                        string fileName = Path.GetFileName(imageFile);
                        string extension = Path.GetExtension(imageFile);
                        string shortFileName = fileName.Replace(extension, string.Empty);
                        string mediaPath = imageFile.Substring(0, imageFile.LastIndexOf("/"));

                        string format1000X750File = string.Concat(mediaPath, "/", shortFileName, extension);
                        string filePath = Path.Combine(serverPath, format1000X750File.Replace("/", "\\"));

                        ResizeImage(filePath, imageHeight, imageWidth, false, imageSelection);

                    }
                }
            }
    
    
    
    internal static string ResizeImage(string imageUrl, int maxHeight, int maxWidth, bool isThumbnail, int type)
            {
                if (imageUrl == String.Empty) return String.Empty;

                var origUrl = imageUrl;
                var origFile = origUrl;

                // Get height and width
                var height = maxHeight;
                var width = maxWidth;

                // get orig info
                var origFileNameWithoutExtension = Path.GetFileNameWithoutExtension(origFile);
                var origFileNameWithExtension = Path.GetFileName(origFile);
                var origExtension = Path.GetExtension(origFile);

                // build new file name
                var newFileName = String.Format("{0}_{1}{2}", origFileNameWithoutExtension, "thumb", origExtension);
                var newFile = origFile.Replace(origFileNameWithExtension, newFileName);

                // Resize Image
                if (isThumbnail)
                {
                    ResizeThumbnailImage(origFile, newFile, width, height, type);
                }
                else
                {
                    ResizeOrigImage(origFile, width, height, type);
                }

                return origUrl.Replace(origFileNameWithExtension, newFileName);
            }

            private static void ResizeOrigImage(string origFile, int maxWidth, int maxHeight, int type)
            {
                if (type != 1)
                {
                    var fullSizeImage = Image.FromFile(origFile);

                    // Ensure the generated thumbnail is not being used by rotating it 360 degrees
                    fullSizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    fullSizeImage.RotateFlip(RotateFlipType.Rotate180FlipNone);



                    if ((fullSizeImage.Height < fullSizeImage.Width) || (fullSizeImage.Height == 800))
                    {
                        maxHeight = 800;
                        maxWidth = fullSizeImage.Width * maxHeight / fullSizeImage.Height;

                    }
                    else
                    {
                        // Landscape Image
                        maxWidth = 800;
                        maxHeight = fullSizeImage.Height * maxWidth / fullSizeImage.Width;
                    }

                    GenerateThumbnail(fullSizeImage, maxWidth, maxHeight, origFile);

                    fullSizeImage.Dispose();
                }

            }

    But I noticed that the image is not resizing properly. Can anyone tell if this is not supported in the new version of umbraco 4.7 and 4.11

     

  • Rik Helsen 670 posts 873 karma points
    Feb 28, 2013 @ 09:38
  • Sherry Ann Hernandez 320 posts 344 karma points
    Feb 28, 2013 @ 09:57
    Sherry Ann Hernandez
    0

    But I will need to resize also the thumbnail as the current thumbnail of umbraco is smaller than what we need. I'm just wondering why my code is not working considering that it is almost the same as waffel's code.

  • Rik Helsen 670 posts 873 karma points
    Feb 28, 2013 @ 09:59
    Rik Helsen
    0

    don't know, personally i wouldn't use a code solution but simply use imageGen for generating resizes of any dimensions ? :)

     

Please Sign in or register to post replies

Write your reply to:

Draft