Images not Resizing in Sitecore 7.5, Sitecore 8.0
Sitecore 7.5 brings some changes to the way images are resized on the server, and it may mean that you’ll find some images on your site aren’t being resized after upgrading. These changes are intentional, and provide better security for media requests, but depending on your past implementations…you may see this issue on your site.
Sitecore has always been able to resize images on the server by passing parameters to the image rendering controls, <sc:image> and <sc:FieldRenderer>.
In the past, the resulting image URL of this was the normal image path with a query string appended showing the parameters.
i.e http://mywebsite/~/media/System/Simulator Backgrounds/Android Phone.ashx?w=50
Shinks down to this….
From Sitecore 7.5 onwards, if you attempt to load an image URL directly (by just appending a query string like the one above), the image will not be resized, and the full image will be shown.
Initially, I thought this was a bug…but Sitecore’s own Kerry Bellerose pointed me in the right direction, saying that there is new added security to media requests. (Credit given to Adam Najmanowicz and Marek Musielak for the find).
If you have this issue, the logs will tell you something like:-
ERROR MediaRequestProtection: An invalid/missing hash value was encountered. The expected hash value: 784C89D040E502AAA0AEAA2E66DE223D41D6F0B0. Media URL: /~/media/System/Simulator Backgrounds/Android Phone.ashx?w=50, Referring URL: (empty)
From the 7.5 release notes:-
When the feature is enabled, Sitecore automatically signs image URLs that are rendered by the pipeline and adds a hash value to the query string. When processing an incoming media request, image resizing/scaling is skipped if any of the relevant query string parameters in the image URL have been altered or any extra resizing parameters have been appended to the URL. In these cases, Sitecore returns the original, unaltered image.
So if you’re outputting your image using a control that utilizes Sitecore’s renderField pipeline (like , etc.) image URLs will have a hash value appended on the query string, and they’ll be resized correctly.
If you are manually setting up the media URL, you will need to generate the correct hash value and append it, using one of the following methods:
- in XSLT renderings, you can use sc:SignMediaUrl(url) or sc:GetSignedMediaUrl(…).
- in C# code, you can use HashingUtils.ProtectAssetUrl(url) to get the full media URL with hash included.
- in C# code, you can use GetAssetUrlHash(url) to only return the hash value for the provided URL.
- in static image URLs (for example, in CSS or aspx files) you can find the appropriate hash from a new /sitecore/admin/MediaHash.aspx page. You will be able to enter a media URL and, with the click of a button, generate the corresponding hash value.
- rewrite the code to use a control that runs through Sitecore’s renderField pipeline (, etc.)
The appropriate settings are all setup in /App_Config/Include/Sitecore.Media.RequestProtection.config, including a setting ‘Media.RequestProtection.Enabled’.
While you could just disable that setting, it is highly recommended that you keep it enabled, and comb your current solution for existing images that don’t go through the pipeline.