Mattias Lövström
Aug 30, 2010
  10066
(1 votes)

Handling images in EPiServer programmatically

The image editor in edit mode uses EPiServer.ImageLibrary to transform images with the great functionality of let another process then the web application to handle it. Senses the handlings of images are heavy both for the CPU and the memory this is a great feature to be able to configure the web site to do this in another process (or another machine). But it is not yet any documentation for it…

I talked to the EPiServer development team that said the API is stable and they agree to publish it – so I hope it will come out in the SDK son, but when we wait for this documentation I can try to describe how it is working.

Assembly:

EPiServer.ImageLibrary

Classes:

EPiServer.ImageLibrary.ImageServiceClient – The image service client to access the service with

Methods:

public ImageServiceClient(

        string endpointConfigurationName)

The constructor of the end point that takes a string defining the endpoints name, the name used by EPiServer are "ImageServiceClientEndPoint" and can be used if you want the same service to handle the web sites images in the same process as the image editor use.

public byte[] RenderImage(

          byte[] imageBuffer,

          IEnumerable<ImageOperation> operations,

          string mimeType,

          float zoomFactor,

          int jpegQuality)

Returns:

A transformed image

Parameters:

imageBuffer – the original image

operations – operations for transforming the image

mimeType – the mime type of the image. Defined mime type are image/png, image/bmp, image/gif, image/tiff, image/jpg, image/jpeg, image/pjpeg

zoomFactor - the zoom factor 1 means 100%

jpegQuality – the Jpeg quality (if mime types is image/jpg, image/jpeg or image/pjpeg) in %

The image operations implemented are given by the enum EPiServer.ImageLibrary.ImageEditorCommand and has the following options:

None - No action

Grayscale - Convert image to grey scale

Crop - Crop image

FlipX - Flip the image on the X axis

FlipY - Flip the image on the Y axis

Resize - Resize the image

Rotate - Rotate the image

Sample code

This web page rotates an image 180 degrees given its virtual path.

Example to flip support Jens up side down (on a EPiServer sample site)

http://localhost/FlipImage.aspx?ImageVirtualPath=/Global/StartPage/cms6_headerEN.jpg

<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="EPiServer.ImageLibrary" %>
<%TransformAndViewImage(HttpContext.Current.Request["ImageVirtualPath"]); %>
 
<script runat="server">
    public void TransformAndViewImage(string imageVirtualPath)
    {
        // Operations used to change the image
        ImageOperation[] operations = new ImageOperation[]{
            new ImageOperation(ImageEditorCommand.Rotate) { Angle = 180 }};
        float zoomFactor = 0.5f; //Percent / 100
        string imageMimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
        int jpegQuality = 100; //Percent
 
        // ImageServiceClientEndPoint is the same endpoint name that the image editor uses 
        ImageServiceClient imageService = new ImageServiceClient("ImageServiceClientEndPoint");
 
        //// Load the image and apply requested changes
        using (System.IO.Stream fileStream = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(imageVirtualPath).Open())
        {
            byte[] orginalImage = new byte[fileStream.Length];
            fileStream.Read(orginalImage, 0, (int)fileStream.Length);
            byte[] imageBuffer = imageService.RenderImage(
                  orginalImage,
                  operations,
                  imageMimeType,
                  zoomFactor,
                  jpegQuality);
 
            Response.ContentType = imageMimeType;
            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            Response.OutputStream.Write(imageBuffer, 0, (int)imageBuffer.Length);
            Response.End();
        }
    }
</script>
Aug 30, 2010

Comments

Joel Abrahamsson
Joel Abrahamsson Sep 21, 2010 10:33 AM

Thanks for posting this Mattias! I didn't know that the ImageLibrary stuff ran in it's own process which certainly is interesting. I might do a follow up post on this one with some interesting usage examples.

Marthin Freij
Marthin Freij Sep 21, 2010 10:33 AM

Nice! Please note that ImageServiceClient will use ImageService if the endpointName is not found in configuration, which means that default configuration will process images within the same process as the application. Mattias, could you please provide some sample configuration for the endpoints as well?

Marthin Freij
Marthin Freij Sep 21, 2010 10:33 AM

Great! ;-)

Petter Klang
Petter Klang Sep 21, 2010 10:33 AM

Nice!
Will there be support added for Actually flipping Jens 180 degrees? Perhaps Greger or Sebastian can help you on that?

Feb 8, 2011 08:39 AM

Thanks for this great post. Would be lost without it. Going to use this for EPiImage 2.0 :-)

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024