Skip to content

Commit

Permalink
feat: Respect exif tag for orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
famoser committed Nov 13, 2024
1 parent f9cd80c commit 3f17b4e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 46 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-exif": "*",
"ext-gd": "*",
"ext-iconv": "*",
"ext-json": "*",
Expand All @@ -18,6 +19,7 @@
"doctrine/orm": "^2.7",
"nelmio/cors-bundle": "^2.1",
"phpdocumentor/reflection-docblock": "^5.2",
"phpstan/phpdoc-parser": "^1",
"symfony/asset": "^6",
"symfony/console": "^6",
"symfony/dotenv": "^6",
Expand Down
93 changes: 48 additions & 45 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Helper/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public static function fitInBoundingBox(string $imgPath, mixed $boxWidth, mixed
$imageWidth = $imageSizes[0];
$imageHeight = $imageSizes[1];

return self::fitInBoundingBoxRaw($imageWidth, $imageHeight, $boxWidth, $boxHeight, $expand);
}

/**
* gives back the width and height to be used.
*
* @return int[]
*/
public static function fitInBoundingBoxRaw(int $imageWidth, int $imageHeight, mixed $boxWidth, mixed $boxHeight, bool $expand = true): array
{
// get ratios
$widthRatio = (float) $boxWidth / $imageWidth;
$heightRatio = (float) $boxHeight / $imageHeight;
Expand Down
35 changes: 34 additions & 1 deletion src/Service/Image/GdService.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,30 @@ public function drawCrosshair(float $positionX, float $positionY, string $color,
}
}

private function getQuarterRotation(string $sourcePath): int
{
if (function_exists('exif_read_data')) {
$exif = exif_read_data($sourcePath);
if (!empty($exif['Orientation'])) {
return match ($exif['Orientation']) {
3 => 2,
6 => 3,
8 => 1,
};
}
}

return 0;
}

public function resizeImage(string $sourcePath, string $targetPath, int $maxWidth, int $maxHeight): bool
{
list($width, $height) = ImageHelper::fitInBoundingBox($sourcePath, $maxWidth, $maxHeight, false);
$rotation = $this->getQuarterRotation($sourcePath);
$imageSizes = getimagesize($sourcePath);
$imageWidth = $imageSizes[$rotation % 2];
$imageHeight = $imageSizes[($rotation + 1) % 2];

list($width, $height) = ImageHelper::fitInBoundingBoxRaw($imageWidth, $imageHeight, $maxWidth, $maxHeight, false);
$ending = strtolower(pathinfo($sourcePath, PATHINFO_EXTENSION));

// resize & save
Expand All @@ -121,6 +142,10 @@ public function resizeImage(string $sourcePath, string $targetPath, int $maxWidt
return false;
}

if ($rotation > 0) {
$originalImage = imagerotate($originalImage, $rotation * 90, 0);
}

imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $width, $height, imagesx($originalImage), imagesy($originalImage));
imagejpeg($newImage, $targetPath, 80);
} elseif ('png' === $ending) {
Expand All @@ -129,6 +154,10 @@ public function resizeImage(string $sourcePath, string $targetPath, int $maxWidt
return false;
}

if ($rotation > 0) {
$originalImage = imagerotate($originalImage, $rotation * 90, 0);
}

imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $width, $height, imagesx($originalImage), imagesy($originalImage));
imagepng($newImage, $targetPath, 8);
} elseif ('gif' === $ending) {
Expand All @@ -137,6 +166,10 @@ public function resizeImage(string $sourcePath, string $targetPath, int $maxWidt
return false;
}

if ($rotation > 0) {
$originalImage = imagerotate($originalImage, $rotation * 90, 0);
}

imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $width, $height, imagesx($originalImage), imagesy($originalImage));
imagegif($newImage, $targetPath);
} else {
Expand Down

0 comments on commit 3f17b4e

Please sign in to comment.