spacecatninja / craft-imager-x

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

Could not open create the stream error when checking isAnimated while having a fallback image #137

Closed WHITE-developer closed 2 years ago

WHITE-developer commented 2 years ago

I'm submitting a...

Description

We use S3 for our image storage. In the frontend we check if the image is an animated gif with craft.imager.isAnimated(img). When the image doesn't exist on S3 anymore but Craft still knows the image, you get an error "Could not open create the stream" cause, yeah, it doesn't exist anymore. We thought maybe an fallbackImage could help us, but it seems it doesn't do a check for it there

Displayed errors, stack trace, relevant logs

craft\errors\AssetException: Could not open create the stream for “Hera-cosine-DEF.png” in /var/www/html/vendor/craftcms/cms/src/base/FlysystemVolume.php:189
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/base/FlysystemVolume.php(200): craft\base\FlysystemVolume->getFileStream('Hera-cosine-DEF...')
#1 /var/www/html/vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php(167): craft\base\FlysystemVolume->saveFileLocally('Hera-cosine-DEF...', '/var/www/html/s...')
#2 /var/www/html/vendor/spacecatninja/imager-x/src/services/ImagerService.php(569): spacecatninja\imagerx\models\LocalSourceImageModel->getLocalCopy()
#3 /var/www/html/vendor/spacecatninja/imager-x/src/variables/ImagerVariable.php(340): spacecatninja\imagerx\services\ImagerService->isAnimated(Object(craft\elements\Asset))
#4 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1566): spacecatninja\imagerx\variables\ImagerVariable->isAnimated(Object(craft\elements\Asset))
#5 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(106): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'isAnimated', Array, 'method', false, false)
#6 /var/www/html/storage/runtime/compiled_templates/e3/e3aa382faf0e664528578fab9c808adc19deb427eea6a49e81c85c837834d529.php(38): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'isAnimated', Array, 'method')
#7 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_5e86f8ee1beb5c00f8951c8284651dab20884a98279d409d277f3f69cc2d9c4e->doDisplay(Array, Array)
#8 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#9 /var/www/html/storage/runtime/compiled_templates/0f/0f344a16d8df5220d87fedb4463a187955284b5a5711444c805c184233f2c631.php(225): Twig\Template->display(Array)
#10 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_242401b077277378c1c22120e2f35af97688740cbbcbd12287c04e9810878ae7->doDisplay(Array, Array)
#11 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#12 /var/www/html/storage/runtime/compiled_templates/35/35fb69368397622788b2171223302c4e0f03a72c86bef84665ba41007db0bba9.php(86): Twig\Template->display(Array)
#13 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_87827fee46a16ed29e9e071b25febf035152380334ca4e6e893182d4e884f220->doDisplay(Array, Array)
#14 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#15 /var/www/html/storage/runtime/compiled_templates/76/76fc8a1d2c96b3e870f6f72ff827465e3c08e0ce989f203b68eebed794d375c6.php(60): Twig\Template->display(Array)
#16 /var/www/html/vendor/twig/twig/src/Template.php(182): __TwigTemplate_976fec38cc8b20c43742dae5bf98da5cfa8135f929e5b6f9a539947276b3cc6e->block_content(Array, Array)
#17 /var/www/html/storage/runtime/compiled_templates/62/62664bf60d2e6e9023e0963efed875ca6e21c3fe4bbc0445820d2c82019d3e29.php(142): Twig\Template->displayBlock('content', Array, Array)
#18 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_53a83386fc5d91f71a483409b4eeb356ce49d27a92ec2c8333c78fc14f471c7f->doDisplay(Array, Array)
#19 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#20 /var/www/html/storage/runtime/compiled_templates/76/76fc8a1d2c96b3e870f6f72ff827465e3c08e0ce989f203b68eebed794d375c6.php(49): Twig\Template->display(Array, Array)
#21 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_976fec38cc8b20c43742dae5bf98da5cfa8135f929e5b6f9a539947276b3cc6e->doDisplay(Array, Array)
#22 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#23 /var/www/html/vendor/twig/twig/src/Template.php(390): Twig\Template->display(Array)
#24 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render(Array, Array)
#25 /var/www/html/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render(Array)
#26 /var/www/html/vendor/craftcms/cms/src/web/View.php(408): Twig\Environment->render('_pages/page', Array)
#27 /var/www/html/vendor/craftcms/cms/src/web/View.php(461): craft\web\View->renderTemplate('_pages/page', Array)
#28 /var/www/html/vendor/craftcms/cms/src/web/Controller.php(201): craft\web\View->renderPageTemplate('_pages/page', Array, 'site')
#29 /var/www/html/vendor/craftcms/cms/src/controllers/TemplatesController.php(102): craft\web\Controller->renderTemplate('_pages/page', Array)
#30 [internal function]: craft\controllers\TemplatesController->actionRender('_pages/page', Array)
#31 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#32 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#33 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('render', Array)
#34 /var/www/html/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('templates/rende...', Array)
#35 /var/www/html/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#36 /var/www/html/vendor/craftcms/cms/src/web/Application.php(278): yii\web\Application->handleRequest(Object(craft\web\Request))
#37 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#38 /var/www/html/web/index.php(21): yii\base\Application->run()
#39 {main}

