Last active
August 29, 2015 14:01
-
-
Save asmagin/4132e7ddb00c5be9b124 to your computer and use it in GitHub Desktop.
Create round images in Sitecore
This file contains 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
<getMediaStream> | |
<processor type="Sitecore.Resources.Media.ThumbnailProcessor, Sitecore.Kernel" /> | |
<!-- Processor for rounded images should be installed before resize processor in order to get right propostions of image --> | |
<processor type="Example.ImageProcessor, Example" /> | |
<processor type="Sitecore.Resources.Media.ResizeProcessor, Sitecore.Kernel" /> | |
<processor type="Sitecore.Resources.Media.GrayscaleProcessor, Sitecore.Kernel" /> | |
</getMediaStream> |
This file contains 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
namespace Examples | |
{ | |
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Resources.Media; | |
public class ImageProcessor | |
{ | |
private static readonly string[] ImageExtentions = { "bmp", "jpeg", "jpg", "png", "gif" }; | |
public void Process(GetMediaStreamPipelineArgs args) | |
{ | |
Assert.ArgumentNotNull(args, "args"); | |
if (args.Options.Thumbnail) // should not apply for thumbnails | |
{ | |
return; | |
} | |
var outputStream = args.OutputStream; | |
if (outputStream == null) | |
{ | |
return; | |
} | |
var functionsArg = args.Options.CustomOptions["rnd"]; // searching for parameter in options | |
if (string.IsNullOrEmpty(functionsArg) || !functionsArg.Equals("1")) | |
{ | |
return; | |
} | |
var extension = args.MediaData.Extension; | |
// need to check extension, in order not to apply handler on PDF | |
if (!ImageExtentions.Any(x => x.Equals(args.MediaData.Extension, StringComparison.InvariantCultureIgnoreCase))) | |
{ | |
return; | |
} | |
args.OutputStream = this.GetRoundedImage(outputStream, extension, outputStream.MediaItem, args.Options.BackgroundColor); | |
} | |
private MediaStream GetRoundedImage(MediaStream outputStream, string extension, MediaItem mediaItem, Color backgroundColor) | |
{ | |
var sourceImage = (Bitmap)Image.FromStream(outputStream.Stream); | |
// image processing | |
var diameter = sourceImage.Width > sourceImage.Height ? sourceImage.Height : sourceImage.Width; | |
var cropRectangle = new Rectangle((sourceImage.Width - diameter) / 2, (sourceImage.Height - diameter) / 2, diameter, diameter); | |
var croppedImage = sourceImage.Clone(cropRectangle, sourceImage.PixelFormat); | |
var textureBrush = new TextureBrush(croppedImage); | |
var finalImage = new Bitmap(diameter, diameter); | |
var graphics = Graphics.FromImage(finalImage); | |
var solidBrush = new SolidBrush(extension == "png" ? Color.Transparent : backgroundColor); | |
graphics.FillRectangle(solidBrush, 0, 0, finalImage.Width, finalImage.Height); | |
graphics.FillEllipse(textureBrush, 0, 0, finalImage.Width, finalImage.Height); | |
graphics.Flush(); | |
var memoryStream = new MemoryStream(); | |
finalImage.Save(memoryStream, ImageFormat.Png); | |
memoryStream.Flush(); | |
memoryStream.Seek(0, SeekOrigin.Begin); | |
// image processing end | |
return new MediaStream(memoryStream, extension, mediaItem); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment