Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
52 / 52 |
CompressedImageGenerator | |
100.00% |
1 / 1 |
|
100.00% |
11 / 11 |
25 | |
100.00% |
52 / 52 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
getQuality | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getOutputFileExtension | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setQuality | |
100.00% |
1 / 1 |
4 | |
100.00% |
9 / 9 |
|||
setOutputFileExtension | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
getJpgQuality | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getPngQuality | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getConfigQuality | |
100.00% |
1 / 1 |
4 | |
100.00% |
4 / 4 |
|||
createFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
getGdImage | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
generateCompressedImage | |
100.00% |
1 / 1 |
5 | |
100.00% |
10 / 10 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
4 | |
5 | use Illuminate\Support\Str; |
6 | use Qmp\Laravel\FileGenerator\Config; |
7 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileExtensionException; |
8 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileTypeException; |
9 | |
10 | class CompressedImageGenerator extends AbstractGenerator |
11 | { |
12 | public const DEFAULT_JPG_QUALITY = 60; |
13 | |
14 | public const DEFAULT_PNG_QUALITY = 0; |
15 | |
16 | public const AUTHORIZED_OUTPUT_FILE_EXTENSION = ['jpg', 'jpeg', 'png']; |
17 | |
18 | protected int $quality; |
19 | |
20 | protected string $outputFileExtension; |
21 | |
22 | /** |
23 | * @var array|string[] |
24 | */ |
25 | protected array $rules = [ |
26 | 'type' => 'required|in:compressed image', |
27 | 'content' => 'required|string', |
28 | 'options' => 'present|array' |
29 | ]; |
30 | |
31 | /** |
32 | * @param \Qmp\Laravel\FileGenerator\Config $config |
33 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
34 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileExtensionException |
35 | */ |
36 | public function __construct(Config $config) |
37 | { |
38 | parent::__construct($config); |
39 | |
40 | $this->setOutputFileExtension(); |
41 | $this->setQuality(); |
42 | } |
43 | |
44 | /** |
45 | * @return int |
46 | */ |
47 | public function getQuality(): int |
48 | { |
49 | return $this->quality; |
50 | } |
51 | |
52 | /** |
53 | * @return string |
54 | */ |
55 | public function getOutputFileExtension(): string |
56 | { |
57 | return $this->outputFileExtension; |
58 | } |
59 | |
60 | /** |
61 | * |
62 | */ |
63 | protected function setQuality(): void |
64 | { |
65 | switch ($this->outputFileExtension) { |
66 | case 'jpg': |
67 | case 'jpeg': |
68 | $this->quality = $this->getJpgQuality(); |
69 | break; |
70 | case 'png': |
71 | $this->quality = $this->getPngQuality(); |
72 | break; |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileExtensionException |
78 | */ |
79 | protected function setOutputFileExtension(): void |
80 | { |
81 | $extension = str::lower(pathinfo($this->config->output_path, PATHINFO_EXTENSION)); |
82 | |
83 | if (!in_array($extension, self::AUTHORIZED_OUTPUT_FILE_EXTENSION)) { |
84 | throw new FileExtensionException($extension); |
85 | } |
86 | |
87 | $this->outputFileExtension = $extension; |
88 | } |
89 | |
90 | /** |
91 | * @return int |
92 | */ |
93 | protected function getJpgQuality(): int |
94 | { |
95 | $quality = $this->getConfigQuality(); |
96 | |
97 | if (is_null($quality)) { |
98 | return self::DEFAULT_JPG_QUALITY; |
99 | } |
100 | |
101 | return $quality; |
102 | } |
103 | |
104 | /** |
105 | * @return int |
106 | */ |
107 | protected function getPngQuality(): int |
108 | { |
109 | $quality = $this->getConfigQuality(); |
110 | |
111 | if (is_null($quality)) { |
112 | return self::DEFAULT_PNG_QUALITY; |
113 | } |
114 | |
115 | return intval((100 - $quality) / 10); |
116 | } |
117 | |
118 | /** |
119 | * @return int|null |
120 | */ |
121 | protected function getConfigQuality(): ?int |
122 | { |
123 | $quality = $this->config->getOption('quality'); |
124 | |
125 | if ($quality && $quality >= 0 && $quality <= 100) { |
126 | return (int) $quality; |
127 | } |
128 | |
129 | return null; |
130 | } |
131 | |
132 | /** |
133 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileNotExistException |
134 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileTypeException |
135 | */ |
136 | protected function createFile(): void |
137 | { |
138 | $filename = $this->checkFileExist($this->config->content); |
139 | $gdImage = $this->getGdImage($filename); |
140 | $this->generateCompressedImage($gdImage); |
141 | } |
142 | |
143 | /** |
144 | * @param string $filename |
145 | * @return mixed |
146 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileTypeException |
147 | */ |
148 | protected function getGdImage(string $filename) |
149 | { |
150 | $explode = explode('/', image_type_to_mime_type(exif_imagetype($filename))); |
151 | $type = end($explode); |
152 | |
153 | $method = "imagecreatefrom$type"; |
154 | if (function_exists($method)) { |
155 | return $method($filename); |
156 | } |
157 | |
158 | throw new FileTypeException($type); |
159 | } |
160 | |
161 | /** |
162 | * @param $gdImage |
163 | * @return bool |
164 | */ |
165 | protected function generateCompressedImage($gdImage): bool |
166 | { |
167 | $method = null; |
168 | |
169 | switch ($this->outputFileExtension) { |
170 | case 'jpg': |
171 | case 'jpeg': |
172 | $method = 'imagejpeg'; |
173 | break; |
174 | case 'png': |
175 | $method = 'imagepng'; |
176 | break; |
177 | } |
178 | |
179 | return function_exists($method) ? $method($gdImage, $this->config->output_path, $this->quality) : false; |
180 | } |
181 | } |