Source for file captcha.php

Documentation is available at captcha.php

  1. <?php
  2.  
  3. $c_name            'mos_captcha';
  4. $c_width        '250';
  5. $c_height        '70';
  6. $c_imgtype    'png';
  7. $c_codetype 'true';
  8.  
  9. /**
  10.     * Spam Protection - Code Image Generator - 2006 Dominik Paulus
  11.     * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  12.     * @author: Dominik Paulus, [email]mail@dpaulus.de[/email]
  13.     * @date: 06/24/06
  14.     * @version: 3.4c
  15.     *
  16.     */
  17.  
  18. // Imagecolors
  19. $colors array(
  20. 255244234// Background
  21. 2551280,   // Code
  22. 2551280,   // Vertical Lines
  23. 2551280    // Border (Last value without ',')
  24. );
  25.  
  26. $x $c_width;
  27. $y $c_height;
  28. $y2 $y/2$x2 $x/2;
  29.  
  30. session_name($c_name);
  31. $_SESSION['img'"OK";  // debug
  32.  
  33. mt_srand((double)microtime()*1000000);
  34.  
  35. // Fontsetup
  36. $font './captchaFonts/FreeFarsi.ttf';
  37.  
  38. // Numerical code
  39. if($c_codetype)
  40. $seccode strval(mt_rand(1000099999));
  41. else {
  42.     $string "abcdefghjkmnpqrstuvwxyz0123456789";
  43.     $stringlen strlen($string);
  44.     $seccode "";
  45.     for($i 0$i 5$i++)
  46.     $seccode .= $string{mt_rand(0$stringlen)};
  47. }
  48. $_SESSION['code'$seccode;
  49. $ennum array(0,1,2,3,4,5,6,7,8,9);
  50. $fanum array('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹');
  51. $seccode str_replace($ennum$fanum$seccode);
  52. $box convertBoundingBox(imagettfbbox(200$font$seccode));
  53. $im imagecreate($box['width']+5$box['height']+5);
  54.  
  55. // Create some colors
  56. $white imagecolorallocate($im255255255);
  57. $grey imagecolorallocate($im128128128);
  58. $black imagecolorallocate($imrand(0,200)rand(0,200)rand(0,200));
  59. // Add some shadow to the text
  60. imagettftext($im200226$grey$font$seccode);
  61.  
  62. // Add the text
  63. imagettftext($im200325$black$font$seccode);
  64.  
  65. switch($c_imgtype{
  66.     case 'jpeg':
  67.         Header("Content-Type: image/jpeg");
  68.         ImageJPEG($im,"",75);
  69.         break;
  70.     case 'png':
  71.         Header("Content-Type: image/png");
  72.         ImagePNG($im);
  73.         break;
  74.     case 'gif':
  75.         Header("Content-Type: image/gif");
  76.         ImageGIF($im);
  77.         break;
  78.     default:
  79.         die("Wrong \$type in captcha.php (should be jpeg, png or gif)\n");
  80. }
  81.  
  82.  
  83. ImageDestroy($im);
  84.  
  85. function convertBoundingBox($bbox{
  86.     if ($bbox[0>= -1)
  87.     $xOffset = -abs($bbox[01);
  88.     else
  89.     $xOffset abs($bbox[02);
  90.     $width abs($bbox[2$bbox[0]);
  91.     if ($bbox[0< -1$width abs($bbox[2]abs($bbox[0]1;
  92.     $yOffset abs($bbox[51);
  93.     if ($bbox[5>= -1$yOffset = -$yOffset// Fixed characters below the baseline.
  94.     $height abs($bbox[7]abs($bbox[1]);
  95.     if ($bbox[30$height abs($bbox[7$bbox[1]1;
  96.     return array(
  97.     'width' => $width,
  98.     'height' => $height,
  99.     'xOffset' => $xOffset// Using xCoord + xOffset with imagettftext puts the left most pixel of the text at xCoord.
  100.     'yOffset' => $yOffset// Using yCoord + yOffset with imagettftext puts the top most pixel of the text at yCoord.
  101.     'belowBasepoint' => max(0$bbox[1])
  102.     );
  103. }
  104. ?>

Documentation generated on Mon, 05 May 2008 16:16:50 +0400 by phpDocumentor 1.4.0