The Nourishing Fruits of Boredom
So last night I got a little bored watching the old haunted fishtank and my mind, as it often does, drifted to code.
I started thinking about a talk that Mads Kristensen gave at CodeGarden this year that featured a tool he had written with Sayed I. Hashimi called Azure Image Optimizer which wraps various tools like Jpegtran and Gifsicle in an Azure Job to optimize your file based images. He writes about it here and the source can be found here.
I thought it was really nifty and since they've kindly released it under the wonderfully permissive Apache 2.0 license I decided to see if I could grab some of the goodies from the code and add something to ImageProcessor.Web.
Three hours later ImageProcessor.Web.PostProcessor was born...
How It Works
ImageProcessor.Web fires an event once an image has been processed and saved to the cache that can be wired up to asynchronously to further manipulate that file. Tapping into it is fairly simple.
ImageProcessingModule.OnPostProcessing += YOUR_POST_PROCESSING_METHOD_ASYNC;
private static async void YOUR_POST_PROCESSING_METHOD_ASYNC(object sender, PostProcessingEventArgs e)
// Do your thang...
PostProcessingEventArgs contains a single property
In ImageProcessor.Web.PostProcessor I have embedded copies of the various image compressing tools: Gifsicle, Jpegtran, Optipng, and Pngout which are extracted into the running web applications folder and are used to further compress the cached images whenever the event is fired.
If a saving can be found then the cached file is replaced with the newly optimized image and voila your fancy-dancy website is now even leaner and you can celebrate by having a beverage of your choice.
Simply install the package from Nuget and it will instantly kick in; there's no configuration required so go and get it now.
As ever the source code can be found here.