|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Glide\Manipulators; |
|
4
|
|
|
|
|
5
|
|
|
use Intervention\Image\Image; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @property string $fm |
|
9
|
|
|
* @property string $q |
|
10
|
|
|
*/ |
|
11
|
|
|
class Encode extends BaseManipulator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Perform output image manipulation. |
|
15
|
|
|
* @param Image $image The source image. |
|
16
|
|
|
* @return Image The manipulated image. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function run(Image $image) |
|
19
|
|
|
{ |
|
20
|
|
|
$format = $this->getFormat($image); |
|
21
|
|
|
$quality = $this->getQuality(); |
|
22
|
|
|
|
|
23
|
|
|
if (in_array($format, ['jpg', 'pjpg'], true)) { |
|
24
|
|
|
$image = $image->getDriver() |
|
25
|
|
|
->newImage($image->width(), $image->height(), '#fff') |
|
26
|
|
|
->insert($image, 'top-left', 0, 0); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($format === 'pjpg') { |
|
30
|
|
|
$image->interlace(); |
|
31
|
|
|
$format = 'jpg'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $image->encode($format, $quality); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Resolve format. |
|
39
|
|
|
* @param Image $image The source image. |
|
40
|
|
|
* @return string The resolved format. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getFormat(Image $image) |
|
43
|
|
|
{ |
|
44
|
|
|
$allowed = [ |
|
45
|
|
|
'gif' => 'image/gif', |
|
46
|
|
|
'jpg' => 'image/jpeg', |
|
47
|
|
|
'pjpg' => 'image/jpeg', |
|
48
|
|
|
'png' => 'image/png', |
|
49
|
|
|
'webp' => 'image/webp', |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
if (array_key_exists($this->fm, $allowed)) { |
|
53
|
|
|
return $this->fm; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($format = array_search($image->mime(), $allowed, true)) { |
|
57
|
|
|
return $format; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return 'jpg'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Resolve quality. |
|
65
|
|
|
* @return string The resolved quality. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getQuality() |
|
68
|
|
|
{ |
|
69
|
|
|
$default = 90; |
|
70
|
|
|
|
|
71
|
|
|
if (!is_numeric($this->q)) { |
|
72
|
|
|
return $default; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($this->q < 0 or $this->q > 100) { |
|
76
|
|
|
return $default; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return (int) $this->q; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|