Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SizeMetadataProvider): Swap the width and height if the image is rotated #2474

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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 @@ -53,6 +53,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
Loading