Copied to clipboard

Flag this post as spam?

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


  • Stephen 94 posts 255 karma points
    May 19, 2016 @ 02:46
    Stephen
    1

    Limit image upload size (not through web.config)

    Hi Guys,

    I have looked around and I can't really see anything that answers this.

    I know I can set the uploadsize in the web.config.

    I work with a lot of designers, and they simply have no idea how big images can be. They can be uploading images into the cropper at sizes in excess of 6000px wide, and 10's of mb. This is a total waste for the web stuff we need. The images can't be displayed any bigger than 960px wide.

    I want to continue using the built-in image cropper for uploading the images. If I have to, I am happy to force them to upload them to the media area first, then select them (but that would not be ideal).

    So ideally I am looking for this:

    Ideally is there a simple way I can just resize the image after it is uploaded and dump the oversized version (this gets around any user issues).

    Failing that is there a function that will pop up and say "image too big" when people try to upload an image through the cropper.

    In many cases this would take some of the sites I need to deal with from 1Gb down to 100Mb type of thing.

    I am kinda surprised this is not already a function in Umbraco.

    Thanks

    Stephen

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 19, 2016 @ 05:29
    Jan Skovgaard
    2

    Hi Stephen

    You should be able to pre-process the images making it possible to resize the images before they're actually being uploaded and turned into media items in Umbraco.

    The creator of the nice image cropper being used in Umbraco, mr. James Jackson-South wrote an article with a code example showing this in the 2014 edition of the annual 24 days in Umbraco advent calendar - You can read the article here http://24days.in/umbraco/2014/all-your-images-are-belong-to-umbraco/ - You can skip to the last part with the headline "Pre Processing" if you would like.

    I hope this helps!

    /Jan

  • Stephen 94 posts 255 karma points
    May 19, 2016 @ 23:45
    Stephen
    0

    Thanks Jan. I have had a read through. Looks like what I need. I will have a play tomorrow. I might be looking for a little more help.

    Thanks Stephen

  • James Jackson-South 489 posts 1747 karma points c-trib
    May 20, 2016 @ 02:30
    James Jackson-South
    1

    If you get stuck, Give me a holler and I'll give you a hand.

  • Stephen 94 posts 255 karma points
    May 21, 2016 @ 04:04
    Stephen
    0

    Hi James,

    Thanks for the offer to help. I am going to have to take you up on that please.

    Firstly to be honest, this is above my head. My level of coding is now where near this standard. I basically understand what the code provided here : http://24days.in/umbraco/2014/all-your-images-are-belong-to-umbraco/ under the section "Pre Processing", but I have no idea what to do with it, where to put it, how to make it work....If you follow what I mean. I have read a few other articles related, but I don't think any are quite what I need.

    My head is swimming, so any pointers would help, and if the thought enters you head, "how much does he know", just assume the paperweight sitting on your desk knows more than me, and we have a good starting point :).

    Don't get me wrong I am happy to learn, but the feeling I am trying to earn the wrong stuff to start with...

    Thanks

    Stephen

  • Stephen 94 posts 255 karma points
    May 21, 2016 @ 16:46
    Stephen
    0

    OK, so I totally feal like a bull in a china shop here, but I did this:

    I created a .cs file in the app_code folder, with the following content:

        using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Web.Helpers;
    
    using ImageProcessor;
    using ImageProcessor.Imaging;
    using ImageProcessor.Imaging.Formats;
    
    using Umbraco.Core;
    using Umbraco.Core.Configuration;
    using Umbraco.Core.Configuration.UmbracoSettings;
    using Umbraco.Core.IO;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    

    public class ApplicationEvents : ApplicationEventHandler { protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { // Tap into the Saving event MediaService.Saving += (sender, args) => { MediaFileSystem mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider

            foreach (IMedia media in args.SavedEntities)
            {
                if (media.HasProperty("umbracoFile"))
                {
                    // Make sure it's an image.
                    string path = media.GetValue<string>("umbracoFile");
                    string extension = Path.GetExtension(path).Substring(1);
                    if (supportedTypes.InvariantContains(extension))
                    {
                        // Resize the image to 1920px wide, height is driven by the
                        // aspect ratio of the image.
                        string fullPath = mediaFileSystem.GetFullPath(path);
                        using (ImageFactory imageFactory = new ImageFactory(true))
                        {
                            ResizeLayer layer = new ResizeLayer(new Size(1920, 0), ResizeMode.Max)
                            {
                                Upscale = false
                            };
    
                            imageFactory.Load(fullPath)
                                        .Resize(layer)
                                        .Save(fullPath);
                        }
                    }
                }
            }
        };
    }
    

    }

    Which now doesn't throw any dependency errors when loading the website, but does throw an undefined error when saving images, telling me to check the log, where there is no info (i.e. nothing is appended to the log when I get the error).

    Any further advice, much appreciated.

    Just for clarity I am running Umbraco 7.4.3

    Thanks

    Stephen.

  • Stephen 94 posts 255 karma points
    May 23, 2016 @ 01:12
    Stephen
    0

    OK, so I (I say I, but you know what I mean) got it to work. Duno exactly what the problem was, but here is what I did.

    I found this code:

    https://gist.github.com/JimBobSquarePants/ed90a0f9b34cd14a3a2a

    Which appears to be a slightly fleshed out version of what I found at the previous link stated, still by "James Jackson-South" by the looks of it. So thanks again James.

    I took everything inside the namespace and dropped it in my .cs file located in the app_code folder. Reloaded the site (waited for it to rebuild), and hey presto it worked.

    So Hopefully this is enough info for anyone else at my level to add an image preprocessor to resize the image before it is saved in the Umbraco media area.

    Thanks guys for the pointers on the way, I would not have found it with out you.

    Stephen

  • Stephen 94 posts 255 karma points
    May 23, 2016 @ 01:53
    Stephen
    0

    Just as a quick note. At some point I managed to create a problem with the Examine index getting locked.

    I just had to cycle the the App pool to fix it on IIS. I wouldn't expect others to have this problem, I suspect it was caused when I was crashing around with abandon...

  • James Jackson-South 489 posts 1747 karma points c-trib
    May 23, 2016 @ 14:06
    James Jackson-South
    0

    Hi Stephen,

    Sorry I didn't get back to you sooner. I'm based in Sydney so timezones are sometimes an issue.

    I'm glad you found that gist. It's pretty comprehensive and should cater for every situation I can think of.

    Nice work getting to grips with everything. If you ever need any more help please let me know.

    Cheers

    James

Please Sign in or register to post replies

Write your reply to:

Draft