Conditions | 30 |
Paths | 1728 |
Total Lines | 160 |
Code Lines | 110 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
97 | public function rotate($angle, $options = null) |
||
98 | { |
||
99 | if (null === $options) { |
||
100 | $autoresize = true; |
||
101 | $color_mask = [255, 255, 0]; |
||
102 | } else { |
||
103 | extract($options); |
||
104 | } |
||
105 | |||
106 | while ($angle <= -45) { |
||
107 | $angle += 360; |
||
108 | } |
||
109 | while ($angle > 270) { |
||
110 | $angle -= 360; |
||
111 | } |
||
112 | |||
113 | $t = deg2rad($angle); |
||
114 | |||
115 | if (!is_array($color_mask)) { |
||
116 | // Not already in numberical format, so we convert it. |
||
117 | if ('#' == $color_mask[0]) { |
||
118 | $color_mask = $this->colorhex2colorarray($color_mask); |
||
119 | } else { |
||
120 | require_once __DIR__ . '/Image/Transform/Driver/ColorsDefs.php'; |
||
121 | $color_mask = $colornames[$color_mask] ?? false; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // Do not round it, too much lost of quality |
||
126 | $cosT = cos($t); |
||
127 | $sinT = sin($t); |
||
128 | |||
129 | $img = &$this->imageHandle; |
||
130 | |||
131 | $width = $max_x = $this->img_x; |
||
132 | $height = $max_y = $this->img_y; |
||
133 | $min_y = 0; |
||
134 | $min_x = 0; |
||
135 | |||
136 | $x1 = round($max_x / 2, 0); |
||
137 | $y1 = round($max_y / 2, 0); |
||
138 | |||
139 | if ($autoresize) { |
||
140 | $t = abs($t); |
||
141 | $a = round($angle, 0); |
||
142 | switch ((int)$angle) { |
||
143 | case 0: |
||
144 | $width2 = $width; |
||
145 | $height2 = $height; |
||
146 | break; |
||
147 | case 90: |
||
148 | $width2 = $height; |
||
149 | $height2 = $width; |
||
150 | break; |
||
151 | case 180: |
||
152 | $width2 = $width; |
||
153 | $height2 = $height; |
||
154 | break; |
||
155 | case 270: |
||
156 | $width2 = $height; |
||
157 | $height2 = $width; |
||
158 | break; |
||
159 | default: |
||
160 | $width2 = (int)abs(sin($t) * $height + cos($t) * $width); |
||
161 | $height2 = (int)abs(cos($t) * $height + sin($t) * $width); |
||
162 | } |
||
163 | |||
164 | $width2 -= $width2 % 2; |
||
165 | $height2 -= $height2 % 2; |
||
166 | |||
167 | $d_width = abs($width - $width2); |
||
168 | $d_height = abs($height - $height2); |
||
169 | $x_offset = $d_width / 2; |
||
170 | $y_offset = $d_height / 2; |
||
171 | $min_x2 = -abs($x_offset); |
||
172 | $min_y2 = -abs($y_offset); |
||
173 | $max_x2 = $width2; |
||
174 | $max_y2 = $height2; |
||
175 | } |
||
176 | |||
177 | $img2 = @imagecreatetruecolor($width2, $height2); |
||
178 | |||
179 | if (!is_resource($img2)) { |
||
180 | return PEAR::raiseError('Cannot create buffer for the rotataion.', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); |
||
181 | } |
||
182 | |||
183 | $this->img_x = $width2; |
||
184 | $this->img_y = $height2; |
||
185 | |||
186 | imagepalettecopy($img2, $img); |
||
187 | |||
188 | $mask = imagecolorresolve($img2, $color_mask[0], $color_mask[1], $color_mask[2]); |
||
189 | |||
190 | // use simple lines copy for axes angles |
||
191 | switch ((int)$angle) { |
||
192 | case 0: |
||
193 | imagefill($img2, 0, 0, $mask); |
||
194 | for ($y = 0; $y < $max_y; $y++) { |
||
195 | for ($x = $min_x; $x < $max_x; $x++) { |
||
196 | $c = @imagecolorat($img, $x, $y); |
||
197 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
198 | } |
||
199 | } |
||
200 | break; |
||
201 | case 90: |
||
202 | imagefill($img2, 0, 0, $mask); |
||
203 | for ($x = $min_x; $x < $max_x; $x++) { |
||
204 | for ($y = $min_y; $y < $max_y; $y++) { |
||
205 | $c = imagecolorat($img, $x, $y); |
||
206 | imagesetpixel($img2, $max_y - $y - 1, $x, $c); |
||
207 | } |
||
208 | } |
||
209 | break; |
||
210 | case 180: |
||
211 | imagefill($img2, 0, 0, $mask); |
||
212 | for ($y = 0; $y < $max_y; $y++) { |
||
213 | for ($x = $min_x; $x < $max_x; $x++) { |
||
214 | $c = @imagecolorat($img, $x, $y); |
||
215 | imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c); |
||
216 | } |
||
217 | } |
||
218 | break; |
||
219 | case 270: |
||
220 | imagefill($img2, 0, 0, $mask); |
||
221 | for ($y = 0; $y < $max_y; $y++) { |
||
222 | for ($x = $max_x; $x >= $min_x; $x--) { |
||
223 | $c = @imagecolorat($img, $x, $y); |
||
224 | imagesetpixel($img2, $y, $max_x - $x - 1, $c); |
||
225 | } |
||
226 | } |
||
227 | break; |
||
228 | // simple reverse rotation algo |
||
229 | default: |
||
230 | $i = 0; |
||
231 | for ($y = $min_y2; $y < $max_y2; $y++) { |
||
232 | // Algebra :) |
||
233 | $x2 = round((($min_x2 - $x1) * $cosT) + (($y - $y1) * $sinT + $x1), 0); |
||
234 | $y2 = round(($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1, 0); |
||
235 | |||
236 | for ($x = $min_x2; $x < $max_x2; $x++) { |
||
237 | // Check if we are out of original bounces, if we are |
||
238 | // use the default color mask |
||
239 | if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) { |
||
240 | $c = imagecolorat($img, $x2, $y2); |
||
241 | } else { |
||
242 | $c = $mask; |
||
243 | } |
||
244 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
245 | |||
246 | // round verboten! |
||
247 | $x2 += $cosT; |
||
248 | $y2 -= $sinT; |
||
249 | } |
||
250 | } |
||
251 | break; |
||
252 | } |
||
253 | |||
254 | $this->imageHandle = $img2; |
||
255 | |||
256 | return true; |
||
257 | } |
||
259 |