php随机验证码图片生成实例详解

WBOY
Freigeben: 2016-07-25 08:52:47
Original
983 Leute haben es durchsucht
  1. /** 默认首页 **/

  2. class DefaultController extends AppController
  3. {
  4. public function index() {
  5. $len = 5;
  6. $str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789";
  7. $im = imagecreatetruecolor ( 70, 20 );

  8. $bgc = imagecolorallocate($im, 255, 255, 255);
  9. $bgtxt = imagecolorallocate($im, 220, 220, 220);
  10. //随机调色板

  11. $colors = array(
  12. imagecolorallocate($im, 255, 0, 0),
  13. imagecolorallocate($im, 0, 200, 0),
  14. imagecolorallocate($im, 0, 0, 255),
  15. imagecolorallocate($im, 0, 0, 0),
  16. imagecolorallocate($im, 255, 128, 0),
  17. imagecolorallocate($im, 255, 208, 0),
  18. imagecolorallocate($im, 98, 186, 245),
  19. );
  20. //填充背景色

  21. imagefill($im, 0, 0, $bgc);
  22. //随机获取数字

  23. $verify = "";
  24. while (strlen($verify) $i = strlen($verify);
  25. $random = $str[rand(0, strlen($str))];
  26. $verify .= $random;
  27. //绘制背景文字

  28. imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $bgtxt);
  29. //绘制主文字信息
  30. imagestring($im, 6, ($i*10)+3, rand(0,6), $random, $colors[rand(0, count($colors)-1)]);
  31. }
  32. //添加随机杂色

  33. for($i=0; $i$color = imagecolorallocate($im, rand(50,220), rand(50,220), rand(50,220));
  34. imagesetpixel($im, rand(0,70), rand(0,20), $color);
  35. }
  36. //将验证码存入$_SESSION中

  37. sess("verify", $verify);
  38. //输出图片并释放缓存

  39. header('Content-type: image/png');
  40. imagepng($im);
  41. imagedestroy($im);
  42. }
  43. };
  44. ?>
复制代码

例2,生成随机字符串和验证码的类的php实例 生成随机字符串和验证码的类。

以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_check_image(),输出图像直接调用后面的,session()取验证码时直接get_code()就可以。 使用session时必须将session_star()放在最前面。

完整代码:

  1. class RandCheckCode

  2. {
  3. /*函数名称:get_code()
  4. *作用:取得随机字符串
  5. * 参数:
  6. 1、(int)$length = 32 #随机字符长度
  7. 2、(int)$mode = 0 #随机字符类型,
  8. 0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,
  9. 4为大小写字母,5为大写字母和数字,6为小写字母和数字
  10. *返回:取得的字符串
  11. */
  12. function get_code($length=32,$mode=0)//获取随机验证码函数
  13. {
  14. switch ($mode)
  15. {
  16. case '1':
  17. $str='123456789';
  18. break;
  19. case '2':
  20. $str='abcdefghijklmnopqrstuvwxyz';
  21. break;
  22. case '3':
  23. $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  24. break;
  25. case '4':
  26. $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  27. break;
  28. case '5':
  29. $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  30. break;
  31. case '6':
  32. $str='abcdefghijklmnopqrstuvwxyz1234567890';
  33. break;
  34. default:
  35. $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
  36. break;
  37. }
  38. $checkstr='';
  39. $len=strlen($str)-1;
  40. for ($i=0;$i {
  41. //$num=rand(0,$len);//产生一个0到$len之间的随机数
  42. $num=mt_rand(0,$len);//产生一个0到$len之间的随机数
  43. $checkstr.=$str[$num];
  44. }
  45. return $checkstr;
  46. }
  47. /** 函数名称:create_check_image()

  48. 函数作用:产生一个校验码的图片
  49. 参 数:$checkcode:校验码字符串
  50. 返 回 值:返回该图片
  51. */
  52. function create_check_image($checkcode)//产生一个
  53. {
  54. $im=imagecreate(65,22);//产生一个图片
  55. $black=imagecolorallocate($im,0,0,0);//背景颜色
  56. $white=imagecolorallocate($im,255,255,255);//前景颜色
  57. $gray=imagecolorallocate($im,200,200,200);
  58. imagefill($im,30,30,$gray);//在$im图像的坐标30,30(图像左上角为0,0)处用$gray 颜色执行区域填充(即与30,30点颜色相同且相邻的点都会被填充)
  59. imagestring($im,5,8,3,$checkcode,$white);//用$white颜色将字符串$checkcode画到$im 所代表的图像的8,3坐标处(这是字符串左上角坐标,整幅图像的左上角为0,0),5是字体大小, 字体只能是1,2,3,4或5,使用内置字体

  60. for ($i=0;$i {
  61. $randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  62. imagesetpixel($im,rand()%70,rand()%30,$randcolor);//在$im图象上用$randcolor颜色在(rand()%70,rand()%30)坐标(图像左上角为0,0)上画一个点
  63. }
  64. header("Content-type:image/png");
  65. imagepng($im);//以PNG格式将图像输出到浏览器或文件
  66. imagedestroy($im);//销毁图像$im
  67. }
  68. }
  69. /*
  70. $randcode=new RandCheckCode();
  71. $checkstring=$randcode->get_code(5,7);
  72. $image=$randcode->create_check_image($checkstring);
  73. echo $image;
  74. */
  75. ?>
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!