142 lines
4.1 KiB
PHP
142 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Shared\Infrastructure\Images;
|
|
|
|
use App\Modules\Shared\Application\Contracts\ImageResizer;
|
|
use GdImage;
|
|
|
|
/**
|
|
* Turns an uploaded image into upright, downscaled JPEGs with GD. Saving
|
|
* again as JPEG leaves out all metadata (such as the location) and anything
|
|
* else embedded in the upload. Used for task images and stock item images.
|
|
*/
|
|
class GdImageResizer implements ImageResizer
|
|
{
|
|
/**
|
|
* Larger images would not fit in memory when decoded.
|
|
*/
|
|
private const int MAX_PIXELS = 50_000_000;
|
|
|
|
private const int QUALITY = 82;
|
|
|
|
/**
|
|
* JPEG versions of the image whose longer side is at most each of the
|
|
* given sizes, or null when the file is not a readable image.
|
|
*
|
|
* @param list<int> $maxSizes
|
|
* @return list<array{bytes: string, width: int, height: int}>|null
|
|
*/
|
|
public function jpegVersions(string $path, array $maxSizes): ?array
|
|
{
|
|
$image = $this->read($path);
|
|
|
|
if ($image === null) {
|
|
return null;
|
|
}
|
|
|
|
$orientation = $this->orientation($path);
|
|
|
|
try {
|
|
$versions = [];
|
|
|
|
// Each version is scaled straight from the original and turned
|
|
// upright afterwards, so only one full-size image is in memory.
|
|
foreach ($maxSizes as $maxSize) {
|
|
$scaled = $this->upright($this->scaled($image, $maxSize), $orientation);
|
|
$versions[] = ['bytes' => $this->jpeg($scaled), 'width' => imagesx($scaled), 'height' => imagesy($scaled)];
|
|
imagedestroy($scaled);
|
|
}
|
|
|
|
return $versions;
|
|
} finally {
|
|
imagedestroy($image);
|
|
}
|
|
}
|
|
|
|
private function orientation(string $path): int
|
|
{
|
|
$exif = function_exists('exif_read_data') ? @exif_read_data($path) : false;
|
|
|
|
return is_array($exif) ? (int) ($exif['Orientation'] ?? 1) : 1;
|
|
}
|
|
|
|
private function read(string $path): ?GdImage
|
|
{
|
|
$info = @getimagesize($path);
|
|
|
|
if ($info === false || $info[0] < 1 || $info[1] < 1 || $info[0] * $info[1] > self::MAX_PIXELS) {
|
|
return null;
|
|
}
|
|
|
|
$image = match ($info[2]) {
|
|
IMAGETYPE_JPEG => @imagecreatefromjpeg($path),
|
|
IMAGETYPE_PNG => @imagecreatefrompng($path),
|
|
IMAGETYPE_GIF => @imagecreatefromgif($path),
|
|
IMAGETYPE_WEBP => @imagecreatefromwebp($path),
|
|
default => false,
|
|
};
|
|
|
|
if ($image === false) {
|
|
return null;
|
|
}
|
|
|
|
return $image;
|
|
}
|
|
|
|
/**
|
|
* Phone cameras save photos sideways and tell the right way up in EXIF.
|
|
*/
|
|
private function upright(GdImage $image, int $orientation): GdImage
|
|
{
|
|
$rotated = match ($orientation) {
|
|
3 => imagerotate($image, 180, 0),
|
|
5, 6 => imagerotate($image, -90, 0),
|
|
7, 8 => imagerotate($image, 90, 0),
|
|
default => $image,
|
|
};
|
|
|
|
if ($rotated === false) {
|
|
return $image;
|
|
}
|
|
|
|
if (in_array($orientation, [2, 5, 7], true)) {
|
|
imageflip($rotated, IMG_FLIP_HORIZONTAL);
|
|
} elseif ($orientation === 4) {
|
|
imageflip($rotated, IMG_FLIP_VERTICAL);
|
|
}
|
|
|
|
if ($rotated !== $image) {
|
|
imagedestroy($image);
|
|
}
|
|
|
|
return $rotated;
|
|
}
|
|
|
|
/**
|
|
* A copy whose longer side is at most the given size, on white: JPEG has
|
|
* no transparency.
|
|
*/
|
|
private function scaled(GdImage $image, int $maxSize): GdImage
|
|
{
|
|
$width = imagesx($image);
|
|
$height = imagesy($image);
|
|
$ratio = min(1, $maxSize / max($width, $height));
|
|
$newWidth = max(1, (int) round($width * $ratio));
|
|
$newHeight = max(1, (int) round($height * $ratio));
|
|
|
|
$copy = imagecreatetruecolor($newWidth, $newHeight);
|
|
imagefill($copy, 0, 0, (int) imagecolorallocate($copy, 255, 255, 255));
|
|
imagecopyresampled($copy, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
|
|
return $copy;
|
|
}
|
|
|
|
private function jpeg(GdImage $image): string
|
|
{
|
|
ob_start();
|
|
imagejpeg($image, null, self::QUALITY);
|
|
|
|
return (string) ob_get_clean();
|
|
}
|
|
}
|