Copied to clipboard

Flag this post as spam?

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


  • Dan White 206 posts 510 karma points c-trib
    Mar 04, 2013 @ 22:31
    Dan White
    0

    Does not install correctly in 6.0.2

     CS0535: 'WAC' does not implement interface member 'Umbraco.Core.IApplicationEventHandler.OnApplicationInitialized(Umbraco.Core.UmbracoApplicationBase, Umbraco.Core.ApplicationContext)'
  • Ernst Utvik 123 posts 235 karma points
    Mar 07, 2013 @ 23:13
    Ernst Utvik
    0

    Yes. Waffel AutoCompress is currently not V6 compatible due to changes in the Umbraco event handlers. I'll add a note on the package frontpage until I get some more time to look at it.

  • Dan White 206 posts 510 karma points c-trib
    Mar 14, 2013 @ 23:46
    Dan White
    0

    I have the event handler part worked out, but now having another problem.

    line:103 in WaffelImageCompressor.cs

    imgBMP.Save(targetDirectory + fileNameWOExt + ".jpg", codec, eParams);

    throws:

     

    Server Error in '/' Application.


    A generic error occurred in GDI+.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace: 

    [ExternalException (0x80004005): A generic error occurred in GDI+.]
       System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +472698
       WaffelImageCompressor.ImageDownscale(FileStream file, Media sender, String ext) +1938
       WaffelImageCompressor.compressImage(Media sender) +1423
       WAC.Media_AfterSave(Media sender, SaveEventArgs e) +45
       umbraco.cms.businesslogic.media.SaveEventHandler.Invoke(Media sender, SaveEventArgs e) +0
       umbraco.cms.businesslogic.media.Media.FireAfterSave(SaveEventArgs e) +54
       umbraco.cms.businesslogic.media.Media.Save() +678
       umbraco.cms.presentation.editMedia.Save(Object sender, EventArgs e) +1138
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       umbraco.controls.ContentControl.saveClick(Object Sender, ImageClickEventArgs e) +646
       System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
       System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +124
       System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
    



     

     

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

     

    It seems to me that it's having problems creating the new directory. I'm running it on localhost, so I don't think permissions would be the problem.

    Anyway, the new code for WAC.cs:

     

    using Umbraco.Core;
    using Umbraco.Web;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.media;

    /// <summary>
    /// Summary description for WAC
    /// </summary>

    public class WAC : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
     
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            Media.AfterSave += Media_AfterSave;
        }
     
        void Media_AfterSave(Media sender, SaveEventArgs e)
        {
            WaffelImageCompressor.compressImage(sender);
        }

    public WAC()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    }

     

  • Ernst Utvik 123 posts 235 karma points
    Mar 15, 2013 @ 11:28
    Ernst Utvik
    1

    The error came from trying to saving the new image in a folder that did not exist. Perhaps Umbraco 6 no longer use the Id of the upload field as the folder name? Did a little rewrite that seems to hade done it. Could you test the following and let me know how it works?

     

    private static void ImageDownscale(FileStream file, Media sender, string ext)

        {

            //Get settings

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(HttpContext.Current.Server.MapPath("~/config/WaffelAutoCompress.config"));

            var targetWidth = System.Convert.ToInt32(xmlDoc.GetElementsByTagName("targetwidth")[0].InnerText);

            var allowUpscale = xmlDoc.GetElementsByTagName("allowupscale")[0].InnerText;

            var propertyAlias = xmlDoc.GetElementsByTagName("propertyalias")[0].InnerText;

            var jpgQuality = xmlDoc.GetElementsByTagName("jpgquality")[0].InnerText;

            long compression = System.Convert.ToInt32(jpgQuality);

            string fullFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString());

            var fileNameWOExt = Path.GetFileNameWithoutExtension(file.Name.ToString());

     

            //Create new bitmap from uploaded file

            Bitmap originalBMP = new Bitmap(file);

     

            //Check if upscaling is allowed and if rule is relevant for current bitmap

            bool upscaleConflict = false;

            int longest = originalBMP.Width > originalBMP.Height ? originalBMP.Width : originalBMP.Height;

            if (allowUpscale != "true" && longest < targetWidth) upscaleConflict = true;

     

            //If no upscale conflict is found proceed with image scaling and compression.

            if (!upscaleConflict)

            {

                //Set correct width and height for scaled image

                int imgWidth, imgHeight;

                if (originalBMP.Width > originalBMP.Height)

                {

                    imgWidth = targetWidth;

                    imgHeight = originalBMP.Height * targetWidth / originalBMP.Width;

                }

     

                else

                {

                    imgWidth = originalBMP.Width * targetWidth / originalBMP.Height;

                    imgHeight = targetWidth;

                }

     

                //Create scaled bitmap

                Bitmap imgBMP = new Bitmap(originalBMP, imgWidth, imgHeight);

                Graphics iGraphics = Graphics.FromImage(imgBMP);

                if (ext == "png" || ext == "gif") iGraphics.Clear(Color.White); //Sets white background for transparent png and gif

                iGraphics.SmoothingMode = SmoothingMode.HighSpeed; iGraphics.InterpolationMode = InterpolationMode.Default;

                iGraphics.DrawImage(originalBMP, 0, 0, imgWidth, imgHeight);

                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1]; //jpg

                System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);

                eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression); //jpg compression

                

                file.Dispose();

                originalBMP.Dispose();

                //Save new scaled and compressed jpg

                string mediaFolderName = new DirectoryInfo(sender.getProperty(propertyAlias).Value.ToString()).Parent.Name;

                string targetDirectory = HttpContext.Current.Server.MapPath(string.Format("~/media/" + mediaFolderName + "/"));

                imgBMP.Save(targetDirectory + fileNameWOExt + ".jpg", codec, eParams);

                imgBMP.Dispose();

                iGraphics.Dispose();

                

     

                //Delete original file if not jpg

                if (ext != "jpg")

                {

                    File.Delete(HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString()));

                }

     

                //Populate upload property and standard Image properties if they exist

                sender.getProperty(propertyAlias).Value = "/media/" + mediaFolderName + "/" + fileNameWOExt + ".jpg";

                System.IO.FileInfo fi = new FileInfo(fullFilePath);

                if (sender.getProperty("umbracoBytes") != null) sender.getProperty("umbracoBytes").Value = fi.Length.ToString();

                if (sender.getProperty("umbracoExtension") != null) sender.getProperty("umbracoExtension").Value = "jpg";

                if (sender.getProperty("umbracoWidth") != null) sender.getProperty("umbracoWidth").Value = imgWidth.ToString();

                if (sender.getProperty("umbracoHeight") != null) sender.getProperty("umbracoHeight").Value = imgHeight.ToString();

     

                sender.Save();

                

            }

            else

            {

                //If upscaleconflict is detected dispose and do nothing

                file.Dispose();

                originalBMP.Dispose();

            }

        }

     

  • Ernst Utvik 123 posts 235 karma points
    Mar 15, 2013 @ 11:50
    Ernst Utvik
    0

    One more thing. When original image is not jpg the code above fails to update media parameters. Replacing the last part (from the "//delete original file if"-line) with the following should do it :

        string originalFilePath = HttpContext.Current.Server.MapPath(sender.getProperty(propertyAlias).Value.ToString()); 

                //Populate upload property and standard Image properties if they exist

                sender.getProperty(propertyAlias).Value = "/media/" + mediaFolderName + "/" + fileNameWOExt + ".jpg";

                System.IO.FileInfo fi = new FileInfo(fullFilePath);

                if (sender.getProperty("umbracoBytes") != null) sender.getProperty("umbracoBytes").Value = fi.Length.ToString();

                if (sender.getProperty("umbracoExtension") != null) sender.getProperty("umbracoExtension").Value = "jpg";

                if (sender.getProperty("umbracoWidth") != null) sender.getProperty("umbracoWidth").Value = imgWidth.ToString();

                if (sender.getProperty("umbracoHeight") != null) sender.getProperty("umbracoHeight").Value = imgHeight.ToString();

                sender.Save();

               //Delete original file if not jpg

                if (ext != "jpg")

                {

                    File.Delete(originalFilePath);

                }

  • Dan White 206 posts 510 karma points c-trib
    Mar 15, 2013 @ 18:55
    Dan White
    0

    Thanks. Got it working.

    Would it be possible to make it support png and gif just like jpg? I mean without losing transparency or changing file extensions.

    Jeroen Breuer shared some code here [ http://our.umbraco.org/forum/developers/extending-umbraco/31388-Resizing-image-on-upload ] I was playing around with as well, but haven't gotten it to work yet. I believe his maintains the transparency & extension.

    Anyway, thanks for the help!

  • Ernst Utvik 123 posts 235 karma points
    Mar 17, 2013 @ 00:49
    Ernst Utvik
    100

    Should be possible yes. Maby I'll make it an option some time. Not sure the code from your link preserves image formats. If I'm not mistaken System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1] is the jpg encoder, which is the one used in the example.

    Anyways. Thnx for trying out the package :) Hope you make it work for you.

  • Dan White 206 posts 510 karma points c-trib
    Mar 17, 2013 @ 00:52
    Dan White
    0

    You might be right. I'm not too familiar with the image libraries. Anyway, thanks again.Great work! 

  • Ernst Utvik 123 posts 235 karma points
    May 22, 2013 @ 03:02
    Ernst Utvik
    0

    Just a heads up: Released v1.1 for Umbraco 6. Added an experimental setting that allows you to preserve and resize png's with transparency. Better late than never ;)

Please Sign in or register to post replies

Write your reply to:

Draft