Image Cropper
Returns: MediaWithCrops
Returns a path to an image, along with information about focal point and available crops.
When the Image Cropper is used on a Media Type the crops are shared between all usages of a Media Item. This is called global crops.
If the Image Cropper is used on a Document Type, the file and crops will be local to the Document.
Notice that it is possible make local crops on shared Media Items via the Media Picker Property Editor.
Settings
Define Crops
You can add, edit & delete crop presets the cropper UI can use.
Data Type Definition Example
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3701346516-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FG1Byxw7XfiZAj8zDMCTD%252Fuploads%252Fgit-blob-786888e25e94795c7a5d8fcf18e4cb8b21c21311%252FimageCropper.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=2b456d2f&sv=1)
Content Example
The Image Cropper provides a UI to upload an image, set a focal point on the image, and use predefined crops.
By default, images in the Image Cropper will be shown based on a set focal point and only use specific crops if they are available.
The Image Cropper comes with 3 modes:
Uploading an image
Setting a focal point
Cropping the image to predefined crops
Uploading images
The editor exposes a drop area for files. Select it to upload an image.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3701346516-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FG1Byxw7XfiZAj8zDMCTD%252Fuploads%252Fgit-blob-94e5f4e305625a2b93b48c8a6ded91298a432b39%252FimageCropper-upload-v8.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=86e32426&sv=1)
Set focal point
By default, the Image Cropper allows the editor to set a focal point on the uploaded image.
All the preset crops are shown to give the editor a preview of what the image will look like on the frontend.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3701346516-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FG1Byxw7XfiZAj8zDMCTD%252Fuploads%252Fgit-blob-8124cc536813c427171c1a3410d47e0bc76b7dfd%252FimageCropper-focalpoint.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=d535c809&sv=1)
Crop and resize
The editor can fit the crop to the image to ensure that the image is presented as intended.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3701346516-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FG1Byxw7XfiZAj8zDMCTD%252Fuploads%252Fgit-blob-de2e956bea59bab145a60a39b17fc5057f238bb3%252FimageCropper-crop.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=4be02619&sv=1)
Powered by ImageSharp.Web
ImageSharp.Web is image processing middleware for ASP.NET.
We bundle this package with Umbraco and you can therefore take full advantage of all its features for resizing and format changing. Learn more about the built in processing commands in the official ImageSharp documentation.
Sample code
The Image Cropper comes with an API to generate crop URLs. You can also access the raw data directly as a dynamic object.
For rendering a cropped media item, the .GetCropUrl
is used:
The third parameter is HtmlEncode
and is by default set to true. This means you only need to define the parameter if you want to disable HTML encoding.
Example to output a "banner" crop from a cropper property with the property alias "customCropper"
Or, alternatively using the MediaWithCrops
extension method:
Example to dynamically create a crop using the focal point - in this case 300 x 400px image
CSS background example to output a "banner" crop
Set the htmlEncode
to false so that the URL is not HTML encoded
Add values programmatically
To update a content property value you need the Content Service.
The following sample demonstrates how to add or change the value of an Image Cropper property programmatically. The sample creates an API controller with an action, which must be invoked via a POST request to the URL written above the action.
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
If you use Models Builder to generate source code (modes SourceCodeAuto
or SourceCodeManual
), you can use nameof([generated property name])
to access the desired property without using a magic string:
Get all the crop urls for a specific image
Crop URLs are not limited to usage within a view. IPublishedContent
has a GetCropUrl
extension method, which can be used to access crop URLs anywhere.
The following sample demonstrates how to use GetCropUrl
to retrieve URLs for all crops defined on a specific image:
Sample on how to change the format of the image
Below the example to output a PNG using ImageSharp.Web format command.
Last updated