Skip to content

Commit

Permalink
Fix image extension with none exist properties (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Jul 20, 2022
1 parent 327abe6 commit bda5ea3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"symfony/property-access": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"conflict": {
"symfony/intl": ">=7.0",
"symfony/property-access": ">=7.0"
},
"suggest": {
"symfony/property-access": "The ImageExtension requires the symfony/property-access service.",
"symfony/intl": "The IntlExtension requires the symfony/intl service."
Expand Down
8 changes: 7 additions & 1 deletion src/ImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sulu\Twig\Extensions;

use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -490,7 +491,12 @@ private function guessAspectRatio($media, array $attributes): array
return [$width, $height];
}

$properties = $this->getPropertyAccessor()->getValue($media, 'properties');
try {
$properties = $this->getPropertyAccessor()->getValue($media, 'properties');
} catch (AccessException $e) {
$properties = [];
}

$originalWidth = $properties['width'] ?? null;
$originalHeight = $properties['height'] ?? null;

Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/ImageExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,28 @@ public function testAspectRatio(string $format, string $expectedWidth, string $e
);
}

public function testAspectRatioNoProperties(): void
{
$imageExtension = new ImageExtension(null, [], [], true);

$image = array_replace_recursive(
$this->image,
[
'thumbnails' => [
'200x100-inset' => '/uploads/media/200x100-inset/01/image.jpg?v=1-0',
'200x100-inset' . '.webp' => '/uploads/media/200x100-inset/01/image.webp?v=1-0',
],
]
);

unset($image['properties']);

$this->assertSame(
'<img alt="Title" title="Description" src="/uploads/media/200x100-inset/01/image.jpg?v=1-0">',
$imageExtension->getImage($image, '200x100-inset')
);
}

/**
* @dataProvider aspectRatioDataProvider
*/
Expand Down

0 comments on commit bda5ea3

Please sign in to comment.