Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

cropResize an image from (0,0)

media

Solved by Hugh Barnes View solution

Started by Hugh Barnes 4 years ago · 2 replies · 615 views
4 years ago

I have some portrait images that I want to resize to 640x640 and crop the bottom. The media crop operation accepts a starting point for the crop but requires exact pixel sizes rather than a dimension.

I have tried cropZoom, which works but cuts around the centre of the image. I have also tried combining cropResize and crop with .cropResize(640, 640).crop(0, 0, 640, 640), but that adds black space on the right, even though cropResize on its own gives the right width (just too high).

I hope I explained all that OK.

Is there a trick I am missing?

4 years ago

@hughbris, Not sure if I understand all of it...

crop the bottom

Do you mean you only want to keep the bottom of the image and cut away the top?

requires exact pixel sizes rather than a dimension.

What do you mean by dimension?

If you want to crop from the bottom to cut away the top, you could try the following:

TWIG
{% set original = page.media|first %}
{{ original.html | raw }}

{% set image = page.media|first %}
{% set height = image.height %}

{{ image.crop(0, height - 300, 300, 300).html | raw }}

yields the following images:
Untitled|635x379

Is that what you are looking for?

last edited 06/01/22 by pamtbaau
4 years ago Solution

I suck at graphics and explaining visual concepts. I wanted to scale the image down and then remove the bottom (the other way to your example).

I did achieve this taking inspiration from your example. In fact, I crop it first and then do a simple resize, something like:

TWIG
{% set image_square = image.crop(0, 0, image.width, image.width).resize(640, 640) %}

Thanks!

Edit: the reason for this is so that I can use the same image in two different templates, rather than having to make a copped/resized version of each.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 10 hours ago
Support · by Anna, 3 days ago
2 60 13 hours ago
Support · by Justin Young, 14 hours ago
1 30 14 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago