Last active
January 12, 2018 14:34
-
-
Save jordanrobinson/d5740f50928fb5cbe494fade98a40353 to your computer and use it in GitHub Desktop.
Image Processor Blog Post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<mediaLibrary> | |
<requestProtection> | |
<protectedMediaQueryParameters> | |
<parameter description="tint" name="tint"/> | |
</protectedMediaQueryParameters> | |
<customMediaQueryParameters> | |
<parameter description="tint" name="tint"/> | |
</customMediaQueryParameters> | |
</requestProtection> | |
<requestParser type="Sitecore.Resources.Media.MediaRequest, Sitecore.Kernel"> | |
<patch:attribute name="type">JordanRobinson.Requests.TintMediaRequest, JordanRobinson.Processors</patch:attribute> | |
</requestParser> | |
</mediaLibrary> | |
<pipelines> | |
<getMediaStream> | |
<processor type="JordanRobinson.Processors.TintProcessor, JordanRobinson.Processors" | |
patch:before="processor[@type='Sitecore.Resources.Media.ThumbnailProcessor, Sitecore.Kernel']"/> | |
</getMediaStream> | |
</pipelines> | |
</sitecore> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web; | |
using Sitecore.Configuration; | |
using Sitecore.Diagnostics; | |
using Sitecore.Resources.Media; | |
namespace JordanRobinson.Requests | |
{ | |
public class TintMediaRequest : MediaRequest | |
{ | |
private HttpRequest _innerRequest; | |
private MediaUrlOptions _mediaQueryString; | |
private MediaUri _mediaUri; | |
private MediaOptions _options; | |
public override MediaRequest Clone() | |
{ | |
return new TintMediaRequest | |
{ | |
_innerRequest = _innerRequest, | |
_mediaUri = _mediaUri, | |
_options = _options, | |
_mediaQueryString = _mediaQueryString | |
}; | |
} | |
protected override MediaOptions GetOptions() | |
{ | |
var queryString = InnerRequest.QueryString; | |
if (!string.IsNullOrEmpty(queryString.Get("tint"))) | |
{ | |
_options = new MediaOptions(); | |
ProcessCustomParameters(_options); | |
if (!_options.CustomOptions.ContainsKey("tint") && !string.IsNullOrEmpty(queryString.Get("tint"))) | |
{ | |
_options.CustomOptions.Add("tint", queryString.Get("tint")); | |
} | |
} | |
else | |
{ | |
var mediaQueryString = GetMediaQueryString(); | |
_options = new MediaOptions | |
{ | |
AllowStretch = mediaQueryString.AllowStretch, | |
BackgroundColor = mediaQueryString.BackgroundColor, | |
IgnoreAspectRatio = mediaQueryString.IgnoreAspectRatio, | |
Scale = mediaQueryString.Scale, | |
Width = mediaQueryString.Width, | |
Height = mediaQueryString.Height, | |
MaxWidth = mediaQueryString.MaxWidth, | |
MaxHeight = mediaQueryString.MaxHeight, | |
Thumbnail = mediaQueryString.Thumbnail | |
}; | |
if (mediaQueryString.DisableMediaCache) | |
{ | |
_options.UseMediaCache = false; | |
} | |
var keys = queryString.AllKeys; | |
for (var i = 0; i < keys.Length; i = i + 1) | |
{ | |
var value = keys[i]; | |
if (value != null && queryString.Get(value) != null) | |
{ | |
_options.CustomOptions[value] = queryString.Get(value); | |
} | |
} | |
} | |
if (!IsRawUrlSafe) | |
{ | |
if (Settings.Media.RequestProtection.LoggingEnabled) | |
{ | |
var urlReferrer = GetUrlReferrer(); | |
Log.SingleError($"MediaRequestProtection: An invalid/missing hash value was encountered. The expected hash value: {HashingUtils.GetAssetUrlHash(this.InnerRequest.Path)}. Media URL: { InnerRequest.Path }, Referring URL: { (string.IsNullOrEmpty(urlReferrer) ? "(empty)" : urlReferrer)}", this); | |
} | |
_options = new MediaOptions(); | |
} | |
return _options; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Resources.Media; | |
namespace JordanRobinson.Processors | |
{ | |
public class TintProcessor | |
{ | |
private static readonly string[] ImageExtensions = { "bmp", "jpeg", "jpg", "png", "gif" }; | |
public void Process(GetMediaStreamPipelineArgs args) | |
{ | |
Assert.ArgumentNotNull(args, nameof(args)); | |
var outputStream = args.OutputStream; | |
if (outputStream == null) | |
{ | |
return; | |
} | |
if (!ImageExtensions.Any(i => i.Equals(args.MediaData.Extension, StringComparison.InvariantCultureIgnoreCase))) | |
{ | |
return; | |
} | |
var tint = args.Options.CustomOptions["tint"]; | |
if (!string.IsNullOrEmpty(tint)) | |
{ | |
var color = ColorTranslator.FromHtml(tint); | |
var outputMedia = Stream.Synchronized(GetTintedImage(color, outputStream.MediaItem)); | |
args.OutputStream = new MediaStream(outputMedia, args.MediaData.Extension, outputStream.MediaItem); | |
} | |
} | |
private Stream GetTintedImage(Color color, MediaItem mediaItem) | |
{ | |
var outputStream = new MemoryStream(); | |
var mediaStream = mediaItem.GetMediaStream(); | |
var image = Image.FromStream(mediaStream); | |
var proc = new ImageProcessor.ImageFactory(); | |
proc.Load(image); | |
proc = proc.Tint(color); | |
proc.Save(outputStream); | |
return outputStream; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment