Total Complexity | 96 |
Total Lines | 604 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like YeelightImageService 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 YeelightImageService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class YeelightImageService |
||
30 | { |
||
31 | /** |
||
32 | * @param $key |
||
33 | * @param null $default |
||
|
|||
34 | * |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function getConfig($key, $default = null) |
||
38 | { |
||
39 | return config("yeelight-image.$key", $default); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | private function storagePath() |
||
46 | { |
||
47 | return $this->getConfig('storage_path'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return int |
||
52 | */ |
||
53 | private function defaultQuality() |
||
54 | { |
||
55 | return (int) $this->getConfig('default_quality', 75); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return int |
||
60 | */ |
||
61 | private function largeImageQuality() |
||
62 | { |
||
63 | return (int) $this->getConfig('large_image_quality', 65); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param UploadedFile $file |
||
68 | * @param string $additionValidatorRule |
||
69 | * @param bool $isAllowGIF |
||
70 | * @param int $maxSizeAllowed |
||
71 | * @param bool $isStorePNG |
||
72 | * |
||
73 | * @return YeelightImage |
||
74 | */ |
||
75 | public function handleUploadedFile(UploadedFile $file, $additionValidatorRule = '', $isAllowGIF = false, $maxSizeAllowed = 12829, $isStorePNG = false) |
||
76 | { |
||
77 | // check valid |
||
78 | if (!$file->isValid()) { |
||
79 | throw new StoreImageException($file->getErrorMessage()); |
||
80 | } |
||
81 | |||
82 | // validate image |
||
83 | $additionValidatorRule = !empty($additionValidatorRule) ? '|'.$additionValidatorRule : ''; |
||
84 | $mimes = $isAllowGIF ? ',gif' : ''; |
||
85 | /** @var Validator $validator */ |
||
86 | $validator = Validator::make(['image' => $file], [ |
||
87 | 'image' => 'required|mimes:jpg,jpeg,bmp,png'.$mimes.'|max:'.$maxSizeAllowed.$additionValidatorRule, |
||
88 | ]); |
||
89 | if ($validator->fails()) { |
||
90 | throw new StoreImageException($validator->errors()->first()); |
||
91 | } |
||
92 | |||
93 | // file info |
||
94 | $path_with_name = $file->getPathname(); |
||
95 | $file_extension = $file->getClientOriginalExtension(); |
||
96 | |||
97 | return $this->storeImageFromPath($path_with_name, $file_extension, $isAllowGIF, $isStorePNG); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param $path_with_name |
||
102 | * @param null $file_extension |
||
103 | * @param bool $isAllowGIF |
||
104 | * @param bool $isStorePNG |
||
105 | * |
||
106 | * @return null|YeelightImage |
||
107 | */ |
||
108 | public function storeImageFromPath($path_with_name, $file_extension = null, $isAllowGIF = false, $isStorePNG = false) |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param $originImageEncodedData |
||
202 | * @param bool $isAllowGIF |
||
203 | * |
||
204 | * @return null|YeelightImage |
||
205 | */ |
||
206 | public function storeImageFromURLEncodedImageData($originImageEncodedData, $isAllowGIF = false) |
||
207 | { |
||
208 | $exif = read_exif_data_safe($originImageEncodedData); |
||
209 | $is_gif = self::isDataURLEncodedImageGif($originImageEncodedData); |
||
210 | $origin_image_data = self::convertDataURLEncodedToImageData($originImageEncodedData); |
||
211 | $origin_image_sha1 = sha1($origin_image_data); |
||
212 | |||
213 | return $this->storeImageFromMake($origin_image_data, $origin_image_sha1, $exif, $isAllowGIF, $is_gif); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param $source |
||
218 | * @param $file_sha1 |
||
219 | * @param $exif |
||
220 | * @param bool $isAllowGIF |
||
221 | * @param bool $isGIF |
||
222 | * |
||
223 | * @return YeelightImage |
||
224 | */ |
||
225 | public function storeImageFromMake($source, $file_sha1, $exif, $isAllowGIF = false, $isGIF = false) |
||
226 | { |
||
227 | try { |
||
228 | $image = Image::make($source); |
||
229 | } catch (StoreImageException $e) { |
||
230 | throw new StoreImageException('YeelightImageService: Unable to make image [' + (string) $e + ']'); |
||
231 | } |
||
232 | |||
233 | $is_allowed_animated_gif = $isAllowGIF && $isGIF; |
||
234 | |||
235 | // save as jpg |
||
236 | $default_file_extension = 'jpg'; |
||
237 | // save as gif if is allowed |
||
238 | if ($isAllowGIF && $is_allowed_animated_gif) { |
||
239 | $default_file_extension = 'gif'; |
||
240 | } |
||
241 | $storage_path = $this->storagePath(); |
||
242 | $final_file_sha1 = null; |
||
243 | if ($file_sha1 === false || strlen($file_sha1) != 40) { |
||
244 | throw new StoreImageException('Invalid SHA1 for file.'); |
||
245 | } |
||
246 | |||
247 | // final |
||
248 | $final_file_name = strtolower($file_sha1.'.'.$default_file_extension); |
||
249 | $final_path_with_name = $storage_path.$final_file_name; |
||
250 | |||
251 | // check directory |
||
252 | if (!self::autoCreateDirectory($final_path_with_name)) { |
||
253 | throw new StoreImageException('Failed to make dir: '.dirname($final_path_with_name)); |
||
254 | } |
||
255 | |||
256 | // save |
||
257 | $isExists = File::exists($final_path_with_name); |
||
258 | $isSimilarExists = false; |
||
259 | $yeelight_image_hash = null; |
||
260 | $final_file_sha1 = null; |
||
261 | $final_file_size = null; |
||
262 | if ($isExists) { |
||
263 | $final_file_sha1 = $file_sha1; |
||
264 | $final_file_size = filesize($final_path_with_name); |
||
265 | } else { |
||
266 | $yeelight_image_hash = YeelightImageHash::where(['file_sha1' => $file_sha1])->first(); |
||
267 | $isExists = $isSimilarExists = $yeelight_image_hash ? true : false; |
||
268 | } |
||
269 | |||
270 | if (!$isExists) { |
||
271 | if ($isAllowGIF && $is_allowed_animated_gif) { |
||
272 | if (file_put_contents($final_path_with_name, $source)) { |
||
273 | @chmod($final_path_with_name, 0666 & ~umask()); |
||
274 | } else { |
||
275 | throw new StoreImageException('Failed to move file to path: '.$final_path_with_name); |
||
276 | } |
||
277 | $final_file_sha1 = sha1_file($final_path_with_name); |
||
278 | $final_file_size = filesize($final_path_with_name); |
||
279 | $isExists = File::exists($final_path_with_name); |
||
280 | } else { |
||
281 | $isExists = $this->saveImage($image, $exif, |
||
282 | $final_path_with_name, $final_file_sha1, $final_file_size); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | // stored and save to database |
||
287 | $yeelight_image = null; |
||
288 | if ($isExists) { |
||
289 | $yeelight_image = $this->saveImageInfo($isSimilarExists, $yeelight_image_hash, |
||
290 | $final_file_name, $is_allowed_animated_gif, $final_file_size, $image, $exif, |
||
291 | $file_sha1, $final_file_sha1); |
||
292 | } |
||
293 | |||
294 | if (!$yeelight_image) { |
||
295 | throw new StoreImageException('Failed to store image.'); |
||
296 | } |
||
297 | |||
298 | return $yeelight_image; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param $filename |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function isAnimatedGif($filename) |
||
307 | { |
||
308 | $file_contents = file_get_contents($filename); |
||
309 | |||
310 | $str_loc = 0; |
||
311 | $count = 0; |
||
312 | |||
313 | // There is no point in continuing after we find a 2nd frame |
||
314 | while ($count < 2) { |
||
315 | $where1 = strpos($file_contents, "\x00\x21\xF9\x04", $str_loc); |
||
316 | if ($where1 === false) { |
||
317 | break; |
||
318 | } |
||
319 | |||
320 | $str_loc = $where1 + 1; |
||
321 | $where2 = strpos($file_contents, "\x00\x2C", $str_loc); |
||
322 | if ($where2 === false) { |
||
323 | break; |
||
324 | } else { |
||
325 | if ($where1 + 8 == $where2) { |
||
326 | $count++; |
||
327 | } |
||
328 | $str_loc = $where2 + 1; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | // gif is animated when it has two or more frames |
||
333 | return $count >= 2; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param $image_name |
||
338 | * |
||
339 | * @return mixed |
||
340 | */ |
||
341 | public function getImageOrFail($image_name) |
||
342 | { |
||
343 | return YeelightImage::where('image_name', $image_name)->firstOrFail(); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param $template_name The template to be used for showing |
||
348 | * @param $image_name The image name for showing |
||
349 | * @param $image_templates array Additional image templates, if same template exists, will be replaced |
||
350 | * @param null $storage_path |
||
351 | * |
||
352 | * @return mixed|\Symfony\Component\HttpFoundation\BinaryFileResponse |
||
353 | */ |
||
354 | public function showImage($template_name, $image_name, array $image_templates = [], $storage_path = null) |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @param $image |
||
441 | * @param $exif |
||
442 | * @param $final_path_with_name |
||
443 | * @param null $final_file_sha1 |
||
444 | * @param null $final_file_size |
||
445 | * |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function saveImage($image, $exif, $final_path_with_name, |
||
449 | &$final_file_sha1 = null, &$final_file_size = null) |
||
450 | { |
||
451 | // set correct orientation |
||
452 | if (!empty($exif) && is_array($exif)) { |
||
453 | if (isset($exif['Orientation']) && $exif['Orientation'] != 1) { |
||
454 | $orientation = $exif['Orientation']; |
||
455 | $deg = 0; |
||
456 | switch ($orientation) { |
||
457 | case 3: |
||
458 | $deg = 180; |
||
459 | break; |
||
460 | case 6: |
||
461 | $deg = 270; |
||
462 | break; |
||
463 | case 8: |
||
464 | $deg = 90; |
||
465 | break; |
||
466 | } |
||
467 | // rotate image |
||
468 | $image->rotate($deg); |
||
469 | } |
||
470 | } |
||
471 | |||
472 | // basic info |
||
473 | $originWidth = $image->getWidth(); |
||
474 | $originHeight = $image->getHeight(); |
||
475 | $file_origin_size = $image->filesize(); |
||
476 | $image_file_size_kb = $file_origin_size / 1024; |
||
477 | |||
478 | // default quality 75 |
||
479 | $image_quality = $this->defaultQuality(); |
||
480 | |||
481 | // higher quality for small images |
||
482 | if ($image_file_size_kb < 500) { |
||
483 | $image_quality = 100; |
||
484 | } |
||
485 | |||
486 | // higher compression for large size images, 2000kb |
||
487 | if ($image_file_size_kb > 2000) { |
||
488 | // quality 65 |
||
489 | $image_quality = $this->largeImageQuality(); |
||
490 | } |
||
491 | |||
492 | // reduce dimension to max 5000px |
||
493 | $maxDimensionAllow = 5000; |
||
494 | if ($originWidth > $maxDimensionAllow || $originHeight > $maxDimensionAllow) { |
||
495 | $newWidth = $newHeight = null; |
||
496 | if ($originWidth >= $originHeight) { |
||
497 | $newWidth = $maxDimensionAllow; |
||
498 | } else { |
||
499 | $newHeight = $maxDimensionAllow; |
||
500 | } |
||
501 | |||
502 | // resize |
||
503 | $image->resize($newWidth, $newHeight, function ($constraint) { |
||
504 | $constraint->aspectRatio(); |
||
505 | $constraint->upsize(); |
||
506 | }); |
||
507 | } |
||
508 | |||
509 | // save as JPG |
||
510 | $image->save($final_path_with_name, $image_quality); |
||
511 | $final_file_sha1 = sha1_file($final_path_with_name); |
||
512 | $final_file_size = filesize($final_path_with_name); |
||
513 | $isSaved = File::exists($final_path_with_name); |
||
514 | |||
515 | return $isSaved; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param $isSimilarExists |
||
520 | * @param $yeelight_image_hash |
||
521 | * @param $final_file_name |
||
522 | * @param $is_allowed_animated_gif |
||
523 | * @param $final_file_size |
||
524 | * @param $image |
||
525 | * @param $exif |
||
526 | * @param $file_sha1 |
||
527 | * @param $final_file_sha1 |
||
528 | * |
||
529 | * @return YeelightImage |
||
530 | */ |
||
531 | private function saveImageInfo($isSimilarExists, $yeelight_image_hash, $final_file_name, $is_allowed_animated_gif, $final_file_size, $image, $exif, $file_sha1, $final_file_sha1) |
||
568 | } |
||
569 | |||
570 | public static function convertDataURLEncodedToImageData($encodedData) |
||
571 | { |
||
572 | try { |
||
573 | $filteredData = substr($encodedData, strpos($encodedData, ',') + 1); |
||
574 | $image_data = base64_decode($filteredData); |
||
575 | |||
576 | return $image_data; |
||
577 | } catch (StoreImageException $e) { |
||
578 | \Log::error('YeelightImageService: Unable to convert to image [' + (string) $e + ']'); |
||
579 | |||
580 | return; |
||
581 | } |
||
582 | } |
||
583 | |||
584 | public static function isDataURLEncodedImageGif($encodedData) |
||
585 | { |
||
586 | try { |
||
587 | $image_type = substr($encodedData, 0, strpos($encodedData, ',')); |
||
588 | $is_gif = str_contains($image_type, 'gif'); |
||
589 | |||
590 | return $is_gif; |
||
591 | } catch (StoreImageException $e) { |
||
592 | \Log::error('YeelightImageService: Unable to convert to image [' + (string) $e + ']'); |
||
593 | |||
594 | return false; |
||
595 | } |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * auto create directory if not exists. |
||
600 | * |
||
601 | * @param $path_name |
||
602 | * |
||
603 | * @return bool |
||
604 | */ |
||
605 | public static function autoCreateDirectory($path_name) |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @param int $yeelight_image_id |
||
620 | * |
||
621 | * @return \Intervention\Image\Image |
||
622 | */ |
||
623 | public static function getImageFromYeelightImageId($yeelight_image_id) |
||
633 | } |
||
634 | } |
||
635 |