provider · tools

How to enable Grayscale built-in feature on Sitecore XP 8

My colleagues were playing with Sitecore built-in Grayscale feature on Sitecore CMS 7.2 by simply adding a media query parameter gray=1 to a media URL. However, I cannot play with it like that on Sitecore XP 8.2 Update 3 by default.

gc_4.png

What’s wrong?

Using Sitecore Admin tool Show Config (/sitecore/admin/showconfig.aspx), I’m able to see GrayscaleProcessor is still available, it means there is something wrong with the media URL

gc_1.png

Searching “gray” on Sitecore Admin tool Show Config (/sitecore/admin/showconfig.aspx), I see this one:

gc_2

I suppose that I should disable MediaRequestProtection feature by editing \App_Config\Include\Sitecore.Media.RequestProtection.config

gc_3

… then it works properly

gc_5.png

In my opinion, the default one MediaProvider.GetMediaUrl(MediaItem item, MediaUrlOptions options) will not work as expected if I want to add media query parameter gray to media URL because MediaUrlOptions has no Greyscale item. It causes MediaRequestProtection calculates the hash without gray parameter as always.

MediaRequestProtection restricts media URLs that contain dynamic image-scaling parameters so that only server-generated requests are processed (in Release Note). It means I cannot play with Grayscale by manually adding a media query parameter gray=1 to a media URL on Sitecore XP (version >= 7.5).

The Solutions

Solution 1: disabling MediaRequestProtection feature. However, I don’t want to do it as I would lose a major vulnerability fix as mentioned here.

Solution 2: using the HashingUtils.ProtectAssetUrl(string url) method as this one

gc_10Solution 3: creating a custom Media Provider which alternates the default one. The main purpose is to add media query parameter gray=1 to image URLs only if they’re grayscaleable before MediaRequestProtection calculates the hash, otherwise let the default Media Provider handle them.

gc_8

You may want to play with my own one.

  • How to get it: download it as a Sitecore package then install it to your Sitecore instance via Installation Wizard
  • How to play with it:
    • open \App_Config\Include\z.Vhs.MediaProviderForGrayscale.config
    • update Vhs.MediaProviderForGrayscale.Enabled to true and configure supported media types via Vhs.MediaProviderForGrayscale.SupportedExtensions
    • gc_7.png
    • open a browser and navigate to a webpage which has image(s) on, hope that you would see something like this
    • gc_9

Not Good Enough? Please feel free to download the source code and add your own enhancements if necessary.

Got issues?

Please send your issues (with screenshots if possible) to viet.hoang.sitecore@gmail.com so that I have a chance to understand your problem and be able to suggest the solution.

Happy Site Coding!

Advertisement

2 thoughts on “How to enable Grayscale built-in feature on Sitecore XP 8

  1. Why not just hash the Url using the HashingUtils.ProtectAssetUrl(“url”) method – it would just work then and no need to write another handler for what Sitecore already handles.

    Liked by 1 person

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.