Skip to content

Commit bda5ea3

Browse files
Fix image extension with none exist properties (#45)
1 parent 327abe6 commit bda5ea3

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"symfony/property-access": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
2424
"thecodingmachine/phpstan-strict-rules": "^0.12"
2525
},
26+
"conflict": {
27+
"symfony/intl": ">=7.0",
28+
"symfony/property-access": ">=7.0"
29+
},
2630
"suggest": {
2731
"symfony/property-access": "The ImageExtension requires the symfony/property-access service.",
2832
"symfony/intl": "The IntlExtension requires the symfony/intl service."

src/ImageExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Sulu\Twig\Extensions;
1515

16+
use Symfony\Component\PropertyAccess\Exception\AccessException;
1617
use Symfony\Component\PropertyAccess\PropertyAccess;
1718
use Symfony\Component\PropertyAccess\PropertyAccessor;
1819
use Twig\Extension\AbstractExtension;
@@ -490,7 +491,12 @@ private function guessAspectRatio($media, array $attributes): array
490491
return [$width, $height];
491492
}
492493

493-
$properties = $this->getPropertyAccessor()->getValue($media, 'properties');
494+
try {
495+
$properties = $this->getPropertyAccessor()->getValue($media, 'properties');
496+
} catch (AccessException $e) {
497+
$properties = [];
498+
}
499+
494500
$originalWidth = $properties['width'] ?? null;
495501
$originalHeight = $properties['height'] ?? null;
496502

tests/Unit/ImageExtensionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,28 @@ public function testAspectRatio(string $format, string $expectedWidth, string $e
635635
);
636636
}
637637

638+
public function testAspectRatioNoProperties(): void
639+
{
640+
$imageExtension = new ImageExtension(null, [], [], true);
641+
642+
$image = array_replace_recursive(
643+
$this->image,
644+
[
645+
'thumbnails' => [
646+
'200x100-inset' => '/uploads/media/200x100-inset/01/image.jpg?v=1-0',
647+
'200x100-inset' . '.webp' => '/uploads/media/200x100-inset/01/image.webp?v=1-0',
648+
],
649+
]
650+
);
651+
652+
unset($image['properties']);
653+
654+
$this->assertSame(
655+
'<img alt="Title" title="Description" src="/uploads/media/200x100-inset/01/image.jpg?v=1-0">',
656+
$imageExtension->getImage($image, '200x100-inset')
657+
);
658+
}
659+
638660
/**
639661
* @dataProvider aspectRatioDataProvider
640662
*/

0 commit comments

Comments
 (0)