| Conditions | 32 |
| Paths | 2305 |
| Total Lines | 174 |
| Lines | 112 |
| Ratio | 64.37 % |
| 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 |
||
| 157 | function rotate($angle, $options=null) |
||
| 158 | { |
||
| 159 | if(function_exists('imagerotate') && false) { |
||
| 160 | $white = imagecolorallocate ($this->imageHandle, 255, 255, 255); |
||
| 161 | $this->imageHandle = imagerotate($this->imageHandle, $angle, $white); |
||
| 162 | return true; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ( $options==null ){ |
||
| 166 | $autoresize = true; |
||
| 167 | $color_mask = array(255,255,0); |
||
| 168 | } else { |
||
| 169 | extract( $options ); |
||
| 170 | } |
||
| 171 | |||
| 172 | while ($angle <= -45) { |
||
| 173 | $angle += 360; |
||
| 174 | } |
||
| 175 | while ($angle > 270) { |
||
| 176 | $angle -= 360; |
||
| 177 | } |
||
| 178 | |||
| 179 | $t = deg2rad($angle); |
||
| 180 | |||
| 181 | if( !is_array($color_mask) ){ |
||
| 182 | if ($color[0]=='#'){ |
||
| 183 | $this->colorhex2colorarray( $color_mask ); |
||
| 184 | } else { |
||
| 185 | include_once('Image/Transform/Driver/ColorDefs.php'); |
||
| 186 | $color = isset($colornames[$color_mask])?$colornames[$color_mask]:false; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | // Do not round it, too much lost of quality |
||
| 191 | $cosT = cos($t); |
||
| 192 | $sinT = sin($t); |
||
| 193 | |||
| 194 | $img =& $this->imageHandle; |
||
| 195 | |||
| 196 | $width = $max_x = $this->img_x; |
||
| 197 | $height = $max_y = $this->img_y; |
||
| 198 | $min_y = 0; |
||
| 199 | $min_x = 0; |
||
| 200 | |||
| 201 | $x1 = round($max_x/2,0); |
||
| 202 | $y1 = round($max_y/2,0); |
||
| 203 | |||
| 204 | if ( $autoresize ){ |
||
| 205 | $t = abs($t); |
||
| 206 | $a = round($angle,0); |
||
| 207 | switch((int)($angle)){ |
||
| 208 | case 0: |
||
| 209 | $width2 = $width; |
||
| 210 | $height2 = $height; |
||
| 211 | break; |
||
| 212 | case 90: |
||
| 213 | $width2 = $height; |
||
| 214 | $height2 = $width; |
||
| 215 | break; |
||
| 216 | case 180: |
||
| 217 | $width2 = $width; |
||
| 218 | $height2 = $height; |
||
| 219 | break; |
||
| 220 | case 270: |
||
| 221 | $width2 = $height; |
||
| 222 | $height2 = $width; |
||
| 223 | break; |
||
| 224 | default: |
||
| 225 | $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); |
||
| 226 | $height2 = (int)(abs(cos($t) * $height+sin($t) * $width)); |
||
| 227 | } |
||
| 228 | |||
| 229 | $width2 -= $width2%2; |
||
| 230 | $height2 -= $height2%2; |
||
| 231 | |||
| 232 | $d_width = abs($width - $width2); |
||
| 233 | $d_height = abs($height - $height2); |
||
| 234 | $x_offset = $d_width/2; |
||
| 235 | $y_offset = $d_height/2; |
||
| 236 | $min_x2 = -abs($x_offset); |
||
| 237 | $min_y2 = -abs($y_offset); |
||
| 238 | $max_x2 = $width2; |
||
| 239 | $max_y2 = $height2; |
||
| 240 | } |
||
| 241 | |||
| 242 | if(function_exists('ImageCreateTrueColor')){ |
||
| 243 | $img2 =ImageCreateTrueColor($width2,$height2); |
||
| 244 | } else { |
||
| 245 | $img2 =ImageCreate($width2,$height2); |
||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | if ( !is_resource($img2) ){ |
||
| 250 | return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.', |
||
| 251 | null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ |
||
| 252 | } |
||
| 253 | |||
| 254 | $this->img_x = $width2; |
||
| 255 | $this->img_y = $height2; |
||
| 256 | |||
| 257 | |||
| 258 | imagepalettecopy($img2,$img); |
||
| 259 | |||
| 260 | $mask = imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]); |
||
| 261 | |||
| 262 | // use simple lines copy for axes angles |
||
| 263 | switch((int)($angle)){ |
||
| 264 | case 0: |
||
| 265 | imagefill ($img2, 0, 0,$mask); |
||
| 266 | for ($y=0; $y < $max_y; $y++) { |
||
| 267 | for ($x = $min_x; $x < $max_x; $x++){ |
||
| 268 | $c = @imagecolorat ( $img, $x, $y); |
||
| 269 | imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | break; |
||
| 273 | case 90: |
||
| 274 | imagefill ($img2, 0, 0,$mask); |
||
| 275 | for ($x = $min_x; $x < $max_x; $x++){ |
||
| 276 | for ($y=$min_y; $y < $max_y; $y++) { |
||
| 277 | $c = imagecolorat ( $img, $x, $y); |
||
| 278 | imagesetpixel($img2,$max_y-$y-1,$x,$c); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | break; |
||
| 282 | case 180: |
||
| 283 | imagefill ($img2, 0, 0,$mask); |
||
| 284 | for ($y=0; $y < $max_y; $y++) { |
||
| 285 | for ($x = $min_x; $x < $max_x; $x++){ |
||
| 286 | $c = @imagecolorat ( $img, $x, $y); |
||
| 287 | imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | break; |
||
| 291 | case 270: |
||
| 292 | imagefill ($img2, 0, 0,$mask); |
||
| 293 | for ($y=0; $y < $max_y; $y++) { |
||
| 294 | for ($x = $max_x; $x >= $min_x; $x--){ |
||
| 295 | $c = @imagecolorat ( $img, $x, $y); |
||
| 296 | imagesetpixel($img2,$y,$max_x-$x-1,$c); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | break; |
||
| 300 | // simple reverse rotation algo |
||
| 301 | default: |
||
| 302 | $i=0; |
||
| 303 | for ($y = $min_y2; $y < $max_y2; $y++){ |
||
| 304 | |||
| 305 | // Algebra :) |
||
| 306 | $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0); |
||
| 307 | $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0); |
||
| 308 | |||
| 309 | for ($x = $min_x2; $x < $max_x2; $x++){ |
||
| 310 | |||
| 311 | // Check if we are out of original bounces, if we are |
||
| 312 | // use the default color mask |
||
| 313 | if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){ |
||
| 314 | $c = imagecolorat ( $img, $x2, $y2); |
||
| 315 | } else { |
||
| 316 | $c = $mask; |
||
| 317 | } |
||
| 318 | imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); |
||
| 319 | |||
| 320 | // round verboten! |
||
| 321 | $x2 += $cosT; |
||
| 322 | $y2 -= $sinT; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | break; |
||
| 326 | } |
||
| 327 | $this->old_image = $this->imageHandle; |
||
| 328 | $this->imageHandle = $img2; |
||
| 329 | return true; |
||
| 330 | } |
||
| 331 | |||
| 509 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.