Copied to clipboard

Flag this post as spam?

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


  • Nirav 7 posts 107 karma points
    Feb 11, 2022 @ 06:58
    Nirav
    0

    Need to exclude specific Media folder (Also Images inside that folder) from WebP rendering

    Hello All,

    I am a new developer in Umbraco. I am using Umbraco version 8.

    I need to exclude a specific media folder (Also Images inside that folder) from WebP rendering.

    I have implemented some code and it's working 100% fine. I have written code in RegisterStartUpComponent file. I have created a key and added it to Web.AppSetting.Config file.

    <add key="ImageProcessExcludes" value="qryig6v1,r61mpwj4" />
    

    My code as per below,

    private void ImageProcessingModule_ValidatingRequest(object sender, ValidatingRequestEventArgs e)
            {
                if (!e.Context.Request.Url.AbsolutePath.EndsWith(".gif"))
                {
                    var queryString = HttpUtility.ParseQueryString(e.QueryString);
                    if (e.Context.Request.AcceptTypes != null && e.Context.Request.AcceptTypes.Contains("image/webp"))
                    {
                        string[] imageProcessExcludes = ConfigurationManager.AppSettings["ImageProcessExcludes"].Split(',');
                        foreach (var item in imageProcessExcludes)
                        {
                            if(e.Context.Request.Url.AbsolutePath.Contains(item))
                            {
                                return;
                            }
                        }
    
                        queryString.Remove("format");
                        queryString["format"] = "webp";
                        var quality = ConfigurationManager.AppSettings["WebPQuality"] ?? "80";
                        if (queryString.Get("quality") != null)
                        {
                            quality = queryString["quality"];
                            queryString.Remove("quality");
                        }
                        queryString["quality"] = quality;// quality;
                    }
                    e.QueryString = queryString.ToString();
                }
            }
    

    Here the issue is when we upload any image into the Media folder, Umbraco creating own folder for each image file so it means If I have 10 images then it will create 10 folders for those images.

    Is this possible like we can create 1 folder and upload all images into that folder and just that folder we can exclude from rendering?

    Do Not Process Folder

    I have created this kind of folder in Media folder but after upload images, each image has own folder.

  • Nirav 7 posts 107 karma points
    Mar 28, 2022 @ 13:30
    Nirav
    100

    I have achieved my requirrement as per below code

    <add key="ImageProcessExcludes" value="mailer-" />
    

    Added above key into config file.

    private void ImageProcessingModule_ValidatingRequest(object sender, ValidatingRequestEventArgs e)
            {
                if (!e.Context.Request.Url.AbsolutePath.EndsWith(".gif"))
                {
                    var queryString = HttpUtility.ParseQueryString(e.QueryString);
                    if (e.Context.Request.AcceptTypes != null && e.Context.Request.AcceptTypes.Contains("image/webp"))
                    {
                        string[] imageProcessExcludes = ConfigurationManager.AppSettings["ImageProcessExcludes"].Split(',');
                        foreach (var item in imageProcessExcludes)
                        {
                            if (e.Context.Request.Url.AbsolutePath.Split('/').LastOrDefault().StartsWith(item))
                            {
                                return;
                            }
                        }
    
                        queryString.Remove("format");
                        queryString["format"] = "webp";
                        if (queryString.Get("quality") != null)
                        {
                            string quality = queryString["quality"];
                            queryString.Remove("quality");
                            queryString["quality"] = "";
                        }
                        else
                        {
                            queryString["quality"] = ConfigurationManager.AppSettings["WebPQuality"] ?? "70"; ;
                        }
                    }
                    e.QueryString = queryString.ToString();
                }
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft