1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
12
|
|
|
|
13
|
|
|
use Intervention\Image\ImageManagerStatic as ImageManager; |
14
|
|
|
use Spatie\ImageOptimizer\OptimizerChain; |
15
|
|
|
use Spatie\ImageOptimizer\Optimizers\Gifsicle; |
16
|
|
|
use Spatie\ImageOptimizer\Optimizers\Jpegoptim; |
17
|
|
|
use Spatie\ImageOptimizer\Optimizers\Optipng; |
18
|
|
|
use Spatie\ImageOptimizer\Optimizers\Pngquant; |
19
|
|
|
use Spatie\ImageOptimizer\Optimizers\Svgo; |
20
|
|
|
|
21
|
|
|
class Image |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Image Optimizer Chain. |
25
|
|
|
*/ |
26
|
1 |
|
public static function optimizer(int $quality): OptimizerChain |
27
|
|
|
{ |
28
|
1 |
|
return (new OptimizerChain()) |
29
|
1 |
|
->addOptimizer(new Jpegoptim([ |
30
|
1 |
|
"--max=$quality", |
31
|
1 |
|
'--strip-all', |
32
|
1 |
|
'--all-progressive', |
33
|
|
|
])) |
34
|
1 |
|
->addOptimizer(new Pngquant([ |
35
|
1 |
|
"--quality=$quality", |
36
|
1 |
|
'--force', |
37
|
1 |
|
'--skip-if-larger', |
38
|
|
|
])) |
39
|
1 |
|
->addOptimizer(new Optipng([ |
40
|
1 |
|
'-i0', |
41
|
|
|
'-o2', |
42
|
|
|
'-quiet', |
43
|
|
|
])) |
44
|
1 |
|
->addOptimizer(new Svgo([ |
45
|
1 |
|
'--disable={cleanupIDs,removeViewBox}', |
46
|
|
|
])) |
47
|
1 |
|
->addOptimizer(new Gifsicle([ |
48
|
1 |
|
'-b', |
49
|
|
|
'-O3', |
50
|
|
|
])); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Build the `srcset` attribute for responsive images. |
55
|
|
|
* ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w". |
56
|
|
|
*/ |
57
|
1 |
|
public static function getSrcset(Asset $asset, int $steps, int $wMin, int $wMax): string |
58
|
|
|
{ |
59
|
1 |
|
$srcset = ''; |
60
|
1 |
|
for ($i = 1; $i <= $steps; $i++) { |
61
|
1 |
|
$w = ceil($wMin * $i); |
62
|
1 |
|
if ($w > $asset->getWidth() || $w > $wMax) { |
63
|
1 |
|
break; |
64
|
|
|
} |
65
|
1 |
|
$a = clone $asset; |
66
|
1 |
|
$img = $a->resize(intval($w)); |
67
|
1 |
|
$srcset .= sprintf('%s %sw', $img, $w); |
68
|
1 |
|
if ($i < $steps) { |
69
|
1 |
|
$srcset .= ', '; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
// add reference image |
73
|
1 |
|
if (!empty($srcset)) { |
74
|
1 |
|
$srcset .= sprintf('%s %sw', $asset, $asset->getWidth()); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $srcset; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Converts an asset image to WebP. |
82
|
|
|
*/ |
83
|
|
|
public static function convertTopWebp(Asset $asset, int $quality): Asset |
84
|
|
|
{ |
85
|
|
|
$assetWebp = clone $asset; |
86
|
|
|
$format = 'webp'; |
87
|
|
|
$image = ImageManager::make($assetWebp['content_source']); |
88
|
|
|
$assetWebp['content'] = (string) $image->encode($format, $quality); |
89
|
|
|
$assetWebp['path'] = preg_replace('/\.'.$asset['ext'].'$/m', ".$format", $asset['path']); |
90
|
|
|
$assetWebp['ext'] = $format; |
91
|
|
|
|
92
|
|
|
return $assetWebp; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|