Thursday, June 22, 2017

More on Image Resizing

Apparently starting with Sitecore 7.5, Sitecore has made it so that parameters for image resizing require a hash in order to work.  To programatically generate a url with the hash, the following code can be used:


Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(
                               Sitecore.Resources.Media.MediaManager.GetMediaUrl(
                                   myMediaItem,
                                   new MediaUrlOptions()
                                   {
                                       Language = Context.Language,
                                       Width = 100,
                                       Height = 75
                                   }))

If you only need the hash value for the provided image URL, you could use the GetAssertUrlHash(url).

For more details please see this blog entry here: http://kirkegaard-at.blogspot.sg/2015/06/media-hash-and-resizing.html

For more on MediaUrlOptions:  https://community.sitecore.net/developers/f/8/t/1654