Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 945 posts 2077 karma points
    Mar 20, 2013 @ 22:41
    Nigel Wilson
    0

    Update to provide both height and width maximums

    Hi Ernst

    Fantastic piece of functionality - something a current project needed, and somethnig all content management sites potentially need when people with less appreciation for the importance of image optimsation.

    I had the need to be able to configure both the maximum width and height of images so I have tweaked the code as follows:

    WaffelAutoCompress.config
    I added the following line below the targetwidth setting

    <targetheight>800</targetheight>

    Then within the WaffelImageCompressor.cs file I removed the following code

    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;
            }

    And replaced it with the following code

    if (allowUpscale != "true" && (originalBMP.Width < targetWidth || originalBMP.Height < targetHeight))
    {
    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;
            var ratioX = (double)targetWidth / originalBMP.Width;
            var ratioY = (double)targetHeight / originalBMP.Height;
            var ratio = Math.Min(ratioX, ratioY);
            imgWidth = (int)(originalBMP.Width * ratio);
            imgHeight = (int)(originalBMP.Height * ratio);

    So far it is working nicely and obeying the maximum rules. I've only just got it running and tested it, but thought I'd drop the code in here for others to reference, and/or for you to consider adding to your package.

    The above I did find on the mighty interweb so can't take credit for the code, merely the mushing together of the code...

    Again, thanks for the nice piece of functionality.

    Regards, Nigel

     

  • Ernst Utvik 123 posts 235 karma points
    Mar 21, 2013 @ 13:31
    Ernst Utvik
    0

    Thanks for your kind feedback, and thanks for sharing code :) Glad to see that people are using and customizing this package to fit their needs.

    I'll see if I cant get the Umbraco 6 version out during easter with some extra functionality. Its long overdue ;)

Please Sign in or register to post replies

Write your reply to:

Draft