Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php图片验证码代码多个实例

php图片验证码代码多个实例

WBOY
WBOYasal
2016-07-25 08:52:521090semak imbas
  1. //文件头...

  2. header("Content-type: image/png");
  3. //创建真彩色白纸
  4. $im = @imagecreatetruecolor(50, 20) or die("建立图像失败");
  5. //获取背景颜色
  6. $background_color = imagecolorallocate($im, 255, 255, 255);
  7. //填充背景颜色(这个东西类似油桶)
  8. imagefill($im,0,0,$background_color);
  9. //获取边框颜色
  10. $border_color = imagecolorallocate($im,200,200,200);
  11. //画矩形,边框颜色200,200,200
  12. imagerectangle($im,0,0,49,19,$border_color);
  13. //逐行炫耀背景,全屏用1或0

  14. for($i=2;$i //获取随机淡色
  15. $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
  16. //画线
  17. imageline($im,2,$i,47,$i,$line_color);
  18. }
  19. //设置字体大小

  20. $font_size=12;
  21. //设置印上去的文字

  22. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  23. $Str[1] = "abcdefghijklmnopqrstuvwxyz";
  24. $Str[2] = "01234567891234567890123456";
  25. //获取第1个随机文字

  26. $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
  27. $imstr[0]["x"] = rand(2,5);
  28. $imstr[0]["y"] = rand(1,4);
  29. //获取第2个随机文字

  30. $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
  31. $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
  32. $imstr[1]["y"] = rand(1,3);
  33. //获取第3个随机文字

  34. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
  35. $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
  36. $imstr[2]["y"] = rand(1,4);
  37. //获取第4个随机文字

  38. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  39. $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
  40. $imstr[3]["y"] = rand(1,3);
  41. //写入随机字串

  42. for($i=0;$i //获取随机较深颜色
  43. $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
  44. //画文字
  45. imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color);
  46. }
  47. //显示图片

  48. imagepng($im);
  49. //销毁图片
  50. imagedestroy($im);
  51. ?>
复制代码

例2,漂亮的PHP图片验证码实例 完整代码:

  1. /*
  2. * @Author fy
  3. */
  4. $imgwidth =100; //图片宽度
  5. $imgheight =40; //图片高度
  6. $codelen =4; //验证码长度
  7. $fontsize =20; //字体大小
  8. $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
  9. $font = 'Fonts/segoesc.ttf';
  10. $im=imagecreatetruecolor($imgwidth,$imgheight);
  11. $while=imageColorAllocate($im,255,255,255);
  12. imagefill($im,0,0,$while); //填充图像
  13. //取得字符串
  14. $authstr='';
  15. $_len = strlen($charset)-1;
  16. for ($i=0;$i $authstr .= $charset[mt_rand(0,$_len)];
  17. }
  18. session_start();
  19. $_SESSION['scode']=strtolower($authstr);//全部转为小写,主要是为了不区分大小写
  20. //随机画点,已经改为划星星了
  21. for ($i=0;$i $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
  23. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
  24. }
  25. //随机画线,线条数量=字符数量(随便)
  26. for($i=0;$i{
  27. $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  28. imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
  29. }
  30. $_x=intval($imgwidth/$codelen); //计算字符距离
  31. $_y=intval($imgheight*0.7); //字符显示在图片70%的位置
  32. for($i=0;$i $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
  33. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
  34. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
  35. imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);
  36. }
  37. //生成图像
  38. header("content-type:image/PNG");
  39. imagePNG($im);
  40. imageDestroy($im);
复制代码

例3,php5 图片验证码实例代码

php5生成图片验证码的例子。

需要用到php GD库函数: 1,imagecreatetruecolor -----创建一个真彩色的图像 imagecreatetruecolor(int x_size,int y_size) //x表示宽,y表示高 2,imagecolorallocate 为一幅图像分配颜色(调色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 绘图函数 iamgestring(resource image,font,int x,int y,内容,颜色); 4,输出函数 php的header是定义头的动作,php5中支持3中类型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示内容文件的类型 如:image/gif image/jpeg image/png 例子:header("Content-type:image/jpeg") GD库中有对应的image类型 imagejpeg(),imagegif(),imagepang() 5,imageline画线函数 iamgeline(resource image,int x1,int y1,int x2,int y2,int color); image ---图片 x1 ---启始坐标 y1 x2 ---终点坐标 y2 6,imagesetpixel画点函数 imagesetpixel(resource image,int x,int y,int color) 7,imagettftext带字体的写入函数 imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 8,php验证码插入中文的方法 iconv("gb2312","utf-8","字符串"); //首先要将文字转换成utf-8格式 9,随机函数 1,rand([int min,int max]) //rand(1,4) 生成1-4的数 2, dechex(十进制数) //转换为十六进制

生成验证码的步骤: 生成随机数 -- 创建图片 -- 随机数写成图片 --保存在session中

输入验证码例子 gdchek.php

  1. /*
  2. * 生成图片验证码
  3. * and open the template in the editor.
  4. */
  5. session_start();
  6. for($i=0;$i$rand.=dechex(rand(1,15)); //生成4位数包含十六进制的随机数
  7. }
  8. $_SESSION[check_gd]=$rand;
  9. $img=imagecreatetruecolor(100,30); //创建图片
  10. $bg=imagecolorallocate($img,0,0,0); //第一次生成的是背景颜色
  11. $fc=imagecolorallocate($img,255,255,255); //生成的字体颜色
  12. //给图片画线
  13. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  14. imageline($img,rand(0,15),0,100,30,$te);
  15. }
  16. //给图片画点
  17. for($i=0;$i$te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  18. imagesetpixel($img,rand()%100,rand()%30,$te);
  19. }
  20. //首先要将文字转换成utf-8格式
  21. //$str=iconv("gb2312","utf-8","呵呵呵");
  22. //加入中文的验证
  23. //smkai.ttf是一个字体文件,为了在别人的电脑中也能起到字体作用,把文件放到项目的根目录,可以下载,还有本机C:\WINDOWS\Fonts中有
  24. imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
  25. //把字符串写在图片中
  26. //imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
  27. //输出图片
  28. header("Content-type:image/jpeg");
  29. imagejpeg($img);
  30. ?>
复制代码

login.php

  1. /*
  2. *
  3. *
  4. */
  5. session_start();
  6. if($_POST[sub]){
  7. //判断验证码是否相同
  8. if($_POST[gd_pic]==$_SESSION[check_gd]){
  9. echo "验证成功!";
  10. }else{
  11. echo "验证码错误";
  12. }
  13. }
  14. ?>
  15. 用户名:
  16. 密码:
  17. 验证码:php图片验证码代码多个实例
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn