When you are creating a marketing site with a lot of visuals on it very likely that you will need to process images that you need to display. Fortunately, Sitecore provides you with some basic operations that you would need like resize or grayscale image. Moreover, it will think about caching images on the server-side after processing, so that processing of the image for a specific page will happen only once (for more information about images parameters see this link). In my case, there was a specific request to get round images.
How to create round images
To add this functionality to Sitecore we need to intercept the MediaStream pipeline and make our changes. We must add our processor before the image would be resized, as our processor will not be able to get the original dimensions of the image.
When we identify the correct place to add a handler we need to add some code for processing.
First of all, we should skip processing if flag args.Options.Thumbnail is set to true or OutputStream is not set previously. Then we need to check some parameters (in my case ‘rnd’) in args.Options.CustomOptions to define whether we should apply our transformations and finally to check if the content of the stream is an image. When all conditions are satisfied we could run our transformation functions and assign the returned stream to args.OutputStream, so that resize or grayscale processors will use our rounded image. Image transformation is quite straightforward: we read image data from the stream and fill ellipse with it. Image background we should fill with default background color or transparent in case of png-files.
Follow me on twitter @true_shoorik. Share if the post was useful for you.