Advertisement
Guest User

Capcha

a guest
Jan 27th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php                                                  
  2. session_start();
  3. $image_width = 110;
  4. $image_height = 32;
  5. $characters_on_image = 6;
  6. $font = './template/font/monofont.ttf';
  7. // key
  8. $possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
  9. $random_dots = 0;
  10. $random_lines = 6;
  11. $captcha_text_color="#000";
  12. $captcha_noice_color = "#000";
  13.  
  14. $code = '';
  15.  
  16. $i = 0;
  17. while ($i < $characters_on_image) {
  18. $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
  19. $i++;
  20. }
  21.  
  22.  
  23. $font_size = $image_height * 0.75;
  24. $image = @imagecreate($image_width, $image_height);
  25. // nen
  26. $background_color = imagecolorallocate($image, 249,249, 249);
  27.  
  28. $arr_text_color = hexrgb($captcha_text_color);
  29. $text_color = imagecolorallocate($image, $arr_text_color['red'],
  30.         $arr_text_color['green'], $arr_text_color['blue']);
  31.  
  32. $arr_noice_color = hexrgb($captcha_noice_color);
  33. $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
  34.         $arr_noice_color['green'], $arr_noice_color['blue']);
  35.  
  36. for( $i=0; $i<$random_dots; $i++ ) {
  37. imagefilledellipse($image, mt_rand(0,$image_width),
  38.  mt_rand(0,$image_height), 2, 3, $image_noise_color);
  39. }
  40. /* generating lines randomly in background of image */
  41. for( $i=0; $i<$random_lines; $i++ ) {
  42. imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
  43.  mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
  44. }
  45.  
  46.  
  47. /* create a text box and add 6 letters code in it */
  48. $textbox = imagettfbbox($font_size, 0, $font, $code);
  49. $x = ($image_width - $textbox[4])/2;
  50. $y = ($image_height - $textbox[5])/2;
  51. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
  52.  
  53.  
  54. /* Show captcha image in the page html page */
  55. header('Content-Type: image/jpeg');
  56. imagejpeg($image);//showing the image
  57. imagedestroy($image);//destroying the image instance
  58. $_SESSION['captcha'] = $code;
  59.  
  60. function hexrgb ($hexstr){
  61.   $int = hexdec($hexstr);
  62.   return array("red" => 0xFF & ($int >> 0x10),
  63.                "green" => 0xFF & ($int >> 0x8),
  64.                "blue" => 0xFF & $int);
  65. }
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement