Skip to content

Commit

Permalink
fix(SizeMetadataProvider): Swap the width and height if the image is …
Browse files Browse the repository at this point in the history
…rotated

Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin authored and backportbot[bot] committed May 14, 2024
1 parent 4f532ac commit 2f84ccc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function register(IRegistrationContext $context): void {
// Metadata
$context->registerEventListener(MetadataLiveEvent::class, ExifMetadataProvider::class);
$context->registerEventListener(MetadataBackgroundEvent::class, ExifMetadataProvider::class);
// SizeMetadataProvider optionally depends on ExifMetadataProvider, so it has to be registered afterwards
$context->registerEventListener(MetadataLiveEvent::class, SizeMetadataProvider::class);
$context->registerEventListener(MetadataBackgroundEvent::class, SizeMetadataProvider::class);
$context->registerEventListener(MetadataLiveEvent::class, OriginalDateTimeMetadataProvider::class);
Expand Down
16 changes: 16 additions & 0 deletions lib/Listener/SizeMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public function handle(Event $event): void {
return;
}

// The image might have a rotation stored in the EXIF data.
// If that is the case and the rotation is 90/270 degrees the width and height need to be swapped.
// This is necessary because the clients will take the rotation into account when displaying the image.
if ($event->getMetadata()->hasKey('photos-ifd0')) {
$ifd0 = $event->getMetadata()->getArray('photos-ifd0');
if (array_key_exists('Orientation', $ifd0)) {
/** @var int $orientation */
$orientation = $ifd0['Orientation'];

// https://exiftool.org/TagNames/EXIF.html
if ($orientation >= 5) {
$size = [$size[1], $size[0]];
}
}
}

$event->getMetadata()->setArray('photos-size', [
'width' => $size[0],
'height' => $size[1],
Expand Down

0 comments on commit 2f84ccc

Please sign in to comment.