Add images to tasks

Everyone who may comment on a task sends images to it, e.g. photos
taken with a phone. The server stores an upright, at most 2560 px JPEG
without EXIF data (no location) and a 400 px thumbnail on the private
disk, served only to those who see the job. Uploaders change captions
(also the alt text) and remove their own images; admin and management
may remove anyone's (jobs.remove-any-task-image). Removing a task also
removes its image files.

Livewire's temporary upload limit is raised to PHP's 40 MB so that the
page's own 12 MB limit gives a translated message.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-25 07:46:53 +02:00
co-authored by Claude Sonnet 5.5
parent 4fb8ea1c5f
commit ae8172898d
40 changed files with 2250 additions and 8 deletions
@@ -0,0 +1,149 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Files;
use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
use App\Modules\Jobs\Domain\ValueObjects\StoredTaskImage;
use GdImage;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
/**
* Task image files on the private local disk, made with GD: the image is
* turned upright by its EXIF orientation, scaled down and saved again as a
* JPEG, which leaves out all metadata (such as the location) and anything
* else embedded in the upload.
*/
class GdTaskImageFiles implements TaskImageFiles
{
public const string DISK = 'local';
public const int MAX_SIZE = 2560;
public const int THUMBNAIL_SIZE = 400;
/**
* Larger images would not fit in memory when decoded.
*/
private const int MAX_PIXELS = 50_000_000;
private const int QUALITY = 82;
public function store(string $uploadedFilePath, int $taskId): StoredTaskImage
{
$image = $this->upright($this->read($uploadedFilePath), $uploadedFilePath);
try {
$full = $this->scaled($image, self::MAX_SIZE);
$thumbnail = $this->scaled($image, self::THUMBNAIL_SIZE);
$name = 'task-images/'.$taskId.'/'.Str::uuid()->toString();
$stored = new StoredTaskImage($name.'.jpg', $name.'-thumb.jpg', imagesx($full), imagesy($full));
Storage::disk(self::DISK)->put($stored->path, $this->jpeg($full));
Storage::disk(self::DISK)->put($stored->thumbnailPath, $this->jpeg($thumbnail));
return $stored;
} finally {
imagedestroy($image);
}
}
public function delete(array $paths): void
{
if ($paths !== []) {
Storage::disk(self::DISK)->delete($paths);
}
}
/**
* @throws InvalidTaskImage
*/
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) {
throw new InvalidTaskImage;
}
$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) {
throw new InvalidTaskImage;
}
// Transparent parts become white, as JPEG has no transparency.
$flat = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($flat, 0, 0, (int) imagecolorallocate($flat, 255, 255, 255));
imagecopy($flat, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
return $flat;
}
/**
* Phone cameras save photos sideways and tell the right way up in EXIF.
*/
private function upright(GdImage $image, string $path): GdImage
{
$exif = function_exists('exif_read_data') ? @exif_read_data($path) : false;
$orientation = is_array($exif) ? (int) ($exif['Orientation'] ?? 1) : 1;
$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.
*/
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);
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);
imagedestroy($image);
return (string) ob_get_clean();
}
}