If you’ve done any Drupal site building, I’m sure you’ve experienced the following issue. You create an image style with the “Scale and crop” effect and everything is going great until an editor uploads an image with a different aspect ratio.
Now instead of images getting cropped correctly, they’re getting cut in half, or the top part is chopped off, and images are not displaying nicely.
You could fix this problem by tweaking the image style to handle different aspect ratios, but it’ll never be the perfect solution.
The best option is to crop images directly in Drupal, and this is what you’ll learn today.
In this tutorial, you’ll learn how to avoid these situations by using Crop API.
Now, Crop API doesn’t offer any interface on its own; it’s just an API. The two modules that provide an interface are Image Widget Crop and Focal Point. We’ll take a look at these modules in detail, in this tutorial.
Image Widget Crop gives an editor the most flexibility by allowing them to crop images directly within Drupal.
The only downside is they’ll need to manually crop an image every time they upload an image which is okay if you only have a single crop. But it can be cumbersome if you have multiple crops.
If you want full control over how the image is resized then look at using this module.
Focal Point lets an editor select a point on an image and Drupal will crop around it. It’s more automated but doesn’t give you absolute control like Image Widget Crop.
If all you want to do is make sure a particular region of the image is cropped then look at using this module.
If you’re using some reverse proxy or CDN such as Varnish or CloudFront, you will have issues with images not changing after being cropped. I can’t offer a solution in this tutorial because every case is different so this won’t be covered.
If you know of a good workaround to handle invalidating images, then leave a comment.
Before we begin, go download Crop API, Image Widget Crop and Focal point. For the first part, we’ll only install “Image Widget Crop.”
$ drush dl crop image_widget_crop focal_point
Crop Images using Image Widget Crop
We’ll start things off my using Image Widget Crop to allow content editors to crop the image on the Article content type.
First, make sure you installed the “ImageWidgetCrop” module.
Create Crop Type
1. Go to Configuration, Crop types and click on “Add crop type”.
2. Enter in Large into Name and “Used to crop the Large image style.” into Description.
3. In Aspect Ratio add 1:1.
This will make it easier to select a crop because the aspect ratio will stay the same.
4. Once completed click on “Save crop type”.
What’s a Soft Limit and Hard Limit?
When you set a soft limit, the select region will change color indicating the soft limit has been reached. But the user can still resize the crop area smaller.
Hard limit, on the other hand, will stop a user from selecting a crop smaller than what’s been defined.
Configure Image Style
Now that we’re created a crop type. The next bit of work we need to do is configure an image style.
In this example, we’ll use the “Large (480×480)” image style which comes with the Standard installation profile.
1. Go to Configuration, “Image styles” and click on Edit on the image style named “Large (480×480)”.
2. Select “Manual crop” from the “Select a new effect” drop-down and click on Add.
3. Select Large from the “Crop type” and click on “Add effect”.
4. Re-order the effect, so it’s above “Scale 480×480” and click on “Update style”.
Let’s recap what we’ve done.
We added a manual crop effect which use the Large crop type we defined earlier. The image style will first process what’s been cropped manually, and then it’ll scale the image to 480×480.
The last effect, “Scale 480×480” is still necessary because the cropped size can be anything greater than 480×480.
Configure the Image Widget Crop
So far we’ve created a crop type and tweaked the Large image style to use the crop. Now let’s set up Image Widget Crop on the image field.
1. Go to Structure, “Content types” and click on “Manage form display” on the Article row.
2. From the Widget drop-down on the Image row, select “ImageWidget crop”.
If you can’t see the option make sure you’ve installed Image Widget Crop module.
3. Click on the cog-wheel and select Large from within the “Crop Type” drop-down.
If you can’t see your crop type make sure you’ve added it to an image style. You can’t just create a crop type; it must be added to an image style for it to appear here.
4. Click on Update to close the “Widget settings”, then scroll to the bottom and click on Save.
Test Image Cropping
Create a test article or modify an existing one. Upload an image, and you should see a collapsed fieldset called “Crop image”, click on it.
Select your crop and click on Save.
Now if you go to the article page the Large image style should display the cropped region.
Automatic Crop using Focal Point
In the last section, you learnt how to manually crop images. The problem, however, is that cropping an image is a manual process. An editor will have to crop each image they upload. It’s okay if you only have one, but on large projects, you could have a couple of crop types. Manually cropping images could be tedious.
Focal Point takes the manual work away and automatically crops around a defined focal point.
Select a focal point once, when you upload an image, and Drupal will handle the rest.
If you’re following along, go ahead and install Focal Point. We’ll configure Focal Point on the Large and Medium image style.
Set Focal Point
Go to any image field widget, and you should see a crosshair on the image. You’re not required to configure a custom image widget. Focal Point takes over the standard image widget.
The only bit of configuring we need to do is modify the Large and Medium image style.
1. Go to Configuration, “Image styles”
2. Click Edit on the Large row.
3. Select “Focal Point Scale and Crop” from the “Select a new effect” drop-down and click on Add.
4. Enter 480 into widget and height.
5. Delete the “Scale 480×480” effect.
6. Click on “Update style”.
Do the same thing for the medium image style. But instead of adding 480 to the width and height, add 220.
Focal Point will only work if you’re using the standard Image widget. You can’t use Focal Point and Image Widget Crop at the same time.
Preview Focal Point
Once the image styles have been configured, you can preview what the crop will look like.
Just go back to an image field and click on the Preview link.
Set Focal Point
To select a focal point just move the crosshair to where you want the focal point and save the form. Drupal will handle the rest.
Drupal has always been great at manipulating images but Crop API gives power back to the content editors and offers a flexible solution. No longer are you required to hardcode widths and heights into image styles.
Let the editor choose how they want their images cropped.
Q: Can I use Image Widget Crop and Focal Point together?
Q: I can’t select a crop from the “Crop type” drop-down even though I created a crop type.
You need to add a crop to an image style for it to appear in the drop-down.
Q: I re-cropped an image, but it hasn’t updated.
Try performing a hard refresh, ctrl+f5 on Windows or cmd + shift + r on Mac.
12 thoughts on “How to Crop Images using Crop API, Image Widget Crop and Focal Point in Drupal 8”
Since crop api 8.x-1.1 image derivates should be flushed and forced to reload even with Varnish, as there will be a custom crop token attached. See: https://www.drupal.org/project/crop/releases/8.x-1.1
Thanks for the tip.
We are using EPSA Crop (epsacrop) and I personally find it easier to set up and easier for clients to get a hang of.
Yes “EPSA Crop” is great for Drupal 7. However, there’s no Drupal 8 version. 🙁
How can we delete crop types ?
After created some of content it display the error that may you have to change the content first. ?
How can we remove crop types which is created from image widget crop ?
Good question, never thought of that. 🙂
Once you’ve cropped an image, I don’t think it can be un-cropped.
I can’t find any information in the Image widget crop issue queue.
You can use https://www.drupal.org/project/file_version
This module add simple token in files URLs based on file modified time. This is very useful for caching files in CDNs and differentiate if the image changes like a new resource.
Thanks for the tip. I’ll check out the module.
Can I use it in a form?
These modules extend the image field widget. So you need to use the image field to use these modules. You can’t add it to a custom form.
How I can specify a focal point on image added in ckeditor ?
The last time I checked, I don’t think it’s possible to use focal point on images uploaded via the ckeditor plugin.