Forum

Thread tagged as: Question

Get aspect ratio of image

Is it possible to get the aspect ratio of an image?

I would like to use it for sizing images with flex box.

Tony Astley

Tony Astley 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can get the width and height, so I think you could write a template filter to calculate the ratio.

I'd need to return a figure such as 1.45. It's for flex box to size the images within their parent to fill the space while having equal heights.

It can be done with js, buts it's a bit flaky.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Tony,

Like Drew suggested, you can access the image's width and height in a template filter so you can do the maths in PHP. To learn more about writing a template filter: https://docs.grabaperch.com/api/template-filters/