Skip to content

Commit

Permalink
Allow media width and height values without a token (#7506)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanmarcussen authored Nov 4, 2020
1 parent 8b498f2 commit 54658bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,16 @@ public void Configure(ImageSharpMiddlewareOptions options)
context.Commands[command.Key] = command.Value;
}

// Do not check supported sizes here, as with tokenization any size is allowed.
// Do not evaluate supported sizes here, as with tokenization any size is allowed.
}
else
{
// When tokenization is enabled, but a token is not present, clear the commands to no-op the image processing pipeline.
context.Commands.Clear();
return Task.CompletedTask;
ValidateTokenlessCommands(context, mediaOptions);
}
}
else
{
// The following commands are not supported without a tokenized query string.
context.Commands.Remove(ResizeWebProcessor.Xy);
context.Commands.Remove(ImageVersionProcessor.VersionCommand);

// Width and height must be part of the supported sizes array when tokenization is disabled.
if (context.Commands.TryGetValue(ResizeWebProcessor.Width, out var widthString))
{
var width = context.Parser.ParseValue<int>(widthString, context.Culture);

if (Array.BinarySearch<int>(mediaOptions.SupportedSizes, width) < 0)
{
context.Commands.Remove(ResizeWebProcessor.Width);
}
}

if (context.Commands.TryGetValue(ResizeWebProcessor.Height, out var heightString))
{
var height = context.Parser.ParseValue<int>(heightString, context.Culture);

if (Array.BinarySearch<int>(mediaOptions.SupportedSizes, height) < 0)
{
context.Commands.Remove(ResizeWebProcessor.Height);
}
}
ValidateTokenlessCommands(context, mediaOptions);
}

// The following commands are not supported by default.
Expand All @@ -113,5 +88,33 @@ public void Configure(ImageSharpMiddlewareOptions options)
return Task.CompletedTask;
};
}

private static void ValidateTokenlessCommands(ImageCommandContext context, MediaOptions mediaOptions)
{
// The following commands are not supported without a tokenized query string.
context.Commands.Remove(ResizeWebProcessor.Xy);
context.Commands.Remove(ImageVersionProcessor.VersionCommand);

// Width and height must be part of the supported sizes array when tokenization is disabled.
if (context.Commands.TryGetValue(ResizeWebProcessor.Width, out var widthString))
{
var width = context.Parser.ParseValue<int>(widthString, context.Culture);

if (Array.BinarySearch<int>(mediaOptions.SupportedSizes, width) < 0)
{
context.Commands.Remove(ResizeWebProcessor.Width);
}
}

if (context.Commands.TryGetValue(ResizeWebProcessor.Height, out var heightString))
{
var height = context.Parser.ParseValue<int>(heightString, context.Culture);

if (Array.BinarySearch<int>(mediaOptions.SupportedSizes, height) < 0)
{
context.Commands.Remove(ResizeWebProcessor.Height);
}
}
}
}
}
10 changes: 6 additions & 4 deletions src/docs/reference/modules/Media/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Convert the input URL to create a resized image with the specified size argument
#### Arguments

Refer [Query string tokens](#query-string-tokens) to understand the valid values for a width or height command,
and how the query string will defer from the examples provided.
and how the query string will differ from the examples provided.

#### `width` (or first argument)

Expand Down Expand Up @@ -403,9 +403,9 @@ The `MediaText[]` is kept in sync with the `Paths[]` array and the index for a g

Image anchors are an optional setting, off by default, on the `MediaField`.

When enabled they allow a media field to provide an anchor x or y value for use when cropping, or padding the image.
When enabled they allow a media field to provide an anchor point, or x and y value for use when cropping, or padding the image.

The anchor value provided can be used to specify the center of a crop or pad.
The anchor value provided can be used to specify the center point of a crop or pad.

When the setting is enabled the template must read and provide the value to the resizing helpers or filters.

Expand Down Expand Up @@ -450,10 +450,12 @@ those commands. e.g.
Tokens are only available from the [Preview Feed](../../../getting-started/preview-package-source)
Prior to this the width or height values are limited to `16`, `32`, `50`, `100`, `160`, `240`, `480`, `600`, `1024`, `2048`.

## Video
## Videos

<iframe width="560" height="315" src="https://www.youtube.com/embed/BQHUlvPFRR4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

<iframe width="560" height="315" src="https://www.youtube.com/embed/K0_i4vj00yM" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

## CREDITS

### ImageSharp
Expand Down

0 comments on commit 54658bc

Please sign in to comment.