1

Is there a way to programmatically get the Sitecore media files' size which is being optimized by Dianoga?

We need to show the image size on the site.

MediaItem.Size property is getting us the original image size - which isn't matching the rendered image size which is coming through the Dianoga processor.

Any ideas?

6
  • On the live site you are showing the media size? Nov 22, 2018 at 15:51
  • Yes, we need to show in a certain design, because we are giving the option to download the image to the user.
    – Durga Bose
    Nov 22, 2018 at 16:54
  • You would not have users downloading the optimised images though, surely? these are lossy web-optimised versions of the original content. Nov 22, 2018 at 17:02
  • Can I turn off optimization by adding something to the url? Since I still want the other images on the site to be optimized?
    – Durga Bose
    Nov 22, 2018 at 17:08
  • 1
    Can we stick to one question at the time, please? :-) Nov 22, 2018 at 17:09

1 Answer 1

3

Server-side, no - probably. Dianoga acts as a layer between the Sitecore Media Library and the caching mechanics.

You can get the byte size of the images client-side however. Which would allow you to render these in your page.

This example lists sizes for all images on your page. Adjust as necessary.

var imgElems = document.getElementsByTagName('img');
for ( var i=0, len = imgElems.length; i < len; i++ ) 
{
    var url = imgElems[i].src || imgElems[i].href;
    if (url && url.length > 0)
    {
        var iTime = performance.getEntriesByName(url)[0];
        console.log(iTime.transferSize); //or encodedBodySize, decodedBodySize
    }
}

Source: https://stackoverflow.com/a/45409613/81631

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.