ImageProcessor: Disable upscaling by default or completely
Disabling the upscaling feature (part of the resizing feature)
can be done via ImageProcessor's ValidatingRequest
event.
Not possible via a config file.
Table of contents
Disable via an event
The disabling 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 (part of the resizing feature) by default.
/// </summary>
/// <remarks>
/// Not possible via configuration.
/// See:
/// <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>
/// </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 (part of the resizing feature).
/// </summary>
/// <remarks>
/// ... (see previous section)
/// </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()
.
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.
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 causes 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. That's where this recipe comes into play – disabling upscaling by default or completely.
Source
Original solution found in the GitHub issue #583.