Conditions | 4 |
Paths | 0 |
Total Lines | 77 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
77 | private function circle($image, array $dimensions, array $position) |
||
78 | { |
||
79 | try { |
||
80 | $newDimensions = $this->square($dimensions); |
||
81 | $min = $this->minBetweenDimensions($dimensions); |
||
82 | |||
83 | $croppedImage = imagecrop($image, [ |
||
84 | 'x' => $position['x'], |
||
85 | 'y' => $position['y'], |
||
86 | 'width' => $newDimensions['x'], |
||
87 | 'height' => $newDimensions['y'] |
||
88 | ]); |
||
89 | |||
90 | // Create mask circle |
||
91 | $circleMask = imagecreatetruecolor($min, $min); |
||
92 | |||
93 | if (!is_resource($croppedImage) || !is_resource($circleMask)) { |
||
94 | throw new \Exception('Could not apply circular shape.'); |
||
95 | } |
||
96 | |||
97 | imagealphablending($circleMask, false); |
||
98 | |||
99 | // Create colors |
||
100 | $magentaColor = imagecolorallocatealpha($circleMask, 255, 0, 255, 0); |
||
101 | $transparent = imagecolorallocatealpha($circleMask, 255, 255, 255, 127); |
||
102 | |||
103 | // Add color mask |
||
104 | imagefill($circleMask, 0, 0, $magentaColor); |
||
105 | |||
106 | // Draw circle border line circleMask |
||
107 | imagearc( |
||
108 | $circleMask, |
||
109 | $min / 2, |
||
110 | $min / 2, |
||
111 | $min, |
||
112 | $min, |
||
113 | 0, |
||
114 | 360, |
||
115 | $transparent |
||
116 | ); |
||
117 | |||
118 | // Fill circle |
||
119 | imagefilltoborder( |
||
120 | $circleMask, |
||
121 | $min / 2, |
||
122 | $min / 2, |
||
123 | $transparent, |
||
124 | $transparent |
||
125 | ); |
||
126 | // Mask circle final |
||
127 | |||
128 | // Add alpha channel to image |
||
129 | imagealphablending($croppedImage, true); |
||
130 | |||
131 | // Add mask to image |
||
132 | imagecopyresampled( |
||
133 | $croppedImage, |
||
134 | $circleMask, |
||
135 | 0, |
||
136 | 0, |
||
137 | 0, |
||
138 | 0, |
||
139 | $min, |
||
140 | $min, |
||
141 | $min, |
||
142 | $min |
||
143 | ); |
||
144 | |||
145 | // remove mask color to image |
||
146 | imagecolortransparent($croppedImage, $magentaColor); |
||
147 | |||
148 | imagedestroy($image); |
||
149 | } catch (\Exception $e) { |
||
150 | $croppedImage = $image; |
||
151 | ResponseHandler::fail($e->getMessage()); |
||
152 | } finally { |
||
153 | return $croppedImage; |
||
154 | } |
||
177 |