PHP - 简单验证码

客官°小女子只卖身不卖艺 2023-06-22 14:58 119阅读 0赞
  1. <?php
  2. header("content-type:image/jpeg");
  3. $width = 120;
  4. $height = 40;
  5. $img = imagecreatetruecolor($width,$height);
  6. $color_bg = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
  7. $color_str = imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
  8. $element = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  9. $str = "";
  10. for($i=0;$i<5;$i++) {
  11. $str.=$element[rand(0,count($element)-1)];
  12. }
  13. imagefill($img,0,0,$color_bg);
  14. //imagesetpixel — 画一个单一像素
  15. //imagesetpixel( resource $image, int $x, int $y, int $color) : bool
  16. //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
  17. //循环输出,可以重复输出点
  18. for($i=0;$i<100;$i++) {
  19. imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),$color_str);
  20. }
  21. //循环输出多条线
  22. for($i=0;$i<3;$i++) {
  23. imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),$color_str);
  24. }
  25. //imagettftext — 用 TrueType 字体向图像写入文本
  26. imagettftext($img,25,rand(-5,5),rand(5,15),rand(30,35),$color_str,'fonts/ywsfxs.ttf',$str);
  27. imagejpeg($img);
  28. //最后要释放内存
  29. imagedestroy($img);
  30. //金龙制作

效果图;
效果图片

发表评论

表情:
评论列表 (有 0 条评论,119人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP 验证

    php 验证码 一个特别简单的PHP验证码的小案例,备注基本都写上了,不明白的可以跟我问哦,知无不言言无不尽。 html文件 文件名:vad-login.htmv

    相关 PHP实现验证

    PHP 利用自身带的gd库做验证码功能,也是很方便的,现在什么地方都在使用验证码,就是为了识别是人还是机器,防止一些不法分子对网站进行非法的流量攻击操作,比如论坛的灌水等,有了

    相关 php 实现验证

    \[导读\] 验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。 session\_start();     $im