Try our conversational search powered by Generative AI!

Stephan Lonntorp
Feb 1, 2018
  2343
(2 votes)

Clean and unique media URLs with ImageResizer.NET presets

For a while I've been working on cleaning up the urls generated for images, to ensure that they are clean, cacheable, and preferrably querystring-free. Previously, I have been using IIS URL Rewrites to achieve this, but I have always known there are better ways, but just haven't gotten around to it yet. Better, meaning fewer moving parts, and a drop-in solution.

Yes, I am well aware of the built-in functionality of naming blob properties on media items and tacking on an attribute to get a scaled image, but image optimization is far more than just resizing, and a component like ImageResizer is a really great tool to have in your site. It really is worth the cost.

So, without further ado, here's "UniqueUrlFolderPresets", I didn't know what to name it, but this name is as good as any :)

What does it do?

It takes a media URL, like "/globalassets/my-images/my-image.jpg" and prepends it with a 8 character hash based off of the media item's last saved date. Out of the box, it also adds a max-age header, that caches it for a year. Yes, that's configurable. I know, this isn't a very new concept, but AFAIK no one has done it in conjunction with resizing images. This is the "UniqueUrl" part of the add-on.

The second part, the "FolderPresets" part, has support for imageresizing.net's presets as part of the segments of the URL, as opposed to tacking on an ugly querystring. I've added a couple of really simple UrlHelper extensions to help with generating the URLs, but in essence, if you prepend your image URL with "/optimized/<preset>", it will apply that preset, if it exists, to the image request. If the preset doesn't exist, it'll give you a 404. Yes, the "optimized" part is also configurable.

It should work with any other ImageResizer plugins, but I haven't tested it that much, please help with that. If you have clientcaching configured for imageresizer, this plugin replaces that, so you can remove it.

If there's no hash in the URL, you'll get a 301 redirect to the latest version (hash), and if there's an old hash in the url, you'll also get a 301 redirect.

Hopefully, someone besides me will find this useful. And as usual, I'm open to feedback and suggestions. Use github for that.

There's also a back-port for CMS 10, if you have yet to upgrade, you'll find it here.

Feb 01, 2018

Comments

valdis
valdis Feb 1, 2018 03:34 PM

why just not merge together with core IR plugin for Epi? ;))

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024