Home > Backend Development > PHP Tutorial > PHP uses the GD library to implement a simple verification code

PHP uses the GD library to implement a simple verification code

WBOY
Release: 2016-07-25 08:42:30
Original
804 people have browsed it
  1. $img=imagecreatetruecolor(100, 40);
  2. $red=imagecolorallocate($img, 255, 0, 0);
  3. $green=imagecolorallocate($img, 0, 255, 0) ;
  4. $blue=imagecolorallocate($img, 0, 0, 255);
  5. $white=imagecolorallocate($img, 255, 255, 255);
  6. $black=imagecolorallocate($img, 0, 0, 0);
  7. //Generate image
  8. imagefill($img, 0, 0, $black);
  9. //Set verification code
  10. $code="";
  11. for($i=0;$i<5;$i++){
  12. $ code.=rand(0,9);
  13. }
  14. //Write the verification code into the image
  15. imagestring($img, 5, 20, 15, $code, $white);
  16. //Add a little interference
  17. for($ i=0;$i<10;$i++){
  18. imagesetpixel($img, rand(0,100), rand(0,40), $red);
  19. imagesetpixel($img, rand(0,100), rand(0, 40), $green);
  20. imagesetpixel($img, rand(0,100), rand(0,40), $blue);
  21. }
  22. //Add some interference
  23. for($i=0;$i<1 ;$i++){
  24. imageline($img, rand(0,50), rand(0,20), rand(50,100), rand(20,40), $red);
  25. imageline($img, rand(0 ,50), rand(0,20), rand(50,100), rand(20,40), $green);
  26. imageline($img, rand(0,50), rand(0,20), rand(50,100 ), rand(20,40), $blue);
  27. }
  28. header("Content-type:image/png");
  29. imagepng($img);
  30. imagedestroy($img);
  31. ?>
Copy code

Verification code, PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template