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 committed May 13, 2024
1 parent bac5df4 commit 301da1a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/Listener/SizeMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,37 @@ public function handle(Event $event): void {
return;
}

if (!in_array($node->getMimeType(), Application::IMAGE_MIMES)) {
$mimeType = $node->getMimeType();
if (!in_array($mimeType, Application::IMAGE_MIMES)) {
return;
}

$size = getimagesizefromstring($node->getContent());
$content = $node->getContent();
$size = getimagesizefromstring($content);

if ($size === false) {
return;
}

// Swap the width and height if the image is rotated (-)90 degrees
if (extension_loaded('exif')) {
try {
$rawExifData = exif_read_data('data://' . $mimeType . ';base64,' . base64_encode($content), '', true);

if ($rawExifData && array_key_exists('IFD0', $rawExifData) && array_key_exists('Orientation', $rawExifData['IFD0'])) {
/** @var int $orientation */
$orientation = $rawExifData['IFD0']['Orientation'];

// https://exiftool.org/TagNames/EXIF.html
if ($orientation >= 5) {
$size = [$size[1], $size[0]];
}
}
} catch (\Exception $ex) {
$this->logger->info("Failed to extract metadata for " . $node->getId(), ['exception' => $ex]);
}
}

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

0 comments on commit 301da1a

Please sign in to comment.