The goal of this module is to provide some control over automatic image cropping in SilverStripe.
Problem: SilverStripe crops all images from the centre. If the subject is off-centre, it may be cropped out.
Solution: FocusPoint allows you to tag the subject in an image and ensures it is not lost during cropping.
SilverStripe 3.1
Packagist listing and installation instructions
Download and extract, rename the module folder 'focuspoint', place it in your project root and run a dev/build?flush=1.
Note: Prior to SilverStripe v3.1.9 dev/build?flush=1 did not flush .ss template files. If using SilverStripe 3.1.8 or earlier you'll need to flush the templates on a page URL e.g. yoursite.dev/?flush=1.
Fun fact: you don't generally need to manually flush template files in SilverStripe after changing them - except when they are new or are rendered via <% include %>
.
When you edit an image in the CMS there should be an extra 'Focus Point' field. Click on the subject of the image to set the focus area and save the image.
Use just like $CroppedImage, but use $CroppedFocusedImage instead.
You can specify that images should not be upscaled by passing a third argument: $image->CroppedFocusedImage($w,$h,$upscale=false)
.
You can use this module in combination with jQuery FocusPoint to accomplish 'responsive cropping' on your website. Check out a demo here. To set this up do something like this in your templates:
<% with $SomeImage %>
<div class="focuspoint"
data-focus-x="$FocusX"
data-focus-y="$FocusY"
data-image-w="$Width"
data-image-h="$Height">
<img src="$Link" alt="" />
</div>
<% end_with %>
Try something like this to get a full-screen background image that preserves your focus point.
<body
<% with $BGImage %>
style="background-image: url($Link);
background-position: $PercentageX% $PercentageY%;
background-size: cover;"
<% end_with %>
>
If you are caching page content that includes a CroppedFocusedImage and you edit the image (i.e. by changing the focus point) but not the page, you may want to invalidate the page's cache as the updated CroppedFocusedImage will have a different filename. Gordon Banderson has written a robust extension to help you achieve this.
SilverStripe FocusPoint provides an easy and automated way to get better results when forcing an image to be a different aspect ratio. I have some vague plans to offer more fine-grained control over individual crops in the future, but until then I recommend checking out Will Morgan's SilverStripe CropperField as an alternative.
You can specify that resampled versions of an image should be flushed when its focus point is changed by setting the FocusPointImage.flush_on_change
config value. For example:
# config.yml
FocusPointImage:
flush_on_change: true
If the focus point field shows a non-interactive image and a text field with a comma in it, make sure the module folder is named 'focuspoint' and try visiting yoursite.dev/?flush=1 again.
As a cache-busting mechanism this module includes approximate focus point coordinates in generated filenames. This means that if the focus point is updating correctly in the CMS but you're not seeing images change on your website, it's likely that you're viewing cached HTML output and need to invalidate that to see the updated image.
Other SilverStripe modules can also prevent images being regenerated when the focus point is changed. You can work around this by telling SilverStripe to delete resampled versions of an image when its focus point is changed.
- Override CroppedImage() instead of adding new method (Note: I've tried everything I could think of to do this. It may be impossible)
- ImageMagick support (maybe already works - can anyone confirm?)
- Internationalisation
- Advanced cropping options and interfaces (may be an additional module)
- Auto detect focus point via Imagga API