Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow media width and height values without a token #7506

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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