Conditions | 11 |
Paths | 225 |
Total Lines | 104 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function process($imageUrl, array $thumb) |
||
23 | { |
||
24 | try { |
||
25 | $res = new \StdClass(); |
||
26 | $res->height = null; |
||
27 | $res->width = null; |
||
28 | $res->ratio = null; |
||
29 | $res->done = false; |
||
30 | |||
31 | $filename = tempnam('/tmp', 'mh-'); |
||
32 | $thumbPath = tempnam('/tmp', 'mh-thumb-'); |
||
33 | |||
34 | $this->download($imageUrl, $filename); |
||
35 | |||
36 | $identifier = uniqid(); |
||
37 | |||
38 | $gooPath = 'original'; |
||
39 | $gooThumbPath = sprintf('thumb_%sx%s', $thumb['width'], $thumb['height']); |
||
40 | |||
41 | $dimentions = @getimagesize($filename); |
||
42 | |||
43 | if (!$dimentions) { |
||
44 | throw new \Exception('Image cannot be read properly'); |
||
45 | } |
||
46 | |||
47 | $res->height = $dimentions[1]; |
||
48 | $res->width = $dimentions[0]; |
||
49 | $res->mime = $dimentions['mime']; |
||
50 | $res->ratio = $res->width/$res->height; |
||
51 | |||
52 | switch ($res->mime) { |
||
53 | case 'image/jpeg': |
||
54 | $extension = 'jpeg'; |
||
55 | break; |
||
56 | |||
57 | case 'image/png': |
||
58 | $extension = 'png'; |
||
59 | break; |
||
60 | |||
61 | case 'image/gif': |
||
62 | $extension = 'gif'; |
||
63 | break; |
||
64 | } |
||
65 | |||
66 | $path = $identifier.'.'.$extension; |
||
|
|||
67 | $thumbPath .= '.'.$extension; |
||
68 | |||
69 | $filter = new WebOptimization(); |
||
70 | $imagine = new \Imagine\Gd\Imagine(); |
||
71 | |||
72 | $image = $filter->apply( |
||
73 | $imagine->open($filename) |
||
74 | ->thumbnail( |
||
75 | new \Imagine\Image\Box($thumb['width'], $thumb['height']), |
||
76 | \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND |
||
77 | ) |
||
78 | ) |
||
79 | ; |
||
80 | |||
81 | switch ($res->mime) { |
||
82 | case 'image/jpeg': |
||
83 | $image->save($thumbPath, ['jpeg_quality' => 70]); |
||
84 | |||
85 | exec('jpegoptim '. $filename); |
||
86 | exec('jpegoptim '. $thumbPath); |
||
87 | |||
88 | break; |
||
89 | |||
90 | case 'image/png': |
||
91 | $image->save($thumbPath, ['png_compression_level' => 9]); |
||
92 | break; |
||
93 | |||
94 | case 'image/gif': |
||
95 | $image->save($thumbPath); |
||
96 | break; |
||
97 | |||
98 | default: |
||
99 | throw new \Exception('Quality drop for '.$res->mime.' not supported'); |
||
100 | } |
||
101 | |||
102 | $r = $this->upload($filename, $gooPath, $path, $res->mime); |
||
103 | $res->originalPath = $r->public_link; |
||
104 | |||
105 | $r = $this->upload($thumbPath, $gooThumbPath, $path, $res->mime); |
||
106 | $res->thumbPath = $r->public_link; |
||
107 | |||
108 | $res->done = true; |
||
109 | |||
110 | } catch (\Imagine\Exception\RuntimeException $e) { |
||
111 | $res->error = $e->getMessage(); |
||
112 | |||
113 | } catch (\GuzzleHttp\Exception\ServerException $e) { |
||
114 | $res->error = $e->getMessage(); |
||
115 | |||
116 | } catch (\Exception $e) { |
||
117 | $res->error = $e->getMessage(); |
||
118 | } |
||
119 | |||
120 | $this->cleanup([ |
||
121 | $filename, |
||
122 | $thumbPath, |
||
123 | ]); |
||
124 | |||
125 | return $res; |
||
126 | } |
||
223 |