Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Generate cropped image without resampling


Go to End


4 Posts   1613 Views

Avatar
thany2

Community Member, 7 Posts

12 August 2015 at 1:59am

Edited: 12/08/2015 2:00am

How can I generate a cropped version of an image without it getting resampled and without priorly knowing the dimensions, and output the url for that in a template?
I need to crop an image to a certain ratio, while retaining the maximum width or height (whichever fits). So not crop to a certain width or height.

For example, let's say I've got three images uploaded:
First is 1000x750 (4:3), second is 750:1000 (3:4), and last one is 2400x800 (3:1).
Now let's say I need images that all have a ratio of 2:1.
First would become 1000x500, second becomes 750:375, and last one becomes 1600x800.
So you see, the first two crop chunks from the top and bottom, reducing their height, while the last ones crops from the sides, reducing its width. No widths or heights are the same in this case, but the ratios are, which is all that matters.

How can I do this?

Avatar
Pyromanik

Community Member, 419 Posts

12 August 2015 at 10:08pm

Edited: 12/08/2015 10:09pm

Extend image, manipulate with image_backend as you like.
Look into something like focuspoint

Avatar
thany2

Community Member, 7 Posts

12 August 2015 at 11:35pm

Surely I can extend the Image class, but SS will not magically know that I did that and start using my class instead of its own, right?

Avatar
Pyromanik

Community Member, 419 Posts

13 August 2015 at 3:01am

Edited: 13/08/2015 3:02am

It can if you tell Injector such.