Next spacecatninja\imagerx\exceptions\ImagerException: Could not open create the stream for “Hera-cosine-DEF.png” in /var/www/html/vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php:169
Stack trace:
#0 /var/www/html/vendor/spacecatninja/imager-x/src/services/ImagerService.php(569): spacecatninja\imagerx\models\LocalSourceImageModel->getLocalCopy()
#1 /var/www/html/vendor/spacecatninja/imager-x/src/variables/ImagerVariable.php(340): spacecatninja\imagerx\services\ImagerService->isAnimated(Object(craft\elements\Asset))
#2 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1566): spacecatninja\imagerx\variables\ImagerVariable->isAnimated(Object(craft\elements\Asset))
#3 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(106): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'isAnimated', Array, 'method', false, false)
#4 /var/www/html/storage/runtime/compiled_templates/e3/e3aa382faf0e664528578fab9c808adc19deb427eea6a49e81c85c837834d529.php(38): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(spacecatninja\imagerx\variables\ImagerVariable), 'isAnimated', Array, 'method')
#5 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_5e86f8ee1beb5c00f8951c8284651dab20884a98279d409d277f3f69cc2d9c4e->doDisplay(Array, Array)
#6 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#7 /var/www/html/storage/runtime/compiled_templates/0f/0f344a16d8df5220d87fedb4463a187955284b5a5711444c805c184233f2c631.php(225): Twig\Template->display(Array)
#8 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_242401b077277378c1c22120e2f35af97688740cbbcbd12287c04e9810878ae7->doDisplay(Array, Array)
#9 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#10 /var/www/html/storage/runtime/compiled_templates/35/35fb69368397622788b2171223302c4e0f03a72c86bef84665ba41007db0bba9.php(86): Twig\Template->display(Array)
#11 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_87827fee46a16ed29e9e071b25febf035152380334ca4e6e893182d4e884f220->doDisplay(Array, Array)
#12 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#13 /var/www/html/storage/runtime/compiled_templates/76/76fc8a1d2c96b3e870f6f72ff827465e3c08e0ce989f203b68eebed794d375c6.php(60): Twig\Template->display(Array)
#14 /var/www/html/vendor/twig/twig/src/Template.php(182): __TwigTemplate_976fec38cc8b20c43742dae5bf98da5cfa8135f929e5b6f9a539947276b3cc6e->block_content(Array, Array)
#15 /var/www/html/storage/runtime/compiled_templates/62/62664bf60d2e6e9023e0963efed875ca6e21c3fe4bbc0445820d2c82019d3e29.php(142): Twig\Template->displayBlock('content', Array, Array)
#16 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_53a83386fc5d91f71a483409b4eeb356ce49d27a92ec2c8333c78fc14f471c7f->doDisplay(Array, Array)
#17 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#18 /var/www/html/storage/runtime/compiled_templates/76/76fc8a1d2c96b3e870f6f72ff827465e3c08e0ce989f203b68eebed794d375c6.php(49): Twig\Template->display(Array, Array)
#19 /var/www/html/vendor/twig/twig/src/Template.php(405): __TwigTemplate_976fec38cc8b20c43742dae5bf98da5cfa8135f929e5b6f9a539947276b3cc6e->doDisplay(Array, Array)
#20 /var/www/html/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#21 /var/www/html/vendor/twig/twig/src/Template.php(390): Twig\Template->display(Array)
#22 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render(Array, Array)
#23 /var/www/html/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render(Array)
#24 /var/www/html/vendor/craftcms/cms/src/web/View.php(408): Twig\Environment->render('_pages/page', Array)
#25 /var/www/html/vendor/craftcms/cms/src/web/View.php(461): craft\web\View->renderTemplate('_pages/page', Array)
#26 /var/www/html/vendor/craftcms/cms/src/web/Controller.php(201): craft\web\View->renderPageTemplate('_pages/page', Array, 'site')
#27 /var/www/html/vendor/craftcms/cms/src/controllers/TemplatesController.php(102): craft\web\Controller->renderTemplate('_pages/page', Array)
#28 [internal function]: craft\controllers\TemplatesController->actionRender('_pages/page', Array)
#29 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#30 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#31 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('render', Array)
#32 /var/www/html/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('templates/rende...', Array)
#33 /var/www/html/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#34 /var/www/html/vendor/craftcms/cms/src/web/Application.php(278): yii\web\Application->handleRequest(Object(craft\web\Request))
#35 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#36 /var/www/html/web/index.php(21): yii\base\Application->run()
#37 {main}

Additional info

aelvan commented 2 years ago

Yes, that's an oversight on my part, that exception should be handled. Not sure if it makes sense to use fallbackImage there, it's probably better to just return false, and let the fallbackImage kick in when trying to do a transform on the asset? Of course, that would mean that if your fallback image is an animated image, it wouldn't detect that, but... I think maybe we can assume that a fallback image should be a static image?

WHITE-developer commented 2 years ago

Now I'm thinking about it a bit more, yes, a static fallback image is perfect. Could get a bit messy otherwise :joy:. So returning false, or just don't trow the error on line 169 and 190, would do the trick.