Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Allan Thraen
Nov 9, 2011
  5902
(7 votes)

Use a workflow to resize images upon upload

Here’s a little timesaver for you all to enjoy. Usually when you work with the web, it’s important to ensure that you use images in the size. And typically that size is not the same as you get the pictures in. Naturally you can fire up photoshop and resize your images, or better yet, use the image editing tool in EPiServer CMS to do the same. However, that gets a bit tedious when you have a lot of images to resize, like I had recently. In fact, I had so many images that I needed to convert to two different sizes, that after the first 8 I gave up and figured “there’s got to be an easier way”. And voila! Why not use workflows to do the job for us?  Building custom workflows is no where as scary as it may sound – and the WF engine in .NET (with support in EPiServer as well) has a few advantages we can use here, like a build-in UI to set start parameters, attaching to file system events and a workflow engine to ensure async execution of multiple workflows.

 

First,  install from NuGet  (when available) or download the source code here.

When the workflow is installed either configure it in the episerver.config, by putting this in:

<workflowSettings>
  <definitions>
    <definition type="EPiServer.Labs.Workflows.ImageResize.ResizeImage,EPiServer.Labs.Workflows.ImageResize"
      name="Resize uploaded images" description="Will automatically resize images to your specifications" />
  </definitions>
</workflowSettings>

 

Or adding it manually in the Admin UI.

image

 

Once the workflow type has been configured (and saved) you are ready to attach it to an automatic start event – in this case it makes the most sense to attach to “FileCheckedIn” which happens whenever a new version of a file is checked in.

 

image

However, we only want to resize certain image files, so under “Start Parameters” we configure the virtual path they are under, a file mask, and the max height/width we want to generate. The workflow will always ensure the correct aspect ration.

You also define a new file name for the generated image, where {0} and {1} are used as placeholders for the old filename and old file extension.

 

image

When all this is done, you can just start uploading files and see how resized versions magically appear.

 

image

Nov 09, 2011

Comments

Hampus Persson
Hampus Persson Nov 9, 2011 03:50 PM

Brilliant!

Nov 10, 2011 04:17 PM

Great way to use a workflow, will definitely try this out!

Nov 11, 2011 09:40 AM

Great! A standard workflow for the next release?

Jon Marks
Jon Marks Nov 11, 2011 04:52 PM

Wow! That is neat. Can you suggest any use cases for it?

Thomas Sløk Tvedt
Thomas Sløk Tvedt Jul 3, 2013 02:28 PM

Nice :-)
It would be even nicer if you copied the Unifiedfile-summary to the created files. In ResizeImage.cs, line 118, add something like:

var originalPath = File;
var originalUnifiedFile = HostingEnvironment.VirtualPathProvider.GetFile(originalPath) as UnifiedFile;
UnifiedFile.CopySummary(originalUnifiedFile, newfile);

mhmccabe
mhmccabe Feb 11, 2014 05:11 PM

Is this source code compatible with EPiServer 7.5?

Snehal Ramteke
Snehal Ramteke Apr 3, 2017 11:38 AM

Is it compatiple with later versions of Episerver 7.5 and higher.

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog