Update ImageSharpImageUrlGenerator
This commit is contained in:
@@ -9,59 +9,102 @@ namespace Umbraco.Cms.Infrastructure.Media
|
||||
{
|
||||
public class ImageSharpImageUrlGenerator : IImageUrlGenerator
|
||||
{
|
||||
public IEnumerable<string> SupportedImageFileTypes => new[] { "jpeg", "jpg", "gif", "bmp", "png" };
|
||||
public IEnumerable<string> SupportedImageFileTypes => new[]
|
||||
{
|
||||
"jpeg",
|
||||
"jpg",
|
||||
"gif",
|
||||
"bmp",
|
||||
"png"
|
||||
};
|
||||
|
||||
public string GetImageUrl(ImageUrlGenerationOptions options)
|
||||
{
|
||||
if (options == null) return null;
|
||||
if (options == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);
|
||||
var imageUrl = new StringBuilder(options.ImageUrl);
|
||||
|
||||
if (options.FocalPoint != null) AppendFocalPoint(imageProcessorUrl, options);
|
||||
else if (options.Crop != null) AppendCrop(imageProcessorUrl, options);
|
||||
else if (options.DefaultCrop) imageProcessorUrl.Append("?anchor=center&mode=crop");
|
||||
if (options.FocalPoint != null)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "?rxy={0},{1}", options.FocalPoint.Top, options.FocalPoint.Left);
|
||||
}
|
||||
else if (options.Crop != null)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "?crop={0},{1},{2},{3}&cropmode=percentage", options.Crop.X1, options.Crop.Y1, options.Crop.X2, options.Crop.Y2);
|
||||
}
|
||||
else if (options.DefaultCrop)
|
||||
{
|
||||
imageUrl.Append("?anchor=center&mode=crop");
|
||||
}
|
||||
else
|
||||
{
|
||||
imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? ImageCropMode.Crop).ToString().ToLower());
|
||||
imageUrl.Append("?mode=").Append((options.ImageCropMode ?? ImageCropMode.Crop).ToString().ToLowerInvariant());
|
||||
|
||||
if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToString().ToLower());
|
||||
if (options.ImageCropAnchor != null)
|
||||
{
|
||||
imageUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToString().ToLowerInvariant());
|
||||
}
|
||||
}
|
||||
|
||||
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");
|
||||
|
||||
//Only put quality here, if we don't have a format specified.
|
||||
//Otherwise we need to put quality at the end to avoid it being overridden by the format.
|
||||
if (options.Quality.HasValue && hasFormat == false) imageProcessorUrl.Append("&quality=").Append(options.Quality);
|
||||
if (options.HeightRatio.HasValue) imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
|
||||
if (options.WidthRatio.HasValue) imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
|
||||
if (options.Width.HasValue) imageProcessorUrl.Append("&width=").Append(options.Width);
|
||||
if (options.Height.HasValue) imageProcessorUrl.Append("&height=").Append(options.Height);
|
||||
if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false");
|
||||
if (!string.IsNullOrWhiteSpace(options.AnimationProcessMode)) imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
|
||||
if (!string.IsNullOrWhiteSpace(options.FurtherOptions)) imageProcessorUrl.Append(options.FurtherOptions);
|
||||
// Only put quality here, if we don't have a format specified.
|
||||
// Otherwise we need to put quality at the end to avoid it being overridden by the format.
|
||||
if (options.Quality.HasValue && hasFormat == false)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&quality={0}", options.Quality.Value);
|
||||
}
|
||||
|
||||
//If furtherOptions contains a format, we need to put the quality after the format.
|
||||
if (options.Quality.HasValue && hasFormat) imageProcessorUrl.Append("&quality=").Append(options.Quality);
|
||||
if (!string.IsNullOrWhiteSpace(options.CacheBusterValue)) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
|
||||
if (options.HeightRatio.HasValue)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&heightratio={0}", options.HeightRatio.Value);
|
||||
}
|
||||
|
||||
return imageProcessorUrl.ToString();
|
||||
}
|
||||
if (options.WidthRatio.HasValue)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&widthratio={0}", options.WidthRatio.Value);
|
||||
}
|
||||
|
||||
private void AppendFocalPoint(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
|
||||
{
|
||||
imageProcessorUrl.Append("?rxy=");
|
||||
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(",");
|
||||
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
if (options.Width.HasValue)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&width={0}", options.Width.Value);
|
||||
}
|
||||
|
||||
private void AppendCrop(StringBuilder imageProcessorUrl, ImageUrlGenerationOptions options)
|
||||
{
|
||||
imageProcessorUrl.Append("?crop=");
|
||||
imageProcessorUrl.Append(options.Crop.X1.ToString(CultureInfo.InvariantCulture)).Append(",");
|
||||
imageProcessorUrl.Append(options.Crop.Y1.ToString(CultureInfo.InvariantCulture)).Append(",");
|
||||
imageProcessorUrl.Append(options.Crop.X2.ToString(CultureInfo.InvariantCulture)).Append(",");
|
||||
imageProcessorUrl.Append(options.Crop.Y2.ToString(CultureInfo.InvariantCulture));
|
||||
imageProcessorUrl.Append("&cropmode=percentage");
|
||||
if (options.Height.HasValue)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&height={0}", options.Height.Value);
|
||||
}
|
||||
|
||||
if (options.UpScale == false)
|
||||
{
|
||||
imageUrl.Append("&upscale=false");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.AnimationProcessMode))
|
||||
{
|
||||
imageUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.FurtherOptions))
|
||||
{
|
||||
imageUrl.Append(options.FurtherOptions);
|
||||
}
|
||||
|
||||
// If furtherOptions contains a format, we need to put the quality after the format.
|
||||
if (options.Quality.HasValue && hasFormat)
|
||||
{
|
||||
imageUrl.AppendFormat(CultureInfo.InvariantCulture, "&quality={0}", options.Quality.Value);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.CacheBusterValue))
|
||||
{
|
||||
imageUrl.Append("&rnd=").Append(options.CacheBusterValue);
|
||||
}
|
||||
|
||||
return imageUrl.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user