Help authors find large unoptimized images in Sitecore

Without proper reporting it can be difficult for content authors to manage large amounts of content. Unless there is a really good governance policy in place, it can sometimes become unwieldly to keep track of it all. Here is a helpful report that content authors can use to track down large and unoptimized images in the media library.

Clicking on any media item in the media library will show you the following fields.

Fields on Media Item

Using these fields we can do some analysis on the images that have been loaded into the system.

Report Details

When the report is run, the users will be provided some information on what the cell highlighting means and some instructions on how to handle images properly for websites. Here is what it says:

Summary Of Media Library Items

  1. Images greater than 1MB in size are highlighted for review.
  2. Images that are not in the JPEG format are highlighted for review

Most attention should be paid to images that are greater than 1MB in size.

Actions to take

  1. Convert Images: Use JPEGs for photos normal images, PNGs for graphics or when you need transparent images, and GIF for animated images only (try to use video instead of animated Gifs if possible).
  2. Size Images appropriately: Adhere to the size requirements for the rendering and do not exceed.
  3. Compress Images: Jpegs can be compressed. Try for 89% to 93% quality. Review image after as each image will differ in quality after compression.

Fields

  • Item Name – The name of the item in the media library.
  • Path – The path to the item in the media library.
  • Size – The size of the item in the media library.
  • Megabytes – The size of the item in megabytes.
  • Extension – The extension of the item in the media library.
  • Mime Type – The mine type of the item in the media library.
  • Used On – What other items this item is linked to.

Example

Use the code below and create an .aspx page called ImageReport.aspx. Add it to your Siteore/Admin folder and give it a try.

<%@ Page Language="C#" %>
<%@ Import Namespace="Sitecore" %>
<%@ Import Namespace="Sitecore.Links" %>
<%@ Import Namespace="Sitecore.Data" %>
<%@ Import Namespace="Sitecore.Data.Items" %>

<script runat="server">
    protected string currentItemId = null;
    protected string databaseName = null;
    protected Database database = null;
    protected string selectedCounties = null;
    protected string NotAssociatedCountries = null;
    protected bool checkFlag = false;
</script>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Summary Of Media Library Items</title>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
    <div class="container-fluid" id="summaryOfMediaLibraryItems">

        <h2 style="text-align: left; margin-top: 20px; margin-left: 10px;">Summary Of Media Library Items</h2>
        <ol>
            <li>Images greater than 1MB in size are highlighted for review.</li>
            <li>Images that are not in the JPEG format are highlighted for review</li>
        </ol>
        <p>
            Most attention should be paid to images that are greater than 1MB in size.<p>
                <p>Actions to take</p>
                <ol>
                    <li>Convert Images: Use JPEGs for photos normal images, PNGs for graphics or when you need transparent images, and GIF for animated images only. </li>
                    <li>Size Images appropriately: Adhere to the size requirements for the rendering and do not exceed.</li>
                    <li>
                    Compress Images: Jpegs can be compressed.  Try for 89% to 93% quality.  Review image after as each image will differ in quality after compression.
                </ol>



                <table id="tableRegion" class="table table-striped table-bordered" style="text-align: left; width: auto">
                    <thead>
                        <tr>
                            <th>Item Name</th>
                            <th>Path</th>
                            <th>Size</th>
                            <th>Megabytes</th>
                            <th>Extension</th>
                            <th>Mime Type</th>
                            <th>Used On</th>
                        </tr>
                    </thead>
                    <tbody>
                        <%
                            this.currentItemId = "{3D6658D8-A0BF-4E75-B3E2-D050FABCF4E1}";
                            this.databaseName = "master";
                            this.database = Database.GetDatabase(this.databaseName);
                            Item currentItem = database.GetItem(new ID(this.currentItemId));
                            List<Item> items = currentItem.Axes.GetDescendants()
                            .Where(x => x["Mime Type"].ToLower().Contains("png") || x["Mime Type"].ToLower().Contains("gif") || x["Mime Type"].ToLower().Contains("jpg") || x["Mime Type"].ToLower().Contains("jpeg"))
                            .OrderByDescending(x => string.IsNullOrEmpty(x["Size"]) ? 0 : System.Convert.ToInt64(x["Size"].ToString())).ToList();

                            if (currentItem != null && currentItem.Children.Count > 0)
                            {
                                foreach (Item childItem in items)
                                {
                                    StringBuilder linksBuilder = new StringBuilder();
                                    var mb = string.IsNullOrEmpty(childItem["Size"]) ? 0 : System.Convert.ToDecimal(childItem["Size"].ToString()) / 1048576;
                                    var mbCellColor = string.Empty;
                                    if (mb > 1)
                                    {
                                        mbCellColor = "style=\"background-color:yellow\"";
                                    }

                                    var ft = childItem.Fields["Extension"].ToString();
                                    var ftCellColor = string.Empty;
                                    if (!ft.ToLower().Contains("jpg"))
                                    {
                                        ftCellColor = "style=\"background-color:yellow\"";
                                    }

                                    ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(childItem);
                                    var links = new List<Item>();

                                    if (itemLinks.Any())
                                    {
                                        foreach (var link in itemLinks)
                                        {
                                            Item linkItem = database.GetItem(link.SourceItemID);
                                            if(linkItem != null)
                                            {
                                                links.Add(linkItem);
                                            }
                                        }
                                        
                                        foreach(var link in links)
                                        {
                                            linksBuilder.Append(link.Paths.Path);
                                            linksBuilder.Append("<br />");
                                        }
                                    }
                        %>

                        <tr>
                            <td><%  = childItem.Name %></td>
                            <td><%  = childItem.Paths.Path %></td>
                            <td><%  = childItem.Fields["Size"] %></td>
                            <td <%  = mbCellColor %>><%  = mb.ToString("0.##") %></td>
                            <td <%  = ftCellColor %>><%  = childItem.Fields["Extension"] %></td>
                            <td><%  = childItem.Fields["Mime Type"] %></td>
                            <td><%  = linksBuilder.ToString()  %></td>
                        </tr>
                        <%
                                }
                            } %>
                    </tbody>
                </table>
    </div>
</body>
</html>

About Phil Paris

Hi, my name is Phil Paris and I’m a Sitecore Architect and general Sitecore enthusiast. I’ve been working with Sitecore since 2013. Through this blog I will be sharing Sitecore insights, tips and tricks, best practices and general knowledge with the hopes to further the community at large. Please feel free to reach out to me at any time!

View all posts by Phil Paris →