Total Complexity | 48 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Resizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Resizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Resizer |
||
25 | { |
||
26 | public $sourceFile = ''; |
||
27 | public $endFile = ''; |
||
28 | public $maxWidth = 0; |
||
29 | public $maxHeight = 0; |
||
30 | public $imageMimetype = ''; |
||
31 | public $jpgQuality = 90; |
||
32 | public $mergeType = 0; |
||
33 | public $mergePos = 0; |
||
34 | public $degrees = 0; |
||
35 | public $error = ''; |
||
36 | |||
37 | /** |
||
38 | * resize image if size exceed given width/height |
||
39 | * @return string|bool |
||
40 | */ |
||
41 | public function resizeImage() |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return bool|string |
||
118 | */ |
||
119 | public function resizeAndCrop() |
||
120 | { |
||
121 | // check file extension |
||
122 | switch ($this->imageMimetype) { |
||
123 | case 'image/png': |
||
124 | $original = \imagecreatefrompng($this->sourceFile); |
||
125 | break; |
||
126 | case 'image/jpeg': |
||
127 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
128 | break; |
||
129 | case 'image/gif': |
||
130 | $original = \imagecreatefromgif($this->sourceFile); |
||
131 | break; |
||
132 | default: |
||
133 | return 'Unsupported format'; |
||
134 | } |
||
135 | |||
136 | if (!$original) { |
||
137 | return false; |
||
138 | } |
||
139 | // GET ORIGINAL IMAGE DIMENSIONS |
||
140 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
141 | |||
142 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
143 | $max_width_resize = $this->maxWidth; |
||
144 | $max_height_resize = $this->maxHeight; |
||
145 | if ($original_w > $original_h) { |
||
146 | $max_height_ratio = $this->maxHeight / $original_h; |
||
147 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
148 | } else { |
||
149 | $max_width_ratio = $this->maxWidth / $original_w; |
||
150 | $max_height_resize = (int)\round($original_h * $max_width_ratio); |
||
151 | } |
||
152 | if ($max_width_resize < $this->maxWidth) { |
||
153 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
154 | $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio); |
||
155 | $max_width_resize = $this->maxWidth; |
||
156 | } |
||
157 | |||
158 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
159 | $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize); |
||
160 | if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) { |
||
161 | return false; |
||
162 | } |
||
163 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
164 | $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight); |
||
165 | |||
166 | $max_width_offset = 0; |
||
167 | $max_height_offset = 0; |
||
168 | if ($this->maxWidth < $max_width_resize) { |
||
169 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
170 | } else { |
||
171 | $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2); |
||
172 | } |
||
173 | |||
174 | if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) { |
||
175 | return false; |
||
176 | } |
||
177 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
178 | if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) { |
||
179 | return false; |
||
180 | } |
||
181 | |||
182 | return true; |
||
183 | } |
||
184 | |||
185 | public function mergeImage() |
||
186 | { |
||
187 | $dest = \imagecreatefromjpeg($this->endFile); |
||
188 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
189 | if (4 == $this->mergeType) { |
||
190 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
191 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
192 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
193 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
194 | switch ($this->mergePos) { |
||
195 | case 1: |
||
196 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
197 | break; |
||
198 | case 2: |
||
199 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
200 | break; |
||
201 | case 3: |
||
202 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
203 | break; |
||
204 | case 4: |
||
205 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
206 | break; |
||
207 | } |
||
208 | } |
||
209 | if (6 == $this->mergeType) { |
||
210 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
211 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
212 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
213 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
214 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
215 | |||
216 | switch ($this->mergePos) { |
||
217 | case 1: |
||
218 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
219 | break; |
||
220 | case 2: |
||
221 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
222 | break; |
||
223 | case 3: |
||
224 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
225 | break; |
||
226 | case 4: |
||
227 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
228 | break; |
||
229 | case 5: |
||
230 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
231 | break; |
||
232 | case 6: |
||
233 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
234 | break; |
||
235 | } |
||
236 | } |
||
237 | \imagejpeg($dest, $this->endFile); |
||
238 | |||
239 | \imagedestroy($src); |
||
240 | \imagedestroy($dest); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return bool|string |
||
245 | */ |
||
246 | public function rotateImage() |
||
293 | } |
||
294 | } |
||
295 |