spacecatninja / craft-imager-x

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

Can't create avif from webp #209

Closed martin-coded closed 1 year ago

martin-coded commented 1 year ago

I'm submitting a...

Steps to reproduce

  1. Upload a webp image to an asset field
  2. {% set transformedAvif = craft.imagerx.transformImage(thumbnailImage, 'articleHeaderAvif') %} in template fails

Description

I am trying to create avif files. It works perfectly, but it seems to fail when someone uploaded directly a webp image. I thought imager x, creates a jpeg before passing the file to the avif converter?

Displayed errors, stack trace, relevant logs

Custom encoder failed. Output was:

The executed command was "/usr/bin/cavif --quality 60 --speed 6 --overwrite -o /usr/local/apache2/htdocs/web/assets/images/_generated/thumbnails/906039/my-image-name_8861265d722ae23239d845270658da28.avif /usr/local/apache2/htdocs/storage/runtime/imager/temp/4f95b48b980543ac23f319e2c17eca67.webp"

Stack trace:

#0 /usr/local/apache2/htdocs/vendor/spacecatninja/imager-x/src/variables/ImagerVariable.php(33): spacecatninja\imagerx\services\ImagerService->transformImage(Object(craft\elements\Asset), Array, Array, Array)
#1 /usr/local/apache2/htdocs/vendor/twig/twig/src/Extension/CoreExtension.php(1607): spacecatninja\imagerx\variables\ImagerVariable->transformImage(Object(craft\elements\Asset), 'articleHeaderAv...')
#2 /usr/local/apache2/htdocs/vendor/craftcms/cms/src/helpers/Template.php(146): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'transformImage', Array, 'method', false, false)
#3 /usr/local/apache2/htdocs/storage/runtime/compiled_templates/27/27941fa5683840f147a176e48947ed7c.php(52): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'transformImage', Array, 'method')
#4 /usr/local/apache2/htdocs/vendor/twig/twig/src/Template.php(394): __TwigTemplate_0c577a05b34bc05bc865002d20634798->doDisplay(Array, Array)
#5 /usr/local/apache2/htdocs/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#6 /usr/local/apache2/htdocs/vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#7 /usr/local/apache2/htdocs/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#8 /usr/local/apache2/htdocs/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render(Array)
#9 /usr/local/apache2/htdocs/vendor/craftcms/cms/src/web/View.php(456): Twig\Environment->render('_article', Array)
#10 /usr/local/apache2/htdocs/vendor/craftcms/cms/src/web/View.php(509): craft\web\View->renderTemplate('_article', Array)
#11 /usr/local/apache2/htdocs/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate('_article', Array, 'site')
#12 /usr/local/apache2/htdocs/vendor/yiisoft/yii2/web/Response.php(1098): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#13 /usr/local/apache2/htdocs/vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#14 /usr/local/apache2/htdocs/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#15 /usr/local/apache2/htdocs/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#16 /usr/local/apache2/htdocs/web/index.php(12): yii\base\Application->run()
#17 {main} {"memory":40563216,"exception":"[object] (spacecatninja\\imagerx\\exceptions\\ImagerException(code: 0): Custom encoder failed. Output was:

Additional info

    'imagerSystemPath' => '@webroot/assets/images/_generated/',
    'imagerUrl' => '/assets/images/_generated/',
    'customEncoders' => [
        'webp' => [
            'path' => '/usr/bin/cwebp',
            'options' => [
                'quality' => 80,
                'effort' => 4,
            ],
            'paramsString' => '-q {quality} -m {effort} {src} -o {dest}'
        ],
        'avif' => [
            'path' => '/usr/bin/cavif',
            'options' => [
                'quality' => 60,
                'speed' => 6,
            ],
            'paramsString' => '--quality {quality} --speed {speed}  --overwrite -o {dest} {src}'
        ]
    ]
'articleHeaderAvif' => [
    'displayName' => 'Article Header avif',
    'transforms' => [
        ['width' => 1530],
        ['width' => 765],
        ['width' => 550],
        ['width' => 300],
    ],
    'defaults' => [
        'format' => 'avif'
    ]
],
aelvan commented 1 year ago

Thanks for reporting.

Imager doesn't convert the input format to jpg. What you're referencing means that a file is created in the same format as the inout format, and them that file is passed to the custom encoder.

But, I've never considered the fact that the custom encoder may not be able to read the input format, which seems to be the issue here. I'll have a look and try to come up with a fix for this.

Btw, if your image driver supports webp, you probably don't need to set up a custom encoder for it.

aelvan commented 1 year ago

I've added a fix for custom encoders not being able to read the input format, in this case webp. The temporary file will now be saved only in jpg and png format, with png being chosen for all other formats.

I wasn't able to reproduce the issue reported though, but that could have something to do with what formats cavif was compiled with. I'll close for now, but let me know if this didn't solve your issue, and we can investigate further.

martin-coded commented 1 year ago

I've added a fix for custom encoders not being able to read the input format, in this case webp. The temporary file will now be saved only in jpg and png format, with png being chosen for all other formats.

I wasn't able to reproduce the issue reported though, but that could have something to do with what formats cavif was compiled with. I'll close for now, but let me know if this didn't solve your issue, and we can investigate further.

Thank you very much and sorry for not answering in a timely manner. I solved this before with an ugly quick fix in the twig template.

Today during some work on the search index, I realized your fix is working. When running craft resave/entries --update-search-index, the avif images are getting newly generated with a different hash. I think this is because my source image is different. For the quick fix I just picked an already transformed jpeg.