Fix uploaded image orientation

This commit is contained in:
2026-05-21 21:40:03 +02:00
parent b80eb9f519
commit 811821afa2
+27 -1
View File
@@ -2014,7 +2014,7 @@ final class App
$fileName = $date . '-' . substr($hash, 0, 16) . '.' . $targetExtension; $fileName = $date . '-' . substr($hash, 0, 16) . '.' . $targetExtension;
$target = $directory . '/' . $fileName; $target = $directory . '/' . $fileName;
if (is_file($target)) { if (is_file($target) && $targetExtension !== 'webp') {
return $fileName; return $fileName;
} }
@@ -2052,6 +2052,8 @@ final class App
return false; return false;
} }
$source = $this->applyImageOrientation($source, $sourcePath, $mime);
$width = imagesx($source); $width = imagesx($source);
$height = imagesy($source); $height = imagesy($source);
if ($width <= 0 || $height <= 0) { if ($width <= 0 || $height <= 0) {
@@ -2080,6 +2082,30 @@ final class App
return $written && is_file($target); return $written && is_file($target);
} }
private function applyImageOrientation(GdImage $source, string $sourcePath, string $mime): GdImage
{
if ($mime !== 'image/jpeg' || !function_exists('exif_read_data')) {
return $source;
}
$exif = @exif_read_data($sourcePath);
$orientation = is_array($exif) ? (int) ($exif['Orientation'] ?? 1) : 1;
$oriented = match ($orientation) {
3 => imagerotate($source, 180, 0),
6 => imagerotate($source, -90, 0),
8 => imagerotate($source, 90, 0),
default => $source,
};
if ($oriented instanceof GdImage && $oriented !== $source) {
imagedestroy($source);
return $oriented;
}
return $source;
}
private function deleteDashboardImage(string $username, string $fileName): void private function deleteDashboardImage(string $username, string $fileName): void
{ {
$fileName = basename(trim($fileName)); $fileName = basename(trim($fileName));