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.

General

Resize image from frontmatter / page header

Started by hwk123 8 years ago · 3 replies · 760 views
8 years ago

myfiles.md

YAML
myfiles:
- header:
  text:
  image: this is the image url 
  imagealt:

myfiles.html.twig

TWIG
{% for myfile in page.header.myfiles %}
    {% if myfile.header %}
        <h4>{{ myfile.header }}</h4> 
    {% endif %} 
    {% if myfile.image %}
        <img class="my-image" src="{{ myfile.image }}"> 
    {% endif %} 
    {% if myfile.text %}
        <p>{{ myfile.text }}</p>
    {% endif %}
{% endfor %}

This works in that it's showing all the images, but how can I automatically resize them all?
I've tried several things

TWIG
<img class="my-image" src="{{ myfile.image.cropResize(200,200) }"> 

doesn't work. Nor do any of the page.media things I've tried.

I'd like to put all images in hi-res in one folder (per category), and then have Grav resize (and hopefully cache?) them for every page. (So I don't have to manually resize anything).

last edited 03/16/18 by hwk123
8 years ago

hi @hwk123

With filename in the frontmatter

YAML
myfiles:
    -
        header: header
        text: text
        imagename: banner2.png
        imagealt: imagealt 

and twig

TWIG
{% for myfile in page.header.myfiles %}
    {% if myfile.header %}
        <h4>{{ myfile.header }}</h4>
    {% endif %}
    {% if myfile.image %}
        {% set crop_image_url = page.media[myfile.imagename].cropResize(200, 200).url() %}
        <img class="my-image" src="{{ crop_image_url }}">
    {% endif %}
    {% if myfile.text %}
        <p>{{ myfile.text }}</p>
    {% endif %}
{% endfor %}

Hope that help

8 years ago

Thank you @dimitrilongo !
Images are not yet showing up, but the "some text" in the below example does show up.
I put the images in user/pages/01.pagename/images - so the code below should be correct?

TWIG
{% if myfile.imagename %}
    {% set crop_image_url = page.find('/images').media[myfile.imagename].cropResize(200, 200).url() %}
    <img class="my-image" src="{{ crop_image_url }}">
            <p>some text</p>
{% endif %}

Edit:

It's a modular page actually, I copied all images to several folders, because I'm not really sure where I should put them for page.media to work?

I've got images in :

  • /user/pages/01.pagename/
  • /user/pages/01.pagename/images
  • /user/pages/01.pagename/_modulename/
  • /user/pages/01.pagename/_modulename/images

Still no images are showing up..

When I log in to the Admin Panel, they DO show up as images under Page Media.... (I'm guessing these are the images in this folder - /user/pages/01.pagename/_modulename/ )

last edited 03/19/18 by hwk123
8 years ago

Update: fixed.

I added

TWIG
<p>the image {{ dump(page.media[myfile.imagename].url() ) }}</p> 

and the URL it spit out was the correct one for most images, but "null" for some others.
After some more tweaking I've found the problem:
As soon as I save the PNG images to JPG, dimitrilongo's code solution is working perfectly fine.

last edited 03/19/18 by hwk123

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago