With the new Image Resizer Extension point, it is now easier to manage how the image should be resized.
In the example below, we will use a field on the Litium.Media.File to define what to do with the image when the image is requested by the browser.
The logic is in two different classes, one is generating the URL for the image and the second is the actual resizer.
There is a decorator for the MediaLocationService that is the part that generate the URL for the image. The field will be fetched from the Litium.Media.File object and assign a field-value to the args.CustomData that will be serialized into the image URL.
[ServiceDecorator(typeof(MediaLocationService))]
internal class SpecialMediaLocationService : MediaLocationService
{
private readonly MediaLocationService _parent;
private readonly FileService _fileService;
public SpecialMediaLocationService(MediaLocationService parent, FileService fileService)
{
_parent = parent;
_fileService = fileService;
}
public override string GetLocation<T>([NotNull] MediaLocationServiceArgs args)
{
if (args.CustomData == null)
{
var file = _fileService.Get(args.SystemId);
if (file != null)
{
// Add custom data to use when fetching the image,
// all data that is added is included in the URL for the image.
// All data that is needed for the rendering is needed to be added.
// Resolving different parameters in the resizer should be avoided because the URL will be website neutral.
args.CustomData = file.Fields.GetValue<string>("MyImageTypeAttribute");
}
}
return _parent.GetLocation<T>(args);
}
}
Now when there is modification on how the image URL will be generated, it is important to decorate the BlobImageResizer to update and use another resize algorithm if the specified CustomData with the specific value exists in the URL.
[ServiceDecorator(typeof(BlobImageResizer))]
internal class SpecialBlobImageResizer : BlobImageResizer
{
private readonly BlobImageResizer _parent;
public SpecialBlobImageResizer(BlobImageResizer parent)
{
_parent = parent;
}
public override Task ResizeAsync([NotNull] Blob source, [NotNull] Blob destination, [NotNull] FormatPath formatPath)
{
// CustomData contains all the data that was added when the page is rendering.
// This resizer should not use HttpContext to lookup domain name etc because that will not work if CDN is used and the same image url can be used for multiple websites.
if (formatPath.CustomData == "Article")
{
// Do same special processing for the image. Image should be stored in the destination blob.
return _parent.ResizeAsync(source, destination, formatPath);
}
return _parent.ResizeAsync(source, destination, formatPath);
}
}
In Litium, we use ImageMagick library to resize image, and with this one we can also convert eps/pdf/ps files to image file by installing ghost-script.
The application pool identity should have write permission to the MagickNET temp directory. The MagickNET temp directory is "C:\Windows\Temp" by default.
How to add and config Ghost-script
The Ghost-script is only needed when we must convert PS, EPS, PDF file.
The Ghost-script DLLs can be downloaded here:
http://www.ghostscript.com/download/gsdnld.html.
After installing Ghost-script, copy the DLLs and locate them on the local environment. The DLLs included "gsdll32.dll/gsdl64.dll" and "gswin32c.exe/gswin64c.exe" depend on the local platform.
The Ghost-script on Litium required two application settings keys:
- Litium:ImageResize:GhostScriptPath – the place we located the ghost-script DLLs
- Litium:ImageResize:ConvertExtensions – the value for the image extensions that accepted to convert.
- For example:
<appSettings>
<add key="Litium:ImageResize:GhostScriptPath" value="..\dll-magik-path"/>
<add key="Litium:ImageResize:ConvertExtensions" value="eps,ept"/>
</appSettings>