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

How to select just gif from pages media

Started by Roberto Vázquez González 9 years ago · 6 replies · 1764 views
9 years ago

Hi,
I'm iterating through pages in a collection and I want to select from each post, one image and one gif we can find on each folder.

TWIG
     {% for p in page.collection %}
    {% set first_image = p.media.images|first %}
    {% set first_gif = p.media['avatar.gif'] %}

    <h2>{{ p.title }}</h2>
    <h3>{{ p.header.headline }}</h3>

    {{ first_image }}
    {{ first_gif }}
    {{ p.summary }}
    {% endfor %}

It works with the previous example, but I want to somehow be able to select just one gif image, doesn't matter what's the name of it, like I do with images.

I tried with p.media['*.gif'], p.media['*.gif']|first and p.media.animatedImage|first, but nothing, the only way I found yet to select the gifs per page, is by calling it with the exact name of the file, but this is very restrictive for me.

Could you somehow explain a way to have an array with all the "Animated Image" on each page as we do with the images?

Thanks in advance.

9 years ago

Hello there,

give it try with... (replace page with your p variable)

TWIG
{# get all the images from the page media #}
{% set images = page.media.images %}

{# create an empty array to hold your bg images #}
{% set bg_images = [] %}

{# loop over all the images and put only 'gif' images into the new array #}
{% for filename, image in images %}
  {% if filename ends with 'gif' %]
  {% set bg_images = bg_images|merge(image) %}
  {% endif %}
{% endfor %}

The example comes from this post. There is some more explanation. ;)
/forum/archive/get-the-image-with-name-starting-with-t7926

Regards,
Michael

8 years ago

Hello mgoll, would you have advice on targeting (for eg.) the second image in the pages media? I see there is "first" and "last", but I can't find any examples on knabbing other images in the sequence!

8 years ago

You should be able to simply reference by its index in this case, e.g. bg_images[1].

8 years ago

Please be advised that gifs are not 'images' but instead 'animated images'. See Supported Media Files and class AbstractMedia which is returned by page.media in Twig.

This means that the array returned by page.media.images does not contain gif files.

One could get access to all media types included the gifs by using:

  • page.media['my-animated.gif']
  • page.media.files, or page.media.all and filter the resulting array on gif extension

The rationale of gifs being excluded from page.media.images is that the image manipulation functions do not work properly on gifs. See this issue at Github.

Just for completeness, a similar discussion on gifs can be found in thread Animated GIFs not showing at Quark blog list page

Hope this clarifies the issue of gifs a bit...

last edited 11/21/18 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
General · by jean-louis67, 3 days ago
2 81 2 days ago
General · by Andy Miller, 7 days ago
0 146 7 days ago
General · by Veehem, 1 week ago
0 145 1 week ago
General · by milkboy, 1 week ago
0 138 1 week ago
General · by ian russell, 2 weeks ago
2 226 2 weeks ago