Getting Image Object for Image In Library???

Tourist
48 0 1

Hi all

I am trying to create a reusable promo footer on the collections page. I was hoping to arbitrarily add content blocks etc to my collections page but the template is a section of its' own and you can't have sections inside sections.

So I have now gone down the snippets path to create a reusable footer promotion area but the main issue I'm having is I wanted to access the aspect_ratio of the image object previously uploaded in to a library..

I can't find any way to do this..

when using an image uploaded in to a section you can do stuff like: section.settings.image

 

Because I'd ideally like to be able to pull an image from the library as an image object.. Does someone know if this is possible?

0 Likes
Highlighted
Shopify Partner
21 0 9

Yes, you can get an image from its name, try something like:

{% assign hero=images['myhero.jpg'] %} for the image myhero.jpg in the files.  I use this to get image objects that have been saved in files from names in metafields.

see https://help.shopify.com/themes/liquid/objects.

Now you can use image attributes and filters on this, such as hero.width, hero.alt, hero | img_url.

To get the aspect ratio as a percentage do:

{% assign ratio = hero.height | times: 100.0 | divided_by: hero.width %}

 

3 Likes