Repository navigation
Expand file tree
/
Copy pathCreateImageFiles.php
More file actions
111 lines (90 loc) · 3.74 KB
/
Copy pathCreateImageFiles.php
File metadata and controls
111 lines (90 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Services\EncryptedFiles;
use Illuminate\Http\UploadedFile;
use Illuminate\Image\Image as ProcessedImage;
use Illuminate\Image\ImageException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use RuntimeException;
final readonly class CreateImageFiles
{
private const int MAX_STORED_DIMENSION = 1600;
private const int QUALITY = 60;
private const array ALLOWED_TYPES = [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WEBP];
public function __construct(private EncryptedFiles $encryptedFiles)
{
//
}
/**
* Every image is decoded, scaled down, and re-encoded as WebP: metadata such as GPS location is dropped, and anything smuggled alongside the pixels is not kept.
* All of them are encoded before any is stored, so a bad one leaves nothing behind, and a stored one is erased again if the transaction saving it rolls back.
* Private images, such as the ones sent in remotes, are stored encrypted.
*
* @param list<UploadedFile> $images
* @param array<int, string|null> $descriptions
* @return list<array{path: string, width: int, height: int, description: string|null, position: int}>
*/
public function handle(array $images, array $descriptions = [], string $directory = 'commit-images', bool $encrypted = false): array
{
$optimized = array_map($this->optimize(...), array_slice($images, 0, CreateCommitImages::MAX_IMAGES));
$files = [];
foreach ($optimized as $position => $image) {
if ($encrypted) {
$path = $directory.'/'.Str::uuid7()->toString();
$stored = Storage::disk()->put($path, $this->encryptedFiles->encrypt($image->toBytes()));
if (! $stored) {
throw new RuntimeException('Unable to store image.');
}
} else {
$path = $image->storeAs($directory, Str::uuid7()->toString().'.webp');
if (! is_string($path)) {
throw new RuntimeException('Unable to store image.');
}
}
DB::afterRollBack(fn (): bool => Storage::disk()->delete($path));
$files[] = [
'path' => $path,
'width' => $image->width(),
'height' => $image->height(),
'description' => $descriptions[$position] ?? null,
'position' => $position,
];
}
return $files;
}
/**
* GD is used explicitly since, unlike Imagick, it never writes the source's metadata back out.
*/
private function optimize(UploadedFile $file): ProcessedImage
{
$info = @getimagesize($file->getRealPath());
if ($info === false
|| ! in_array($info[2], self::ALLOWED_TYPES, true)
|| $info[0] > CreateCommitImages::MAX_SOURCE_DIMENSION
|| $info[1] > CreateCommitImages::MAX_SOURCE_DIMENSION) {
throw $this->invalid();
}
$image = Image::fromUpload($file)
->usingGd()
->orient()
->scale(self::MAX_STORED_DIMENSION, self::MAX_STORED_DIMENSION)
->optimize('webp', self::QUALITY);
try {
$image->toBytes();
} catch (ImageException) {
throw $this->invalid();
}
return $image;
}
private function invalid(): ValidationException
{
return ValidationException::withMessages([
'images' => 'Images must be JPEG, PNG, or WebP files of at most '.CreateCommitImages::MAX_SOURCE_DIMENSION.' pixels per side.',
]);
}
}