craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[3.x]: Issue with dynamic image transforms #11587

Closed mikesnoeren closed 2 years ago

mikesnoeren commented 2 years ago

What happened?

Description

Not 100% sure if it is a bug, but I couldn't manage to generate an 1x variant of an image transform (from a svg) for an image that had a user defined column size (and thus transform size). I could fix the issue by changing the twig template to always generate images on a specific width. Here's the original SVG I am trying to transform: logo-ns

{% apply spaceless %}
    {% set matrix_width = 860 %}
    {% set size = block.size.value ?? 12 %} {# block.size.value = 4 in this example. #}
{% endapply %}

{% for image in block.image.all() %}
    {% do image.setTransform({
        width: matrix_width / 12 * size,
    }) %}

    <div class="matrix_image">
        {{ tag('img', {
            'src': craft.svgplaceholder.generate({ width: image.getWidth(), height: image.getHeight() }),
            'width': image.getWidth(),
            'height': image.getHeight(),
            'data-srcset': image.getSrcset(['1x','2x']),
            'alt': image.title,
            'class': 'lazyload',
        }) }}
    </div>
{% endfor %}
Error log generated by this code:
2022-07-12 14:24:51 [-][3][130a3bed380d7ba0e7e26f8d8dcc7792][error][craft\errors\AssetTransformException] craft\errors\AssetTransformException: Ongeldige transformatie-ingang: 286.66666666667xAUTO_crop_center-center_none in /var/www/html/vendor/craftcms/cms/src/services/AssetTransforms.php:873
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/services/AssetTransforms.php(761): craft\services\AssetTransforms->normalizeTransform('286.66666666667...')
#1 /var/www/html/vendor/craftcms/cms/src/services/AssetTransforms.php(714): craft\services\AssetTransforms->_generateTransform(Object(craft\models\AssetTransformIndex))
#2 /var/www/html/vendor/craftcms/cms/src/controllers/AssetsController.php(1150): craft\services\AssetTransforms->ensureTransformUrlByIndexModel(Object(craft\models\AssetTransformIndex))
#3 [internal function]: craft\controllers\AssetsController->actionGenerateTransform(3)
#4 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#5 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#6 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('generate-transf...', Array)
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('assets/generate...', Array)
#8 /var/www/html/vendor/craftcms/cms/src/web/Application.php(602): craft\web\Application->runAction('assets/generate...', Array)
#9 /var/www/html/vendor/craftcms/cms/src/web/Application.php(272): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#10 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#11 /var/www/html/public_html/index.php(26): yii\base\Application->run()
#12 {main}
2022-07-12 14:24:51 [-][3][130a3bed380d7ba0e7e26f8d8dcc7792][error][craft\errors\AssetTransformException] craft\errors\AssetTransformException: Kan geen transformatie genereren met de id 3. in /var/www/html/vendor/craftcms/cms/src/services/AssetTransforms.php:732
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/controllers/AssetsController.php(1150): craft\services\AssetTransforms->ensureTransformUrlByIndexModel(Object(craft\models\AssetTransformIndex))
#1 [internal function]: craft\controllers\AssetsController->actionGenerateTransform(3)
#2 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#4 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('generate-transf...', Array)
#5 /var/www/html/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('assets/generate...', Array)
#6 /var/www/html/vendor/craftcms/cms/src/web/Application.php(602): craft\web\Application->runAction('assets/generate...', Array)
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(272): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#8 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#9 /var/www/html/public_html/index.php(26): yii\base\Application->run()
#10 {main}
2022-07-12 14:24:51 [-][3][130a3bed380d7ba0e7e26f8d8dcc7792][error][yii\web\HttpException:500] yii\web\ServerErrorHttpException: Image transform cannot be created. in /var/www/html/vendor/craftcms/cms/src/controllers/AssetsController.php:1153
Stack trace:
#0 [internal function]: craft\controllers\AssetsController->actionGenerateTransform(3)
#1 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#3 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('generate-transf...', Array)
#4 /var/www/html/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction('assets/generate...', Array)
#5 /var/www/html/vendor/craftcms/cms/src/web/Application.php(602): craft\web\Application->runAction('assets/generate...', Array)
#6 /var/www/html/vendor/craftcms/cms/src/web/Application.php(272): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#7 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#8 /var/www/html/public_html/index.php(26): yii\base\Application->run()
#9 {main}

The x2 variant of the transform is generated without any issue; the file looks great when I open it in Finder, the permissions are the same like other files. But Craft can't seem to create the 1x transform. Perhaps this is because of this? : 286.66666666667xAUTO_crop_center-center_none

Craft CMS version

3.7.47.1

PHP version

7.3.33

Operating system and version

Linux 5.10.104-linuxkit

Database type and version

MySQL 5.7.38

Image driver and version

Imagick 3.6.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

brandonkelly commented 2 years ago

Thanks for reporting that! I was able to reproduce the same error locally, and just released Craft 3.7.48 with a fix.