PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/captcha.php

https://github.com/inderjeet123/project
PHP | 271 lines | 11 code | 4 blank | 256 comment | 0 complexity | 28aec59b66d02a6204c5f769e5543d53 MD5 | raw file
  1. <?php
  2. session_start();
  3. $ranStr = md5(microtime());
  4. $ranStr = substr($ranStr, 0, 6);
  5. $_SESSION['cap_code'] = $ranStr;
  6. $newImage = imagecreatefromjpeg("cap_bg.jpeg");
  7. $txtColor = imagecolorallocate($newImage, 0, 0, 0);
  8. imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
  9. header("Content-type: image/jpeg");
  10. imagejpeg($newImage);
  11. /*
  12. session_start();
  13. if(isset($_SESSION['captcha']))
  14. {
  15. unset($_SESSION['captcha']);
  16. }
  17. $num_chars=6;//number of characters for captcha image
  18. $characters=array_merge(range(0,9),range('A','Z'),range('a','z'));//creating combination of numbers & alphabets
  19. shuffle($characters);//shuffling the characters
  20. //getting the required random 5 characters
  21. $captcha_text="";
  22. for($i=0;$i<$num_chars;$i++)
  23. {
  24. $captcha_text.=$characters[rand(0,count($characters)-1)];
  25. }
  26. $_SESSION['captcha'] =$captcha_text;// assigning the text into session
  27. //header("Content-type: image/png");// setting the content type as png
  28. $captcha_image=imagecreatetruecolor(140,30);
  29. $captcha_background=imagecolorallocate($captcha_image,225,238,221);//setting captcha background colour
  30. $captcha_text_colour=imagecolorallocate($captcha_image,58,94,47);//setting cpatcha text colour
  31. imagefilledrectangle($captcha_image,0,0,140,29,$captcha_background);//creating the rectangle
  32. $font='arial.ttf';//setting the font path
  33. imagettftext($captcha_image,20,0,11,21,$captcha_text_colour,$font,$captcha_text);
  34. imagepng($captcha_image);
  35. imagedestroy($captcha_image);
  36. */
  37. /*
  38. class DayyanConfirmImage
  39. {
  40. private $showLine = true;
  41. private $applyWave = true;
  42. private $winHeight = 50;
  43. private $winWidth = 320;
  44. private $Characters; // random characters
  45. private $Colors = array ( '0' => '145',
  46. '1' => '204',
  47. '2' => '177',
  48. '3' => '184',
  49. '4' => '199',
  50. '5' => '255');
  51. ////////////////////////////////////////////////////////////////////////////////
  52. public function __construct($ConfirmCode)
  53. {
  54. $this -> Characters = $ConfirmCode;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. public function ShowImage()
  58. {
  59. //detect server operation system
  60. if ( strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' )//windows detected
  61. $this -> win();
  62. else //linux detected
  63. $this -> linux();
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. private function win()
  67. {
  68. ////////////////////////////////////
  69. //background image
  70. $image = imagecreatetruecolor($this -> winWidth, $this -> winHeight)
  71. or die("<b>" . __FILE__ . "</b><br />" . __LINE__ . " :
  72. " ."Cannot Initialize new GD image stream");
  73. $bg = imagecolorallocate($image, 255, 255, 255);
  74. imagefill($image, 10, 10, $bg);
  75. for ($x=0; $x < $this -> winWidth; $x++)
  76. {
  77. for ($y=0; $y < $this -> winHeight; $y++)
  78. {
  79. $random = mt_rand(0 , 5);
  80. $temp_color = imagecolorallocate($image,
  81. $this -> Colors["$random"],
  82. $this -> Colors["$random"], $this -> Colors["$random"]);
  83. imagesetpixel( $image, $x, $y , $temp_color );
  84. }
  85. }
  86. $char_color = imagecolorallocatealpha($image, 0, 0, 0, 90);
  87. //Font
  88. $font = "tahomabd.ttf";
  89. $font_size = 33;
  90. ////////////////////////////////////
  91. //Image characters
  92. $char = "";
  93. $char = $this -> Characters[0];
  94. $random_x = mt_rand(10 , 20);
  95. $random_y = mt_rand(35 , 45);
  96. $random_angle = mt_rand(-20 , 20);
  97. imagettftext($image, $font_size, $random_angle,
  98. $random_x, $random_y, $char_color, $font, $char);
  99. $char = $this -> Characters[1];
  100. $random_x = mt_rand(50 , 70);
  101. $random_y = mt_rand(35 , 45);
  102. $random_angle = mt_rand(-20 , 20);
  103. imagettftext($image, $font_size, $random_angle,
  104. $random_x, $random_y, $char_color, $font, $char);
  105. $char = $this -> Characters[2];
  106. $random_x = mt_rand(100 , 120);
  107. $random_y = mt_rand(35 , 45);
  108. $random_angle = mt_rand(-20 , 20);
  109. imagettftext($image, $font_size, $random_angle,
  110. $random_x, $random_y, $char_color, $font, $char);
  111. $char = $this -> Characters[3];
  112. $random_x = mt_rand(150 , 170);
  113. $random_y = mt_rand(35 , 45);
  114. $random_angle = mt_rand(-20 , 20);
  115. imagettftext($image, $font_size, $random_angle,
  116. $random_x, $random_y, $char_color, $font, $char);
  117. $char = $this -> Characters[4];
  118. $random_x = mt_rand(200 , 220);
  119. $random_y = mt_rand(35 , 45);
  120. $random_angle = mt_rand(-20 , 20);
  121. imagettftext($image, $font_size, $random_angle,
  122. $random_x, $random_y, $char_color, $font, $char);
  123. $char = $this -> Characters[5];
  124. $random_x = mt_rand(250 , 270);
  125. $random_y = mt_rand(35 , 45);
  126. $random_angle = mt_rand(-20 , 20);
  127. imagettftext($image, $font_size, $random_angle,
  128. $random_x, $random_y, $char_color, $font, $char);
  129. ////////////////////////////////////
  130. if ($this -> applyWave)
  131. $image = $this -> apply_wave($image, $this -> winWidth,
  132. $this -> winHeight);
  133. ////////////////////////////////////
  134. //lines
  135. if ($this -> showLine)
  136. {
  137. for ($i=0; $i<$this->winWidth; $i++ )
  138. {
  139. if ($i%10 == 0)
  140. {
  141. imageline ( $image, $i, 0,
  142. $i+10, 50, $char_color );
  143. imageline ( $image, $i, 0,
  144. $i-10, 50, $char_color );
  145. }
  146. }
  147. }
  148. ////////////////////////////////////
  149. return imagepng($image);
  150. imagedestroy($image);
  151. }
  152. /////////////////////////////////////////////////////////////
  153. private function linux()
  154. {
  155. ////////////////////////////////////
  156. //Background image
  157. $image = imagecreatetruecolor(150, 50) or
  158. die("<b>" . __FILE__ . "</b><br />" . __LINE__ . " : "
  159. ."Cannot Initialize new GD image stream");
  160. $bg = imagecolorallocate($image, 255, 255, 255);
  161. imagefill($image, 10, 10, $bg);
  162. for ($x=0; $x < 150; $x++)
  163. {
  164. for ($y=0; $y < 50; $y++)
  165. {
  166. $random = mt_rand(0 , 5);
  167. $temp_color = imagecolorallocate($image, $this ->
  168. Colors["$random"], $this -> Colors["$random"], $this ->
  169. Colors["$random"]);
  170. imagesetpixel( $image, $x, $y , $temp_color );
  171. }
  172. }
  173. $char_color = imagecolorallocatealpha($image, 0, 0, 0, 60);
  174. ////////////////////////////////////
  175. //Image Info
  176. $font = 5;
  177. ////////////////////////////////////
  178. //Image characters
  179. $char = $this -> Characters[0];
  180. $random_x = mt_rand(10 , 20);
  181. $random_y = mt_rand(15,25);
  182. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  183. $char = $this -> Characters[1];
  184. $random_x = mt_rand(30 , 40);
  185. $random_y = mt_rand(15,25);
  186. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  187. $char = $this -> Characters[2];
  188. $random_x = mt_rand(50 , 60);
  189. $random_y = mt_rand(15,25);
  190. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  191. $char = $this -> Characters[3];
  192. $random_x = mt_rand(70 , 80);
  193. $random_y = mt_rand(15,25);
  194. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  195. $char = $this -> Characters[4];
  196. $random_x = mt_rand(90 , 100);
  197. $random_y = mt_rand(15,25);
  198. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  199. $char = $this -> Characters[5];
  200. $random_x = mt_rand(110 , 120);
  201. $random_y = mt_rand(15,25);
  202. imagestring($image, $font, $random_x, $random_y, $char, $char_color);
  203. ///////////////////////
  204. return imagepng($image);
  205. imagedestroy($image);
  206. }
  207. ///////////////////////////////////////////////////////////
  208. private function apply_wave($image, $width, $height)
  209. {
  210. $x_period = 10;
  211. $y_period = 10;
  212. $y_amplitude = 5;
  213. $x_amplitude = 5;
  214. $xp = $x_period*rand(1,3);
  215. $k = rand(0,100);
  216. for ($a = 0; $a<$width; $a++)
  217. imagecopy($image, $image, $a-1, sin($k+$a/$xp)*$x_amplitude,
  218. $a, 0, 1, $height);
  219. $yp = $y_period*rand(1,2);
  220. $k = rand(0,100);
  221. for ($a = 0; $a<$height; $a++)
  222. imagecopy($image, $image, sin($k+$a/$yp)*$y_amplitude,
  223. $a-1, 0, $a, $width, 1);
  224. return $image;
  225. }
  226. }
  227. */
  228. ?>