| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 239 |
| Code Lines | 122 |
| Lines | 11 |
| Ratio | 4.6 % |
| Changes | 2 | ||
| 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 |
||
| 157 | function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { |
||
| 158 | /** |
||
| 159 | * Function to create a random color |
||
| 160 | * Note: We aren't using this outside this function so we will sit it inside |
||
| 161 | * @auteur mastercode.nl |
||
| 162 | * @param $type string Mode for the color |
||
| 163 | * @return int |
||
| 164 | * */ |
||
| 165 | if (!function_exists('color')) { |
||
| 166 | |||
| 167 | function color($type) { |
||
| 168 | switch ($type) { |
||
| 169 | case 'bg': |
||
| 170 | //$color = rand(224,255); |
||
| 171 | $color = 255; |
||
| 172 | break; |
||
| 173 | case 'text': |
||
| 174 | $color = rand(0, 127); |
||
| 175 | break; |
||
| 176 | case 'grid': |
||
| 177 | $color = rand(200, 224); |
||
| 178 | break; |
||
| 179 | default: |
||
| 180 | $color = rand(0, 255); |
||
| 181 | break; |
||
| 182 | } |
||
| 183 | return $color; |
||
| 184 | } |
||
| 185 | |||
| 186 | } |
||
| 187 | |||
| 188 | $defaults = [ |
||
| 189 | 'word' => '', |
||
| 190 | 'img_path' => '', |
||
| 191 | 'img_url' => '', |
||
| 192 | 'img_width' => '150', |
||
| 193 | 'img_height' => '30', |
||
| 194 | 'font_size' => '', |
||
| 195 | 'font_path' => '', |
||
| 196 | 'show_grid' => true, |
||
| 197 | 'skew' => true, |
||
| 198 | 'expiration' => 7200, |
||
| 199 | 'alt' => 'captcha', |
||
| 200 | ]; |
||
| 201 | |||
| 202 | foreach ($defaults as $key => $val) { |
||
| 203 | if (!is_array($data)) { |
||
| 204 | if (!isset($$key) OR $ $key == '') { |
||
| 205 | $$key = $val; |
||
| 206 | } |
||
| 207 | } else { |
||
| 208 | $$key = (!isset($data[$key])) ? $val : $data[$key]; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($img_path == '' OR $img_url == '') { |
||
| 213 | |||
| 214 | return FALSE; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!@is_dir($img_path)) { |
||
| 218 | return FALSE; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (!is_really_writable($img_path)) { |
||
| 222 | return FALSE; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (!extension_loaded('gd')) { |
||
| 226 | return FALSE; |
||
| 227 | } |
||
| 228 | |||
| 229 | // ----------------------------------- |
||
| 230 | // Select random Font from folder |
||
| 231 | // ----------------------------------- |
||
| 232 | |||
| 233 | if (is_dir($font_path)) { |
||
| 234 | $handle = opendir($font_path); |
||
| 235 | |||
| 236 | while (($file = @readdir($handle)) !== false) { |
||
| 237 | if (!in_array($file, ['.', '..']) && substr($file, strlen($file) - 4, 4) == '.ttf') { |
||
| 238 | $fonts[] = $file; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | $font_file = $font_path . DIRECTORY_SEPARATOR . $fonts[array_rand($fonts)]; |
||
| 243 | } else { |
||
| 244 | $font_file = $font_path; |
||
| 245 | } |
||
| 246 | |||
| 247 | // ----------------------------------- |
||
| 248 | // Remove old images |
||
| 249 | // ----------------------------------- |
||
| 250 | |||
| 251 | list($usec, $sec) = explode(' ', microtime()); |
||
| 252 | $now = ((float) $usec + (float) $sec); |
||
| 253 | |||
| 254 | $current_dir = @opendir($img_path); |
||
| 255 | |||
| 256 | while ($filename = @readdir($current_dir)) { |
||
| 257 | if ($filename != '.' and $filename != '..' and $filename != 'index.html') { |
||
| 258 | $name = str_replace('.png', '', $filename); |
||
| 259 | |||
| 260 | if (($name + $expiration) < $now) { |
||
| 261 | @unlink($img_path . $filename); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | @closedir($current_dir); |
||
| 267 | |||
| 268 | // ----------------------------------- |
||
| 269 | // Do we have a "word" yet? |
||
| 270 | // ----------------------------------- |
||
| 271 | |||
| 272 | if ($word == '') { |
||
| 273 | // No Zero (for user clarity); |
||
| 274 | $pool = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 275 | |||
| 276 | $str = ''; |
||
| 277 | View Code Duplication | for ($i = 0; $i < 6; $i++) { |
|
| 278 | $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1); |
||
| 279 | } |
||
| 280 | |||
| 281 | $word = strtoupper($str); |
||
| 282 | } |
||
| 283 | |||
| 284 | // ----------------------------------- |
||
| 285 | // Length of Word |
||
| 286 | // ----------------------------------- |
||
| 287 | |||
| 288 | $length = strlen($word); |
||
| 289 | |||
| 290 | // ----------------------------------- |
||
| 291 | // Create image |
||
| 292 | // ----------------------------------- |
||
| 293 | |||
| 294 | $im = imagecreatetruecolor($img_width, $img_height); |
||
| 295 | |||
| 296 | // ----------------------------------- |
||
| 297 | // Assign colors |
||
| 298 | // ----------------------------------- |
||
| 299 | |||
| 300 | $bg_color = imagecolorallocatealpha($im, color('bg'), color('bg'), color('bg'), 0); |
||
| 301 | $border_color = imagecolorallocate($im, 255, 255, 255); |
||
| 302 | $text_color = imagecolorallocate($im, color('text'), color('text'), color('text')); |
||
| 303 | $grid_color[] = imagecolorallocate($im, color('grid'), color('grid'), color('grid')); |
||
| 304 | $grid_color[] = $grid_color[0] + 150; |
||
| 305 | $grid_color[] = $grid_color[0] + 180; |
||
| 306 | $grid_color[] = $grid_color[0] + 210; |
||
| 307 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
||
| 308 | |||
| 309 | // ----------------------------------- |
||
| 310 | // Create the rectangle |
||
| 311 | // ----------------------------------- |
||
| 312 | |||
| 313 | imagefilledrectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
||
| 314 | |||
| 315 | if ($show_grid == TRUE) { |
||
| 316 | // X grid |
||
| 317 | $grid = rand(20, 25); |
||
| 318 | View Code Duplication | for ($x = 0; $x < $img_width; $x += mt_rand($grid - 2, $grid + 2)) { |
|
| 319 | $current_colour = $grid_color[array_rand($grid_color)]; |
||
| 320 | imagedashedline($im, mt_rand($x - 3, $x + 3), mt_rand(0, 4), mt_rand($x - 3, $x + 3), mt_rand($img_height - 5, $img_height), $current_colour); |
||
| 321 | } |
||
| 322 | |||
| 323 | // Y grid |
||
| 324 | View Code Duplication | for ($y = 0; $y < $img_height; $y += mt_rand($grid - 2, $grid + 2)) { |
|
| 325 | $current_colour = $grid_color[array_rand($grid_color)]; |
||
| 326 | imageline($im, mt_rand(0, 4), mt_rand($y - 3, $y), mt_rand($img_width - 5, $img_width), mt_rand($y - 3, $y), $current_colour); |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | // ----------------------------------- |
||
| 331 | // Write the text |
||
| 332 | // ----------------------------------- |
||
| 333 | |||
| 334 | $use_font = ($font_file != '' AND file_exists($font_file) AND function_exists('imagettftext')) ? TRUE : FALSE; |
||
| 335 | |||
| 336 | if ($use_font == FALSE) { |
||
| 337 | $font_size = 5; |
||
| 338 | $x = rand(2, $img_width / ($length / 3)); |
||
| 339 | // y isnt used here |
||
| 340 | } else { |
||
| 341 | // Make font proportional to the image size |
||
| 342 | $font_size = !empty($font_size) ? $font_size : mt_rand(18, 25); |
||
| 343 | $x = rand(4, $img_width - (($font_size + ($font_size >> 1)) * $length)); |
||
| 344 | // y isnt used here |
||
| 345 | } |
||
| 346 | $wordLen = strlen($word); |
||
| 347 | for ($i = 0; $i < $wordLen; $i++) { |
||
| 348 | if ($use_font == FALSE) { |
||
| 349 | $y = rand(0, $img_height / 2); |
||
| 350 | imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color); |
||
| 351 | $x += ($font_size * 2); |
||
| 352 | } else { |
||
| 353 | $letter = substr($word, $i, 1); |
||
| 354 | $less_rotate = [ |
||
| 355 | 'c', |
||
| 356 | 'N', |
||
| 357 | 'U', |
||
| 358 | 'Z', |
||
| 359 | '7', |
||
| 360 | '6', |
||
| 361 | '9', |
||
| 362 | ]; //letters that we don't want rotated too much... |
||
| 363 | |||
| 364 | $angle = $skew == TRUE ? (in_array($letter, $less_rotate)) ? rand(-5, 5) : rand(-15, 15) : 0; |
||
| 365 | $y = $img_height / 2 + ($font_size >> 1) + ($skew == TRUE ? rand(-9, 9) : 0); |
||
| 366 | $x += ($font_size >> 2); |
||
| 367 | imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_file, $letter); |
||
| 368 | $x += $font_size + ($font_size >> 2); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | // ----------------------------------- |
||
| 373 | // Create the border |
||
| 374 | // ----------------------------------- |
||
| 375 | |||
| 376 | imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color); |
||
| 377 | |||
| 378 | // ----------------------------------- |
||
| 379 | // Generate the image |
||
| 380 | // ----------------------------------- |
||
| 381 | |||
| 382 | $img_name = $now . '.png'; |
||
| 383 | |||
| 384 | imagepng($im, $img_path . $img_name); |
||
| 385 | |||
| 386 | $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\"$alt\" />"; |
||
| 387 | |||
| 388 | imagedestroy($im); |
||
| 389 | |||
| 390 | return [ |
||
| 391 | 'word' => $word, |
||
| 392 | 'time' => $now, |
||
| 393 | 'image' => $img, |
||
| 394 | ]; |
||
| 395 | } |
||
| 396 | |||
| 397 | } |