How to disable ImageProcessor's upscaling feature

Published on in C# and Episerver

Last updated on

Disabling the upscaling feature (by default or completely) can be done via ImageProcessor's ValidatingRequest event. It's not possible via a config file.

Table of contents

What's upscaling?

ImageProcessor's resize method lets you modify the dimensions of an image on the fly, by using URL parameters. For example:

<img alt="" src="/my-image.jpg?width=2000&height=1000" />
<img alt="" src="/my-image.jpg?width=2000&heightratio=0.5625" />

Upscaling means that an image is resized to dimensions greater than its original dimensions. This can cause blurry images, so you might want to disable the upscaling feature.

You can disable upscaling on a case-by-case basis with the upscale URL parameter:

<img alt="" src="/my-image.jpg?width=2000&height=1000&upscale=false" />
<!-- ^^^^^^^^^^^^^^ -->

Doing so manually will however quickly become cumbersome. Let's see how to disable it programmatically.

Not possible via a config file

According to GitHub issue #583 and issue #767, it's not possible to disable the upscaling feature via a config file.

Disable via an event

The disabling of the upscaling feature needs to be done via ImageProcessor's ValidatingRequest event. See the Events section in ImageProcessor's docs.

Add the code maybe to the Application_Start event in Global.asax. Or if using Episerver, create an initialization module:

[InitializableModule]
public class ImageProcessorEventsInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
ImageProcessingModule.ValidatingRequest += DisableUpscalingByDefault;
// or
ImageProcessingModule.ValidatingRequest += DisableUpscaling;
}

public void Uninitialize(InitializationEngine context)
{
ImageProcessingModule.ValidatingRequest -= DisableUpscalingByDefault;
// or
ImageProcessingModule.ValidatingRequest -= DisableUpscaling;
}

private void DisableUpscalingByDefault(ImageProcessingModule sender, ValidatingRequestEventArgs args)
{
// ...
}
// or
private void DisableUpscaling(ImageProcessingModule sender, ValidatingRequestEventArgs args)
{
// ...
}
}

Disable by default

/// <summary>
/// Disable ImageProcessor's upscaling feature by default.
/// Upscaling can be enabled for individual images
/// with the <c>upscale=true</c> URL parameter.
/// </summary>
/// <remarks>
/// Not possible via configuration:
/// <list type="bullet">
/// <item>https://imageprocessor.org/imageprocessor-web/imageprocessingmodule/#events</item>
/// <item>https://github.com/JimBobSquarePants/ImageProcessor/issues/583</item>
/// <item>https://github.com/JimBobSquarePants/ImageProcessor/issues/767</item>
/// </list>
///
/// Code from https://mtsknn.fi/blog/how-to-disable-imageprocessors-upscaling-feature/
/// </remarks>
private void DisableUpscalingByDefault(ImageProcessingModule sender, ValidatingRequestEventArgs args)
{
if (String.IsNullOrWhiteSpace(args.QueryString))
{
return;
}

var queryCollection = HttpUtility.ParseQueryString(args.QueryString);

// The URL parameter has already been set,
// so let's not modify it
if (queryCollection.AllKeys.Contains("upscale"))
{
return;
}

queryCollection.Add("upscale", "false");
args.QueryString = queryCollection.ToString();
}

Disable completely

/// <summary>
/// Disable ImageProcessor's upscaling feature completely.
/// Image URLs' <c>upscale</c> URL parameter won't have any effect.
/// </summary>
/// <remarks>
/// Not possible via configuration:
/// <list type="bullet">
/// <item>https://imageprocessor.org/imageprocessor-web/imageprocessingmodule/#events</item>
/// <item>https://github.com/JimBobSquarePants/ImageProcessor/issues/583</item>
/// <item>https://github.com/JimBobSquarePants/ImageProcessor/issues/767</item>
/// </list>
///
/// Code from https://mtsknn.fi/blog/how-to-disable-imageprocessors-upscaling-feature/
/// </remarks>
private void DisableUpscaling(ImageProcessingModule sender, ValidatingRequestEventArgs args)
{
if (String.IsNullOrWhiteSpace(args.QueryString))
{
return;
}

var queryCollection = HttpUtility.ParseQueryString(args.QueryString);
queryCollection.Set("upscale", "false");
args.QueryString = queryCollection.ToString();
}

Notice the usage of queryCollection.Set() instead of queryCollection.Add().

Source

Original solution found in the GitHub issue #583.