-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathOptimizeImageJob.php
More file actions
82 lines (68 loc) · 2.49 KB
/
Copy pathOptimizeImageJob.php
File metadata and controls
82 lines (68 loc) · 2.49 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
<?php
namespace Code16\Sharp\Http\Jobs;
use Code16\Sharp\Form\Eloquent\Uploads\Thumbnails\SharpImageManager;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Encoders\JpegEncoder;
use Spatie\ImageOptimizer\OptimizerChain;
use Spatie\ImageOptimizer\Optimizers\Avifenc;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
class OptimizeImageJob
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
public function __construct(
public string $disk,
public string $filePath,
) {}
public function handle(): void
{
if ($this->optimizeWithIntervention()) {
return;
}
// We do not need to check for exception nor file format because
// the package will not throw any errors and just operate silently.
$chain = app(OptimizerChain::class);
if ($pngquant = collect($chain->getOptimizers())->whereInstanceOf(Pngquant::class)->first()) {
if (! collect($pngquant->options)->some(fn ($option) => str_starts_with($option, '--quality'))) {
$pngquant->options[] = '--quality=85';
}
}
if (! collect($chain->getOptimizers())->whereInstanceOf(Avifenc::class)->first()) {
$chain->addOptimizer(new Avifenc([
'-a cq-level=23',
'-j all',
'--min 0',
'--max 63',
'--minalpha 0',
'--maxalpha 63',
'-a end-usage=q',
'-a tune=ssim',
]));
}
$chain->optimize(Storage::disk($this->disk)->path($this->filePath));
}
protected function optimizeWithIntervention(): bool
{
$imageManager = app(SharpImageManager::class);
$localPath = Storage::disk($this->disk)->path($this->filePath);
if (Storage::disk($this->disk)->mimeType($this->filePath) === 'image/jpeg'
&& ($exif = $this->getExifData($localPath))
&& ($exif['Orientation'] ?? 1) !== 1
) {
Storage::disk($this->disk)->put(
$this->filePath,
$imageManager->read($localPath)->orient()->encode(new JpegEncoder(quality: 85, progressive: true, strip: true)),
);
return true;
}
return false;
}
protected function getExifData(string $path): ?array
{
return @exif_read_data($path) ?: null;
}
}