spacecatninja / craft-imager-x

Image transforms, optimizations and manipulations for your Craft CMS site.
Other
26 stars 16 forks source link

Accessing auto generated images in twig template #249

Closed mark-chief closed 10 months ago

mark-chief commented 10 months ago

Do you still call named transform in the template to access the auto generated image? I feel like the way I am doing it at the moment is still transforming on page load..

E.g. twig template

{% set transformedImages = craft.imagerx.transformImage(image, 'cardThumbail') %}
{% set transformedWebpImages = craft.imagerx.transformImage(image, 'cardThumbailWebp') %}
<picture>
    <source srcset="{{ transformedWebpImages | srcset }}" type="image/webp">
    <img        
    srcset="{{ transformedImages | srcset }}"       
    src="{{ transformedImages[0].url }}" 
    alt="{{ entry.title }}" 
    role="presentation" 
    aria-hidden="true"
    loading="lazy" />           
</picture>

imager-x-generate.php

<?php

return [
    'elements' => [
        [
            'elementType' =>  \craft\elements\Entry::class,
            'criteria' => [
                'section' => 'articles',
            ],
            'fields' => ['articleImage'],
            'transforms' => ['cardThumbail','cardThumbailWebp']
        ],
            [
            'elementType' =>  \craft\elements\Entry::class,
            'criteria' => [
                'section' => 'events',
            ],
            'fields' => ['eventImage'],
            'transforms' => ['cardThumbail','cardThumbailWebp']
        ]
    ],    
];

imager-x-transforms.php

<?php
return [
    'cardThumbail' => [
        'displayName' => 'Card Thumbnail',
        'transforms' => [
            ['width' => 800],
            ['width' => 400],
        ],
        'defaults' => [
            'ratio' => 16/9,
            'jpegQuality' => 100,
            'mode' => 'crop'
        ]       
    ],
    'cardThumbailWebp' => [
        'displayName' => 'Card Thumbnail Webp',
        'transforms' => [
            ['width' => 800],
            ['width' => 400],
        ],
        'defaults' => [
            'ratio' => 16/9,
            'jpegQuality' => 100,
            'mode' => 'crop',
            'format' => 'webp'
        ]       
    ]
];

Thanks for your help

aelvan commented 10 months ago

Hi,

That looks correct.

Try to create a minimal test case to check if auto generating works as it should. When you save an entry with an asset in the specified fields in one of the sections specified, a queue job is created. If the queue job suns successfully, the transforms should be saved in your imagerSystemPath (@web/imager by default).

So check that your queue is running, and see if the transformed images end up in the correct place.

If they do, try to run the transform at the template level and see what happens. If you suspect that the transforms are still run, check if more/duplicate transforms have been created on disk, or if the timestamps for the existing transforms have been updated.

aelvan commented 10 months ago

@mark-chief Did you do any more debugging om this?

mark-chief commented 10 months ago

Hi @aelvan - all sorted thanks. The code was correct as you said I just had to resave the entries. Thanks for this plugin!