Get ImageResizer to play along with EPiServer 7.5
I don’t know about you but I use ImageResizer a lot (http://imageresizing.net/). One of the major changes in EPiServer 7.5 is the way media/images are handled and ImageResizer no longer worked for images stored in EPiServer.
In order to fix this, might maybe be other solutions, is to create a plugin to ImageResizer. This is really simple.
public class EPiServerBlobPlugin : IVirtualImageProvider, IPlugin
{
public bool FileExists(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
EPiServerBlobImage blobImage = this.GetImage(virtualPath);
return (blobImage != null);
}
public IVirtualFile GetFile(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
return this.GetImage(virtualPath);
}
private EPiServerBlobImage GetImage(string virtualPath)
{
ContentRouteHelper routeHelper = ServiceLocator.Current.GetInstance<ContentRouteHelper>();
MediaData mediaData = routeHelper.Content as MediaData;
if (mediaData == null)
{
return null;
}
return new EPiServerBlobImage(virtualPath, mediaData);
}
public IPlugin Install(global::ImageResizer.Configuration.Config config)
{
config.Plugins.add_plugin(this);
return this;
}
public bool Uninstall(global::ImageResizer.Configuration.Config config)
{
config.Plugins.remove_plugin(this);
return true;
}
}
public class EPiServerBlobImage : IVirtualFile
{
private MediaData _mediaData;
public EPiServerBlobImage(string virtualPath, MediaData mediaData)
{
this.VirtualPath = virtualPath;
this._mediaData = mediaData;
}
public Stream Open()
{
return this._mediaData.BinaryData.OpenRead();
}
public string VirtualPath
{
get;
private set;
}
}
Register the plugin in resizer config section like so:
<resizer>
<plugins>
<add name="ImageResizer.EPiServerBlobPlugin" />
...
</plugins>
</resizer>
Comments