-
Notifications
You must be signed in to change notification settings - Fork 163
Images
The admin panel has a whole section devoted to managing images where users can add, update and delete images assets from a central place.
Although we have image resizing APIs in Cofoundry, this is only supported with the use of a plugin. Server side image processing is a tricky area and the plugin you choose might depend on your platform or the way you want to scale.
Currently the only image resizing plugin available is Cofoundry.Plugins.Imaging.ImageSharp, which you can add via NuGet.
The Cofoundry View Helper is the best way to access this:
@using Cofoundry.Web
@model MyContentDisplayModel
@inject ICofoundryHelper<MyContentDisplayModel> Cofoundry
/* From a model */
<img src="@Cofoundry.Routing.ImageAsset(Model.ThumbnailImageAsset)">
/* From an id (see warning below) */
<img src="@await Cofoundry.Routing.ImageAssetAsync(3)">
/* Simple Resizing */
<img src="@Cofoundry.Routing.ImageAsset(Model.ThumbnailImageAsset, 200, 200)">
/* Resizing by passing in a constant of type IImageResizeSettings */
<img src="@Cofoundry.Routing.ImageAsset(Model.ThumbnailImageAsset, MyImageSizes.Thumbnail)">
Warning: Generating a url from just an image asset id involves getting more information about the image from the database. Although caching is used to speed this up, it is recommended that you try to include the full ImageAssetRenderDetails object in your view model to take advantage of batch requests and async methods.
You can request IImageAssetRouteLibrary from the DI container and use this to generate urls. It is the same api used by the Cofoundry View Helper above.
using Cofoundry.Domain;
public class ImageExample
{
private IImageAssetRouteLibrary _imageAssetRouteLibrary;
public ImageExample(IImageAssetRouteLibrary imageAssetRouteLibrary)
{
_imageAssetRouteLibrary = imageAssetRouteLibrary;
}
public string GetExampleUrl(IImageAssetRenderable image)
{
var url = _imageAssetRouteLibrary.ImageAsset(image);
return url;
}
}The simplest way to get image data is by resolving an instance of IImageAssetRepository from the DI container.
using Cofoundry.Domain;
public class ImageExample
{
private IImageAssetRouteLibrary _imageAssetRouteLibrary;
private IImageAssetRepository _imageAssetRepository;
public ImageExample(
IImageAssetRouteLibrary imageAssetRouteLibrary,
IImageAssetRepository imageAssetRepository
)
{
_imageAssetRouteLibrary = imageAssetRouteLibrary;
_imageAssetRepository = imageAssetRepository;
}
public Task<string> GetExampleUrl(int imageId)
{
var image = await _imageAssetRepository.GetImageAssetRenderDetailsByIdAsync(imageId);
var url = _imageAssetRouteLibrary.ImageAsset(image);
return url;
}
}Alternatively you can resolve an instance of CofoundryDbContext from the DI container and use Entity Framework to completely customize your query.