Fix uploaded image orientation
This commit is contained in:
+27
-1
@@ -2014,7 +2014,7 @@ final class App
|
||||
$fileName = $date . '-' . substr($hash, 0, 16) . '.' . $targetExtension;
|
||||
$target = $directory . '/' . $fileName;
|
||||
|
||||
if (is_file($target)) {
|
||||
if (is_file($target) && $targetExtension !== 'webp') {
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
@@ -2052,6 +2052,8 @@ final class App
|
||||
return false;
|
||||
}
|
||||
|
||||
$source = $this->applyImageOrientation($source, $sourcePath, $mime);
|
||||
|
||||
$width = imagesx($source);
|
||||
$height = imagesy($source);
|
||||
if ($width <= 0 || $height <= 0) {
|
||||
@@ -2080,6 +2082,30 @@ final class App
|
||||
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
|
||||
{
|
||||
$fileName = basename(trim($fileName));
|
||||
|
||||
Reference in New Issue
Block a user