mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.4 KiB
64 lines
2.4 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="ImageFactoryExtensions.cs" company="James South">
|
|
// Copyright (c) James South.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace ImageProcessor.Web
|
|
{
|
|
#region Using
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using ImageProcessor.Processors;
|
|
using ImageProcessor.Web.Config;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Extends the ImageFactory class to provide a fluent API.
|
|
/// </summary>
|
|
public static class ImageFactoryExtensions
|
|
{
|
|
/// <summary>
|
|
/// The object to lock against.
|
|
/// </summary>
|
|
private static readonly object SyncRoot = new object();
|
|
|
|
/// <summary>
|
|
/// Auto processes image files based on any query string parameters added to the image path.
|
|
/// </summary>
|
|
/// <param name="factory">
|
|
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class
|
|
/// that this method extends.
|
|
/// </param>
|
|
/// <returns>
|
|
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
|
|
/// </returns>
|
|
public static ImageFactory AutoProcess(this ImageFactory factory)
|
|
{
|
|
if (factory.ShouldProcess)
|
|
{
|
|
// It's faster to lock and run through our activated list than to create new instances.
|
|
lock (SyncRoot)
|
|
{
|
|
// Get a list of all graphics processors that have parsed and matched the query string.
|
|
List<IGraphicsProcessor> graphicsProcessors =
|
|
ImageProcessorConfig.Instance.GraphicsProcessors
|
|
.Where(x => x.MatchRegexIndex(factory.QueryString) != int.MaxValue)
|
|
.OrderBy(y => y.SortOrder)
|
|
.ToList();
|
|
|
|
// Loop through and process the image.
|
|
foreach (IGraphicsProcessor graphicsProcessor in graphicsProcessors)
|
|
{
|
|
Image img = graphicsProcessor.ProcessImage(factory);
|
|
factory.Update(img);
|
|
}
|
|
}
|
|
}
|
|
|
|
return factory;
|
|
}
|
|
}
|
|
}
|
|
|