当前位置:Gxlcms > PHP教程 > php图片验证码代码多个实例

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

时间:2021-07-01 10:21:17 帮助过:19人阅读

  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<18;$i++){
  15. //获取随机淡色
  16. $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
  17. //画线
  18. imageline($im,2,$i,47,$i,$line_color);
  19. }

  20. //设置字体大小

  21. $font_size=12;

  22. //设置印上去的文字

  23. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  24. $Str[1] = "abcdefghijklmnopqrstuvwxyz";
  25. $Str[2] = "01234567891234567890123456";

  26. //获取第1个随机文字

  27. $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
  28. $imstr[0]["x"] = rand(2,5);
  29. $imstr[0]["y"] = rand(1,4);

  30. //获取第2个随机文字

  31. $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
  32. $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
  33. $imstr[1]["y"] = rand(1,3);

  34. //获取第3个随机文字

  35. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
  36. $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
  37. $imstr[2]["y"] = rand(1,4);

  38. //获取第4个随机文字

  39. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  40. $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
  41. $imstr[3]["y"] = rand(1,3);

  42. //写入随机字串

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

  49. //显示图片

  50. imagepng($im);
  51. //销毁图片
  52. imagedestroy($im);
  53. ?>

例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<$codelen;$i++) {
  17. $authstr .= $charset[mt_rand(0,$_len)];
  18. }
  19. session_start();
  20. $_SESSION['scode']=strtolower($authstr);//全部转为小写,主要是为了不区分大小写
  21. //随机画点,已经改为划星星了
  22. for ($i=0;$i<$imgwidth;$i++){
  23. $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  24. imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
  25. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
  26. }
  27. //随机画线,线条数量=字符数量(随便)
  28. for($i=0;$i<$codelen;$i++)
  29. {
  30. $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  31. imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
  32. }
  33. $_x=intval($imgwidth/$codelen); //计算字符距离
  34. $_y=intval($imgheight*0.7); //字符显示在图片70%的位置
  35. for($i=0;$i $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
  36. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
  37. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
  38. imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);
  39. }
  40. //生成图像
  41. header("content-type:image/PNG");
  42. imagePNG($im);
  43. 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<4;$i++){
  7. $rand.=dechex(rand(1,15)); //生成4位数包含十六进制的随机数
  8. }
  9. $_SESSION[check_gd]=$rand;
  10. $img=imagecreatetruecolor(100,30); //创建图片
  11. $bg=imagecolorallocate($img,0,0,0); //第一次生成的是背景颜色
  12. $fc=imagecolorallocate($img,255,255,255); //生成的字体颜色
  13. //给图片画线
  14. for($i=0;$i<3;$i++){
  15. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  16. imageline($img,rand(0,15),0,100,30,$te);
  17. }
  18. //给图片画点
  19. for($i=0;$i<200;$i++){
  20. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  21. imagesetpixel($img,rand()%100,rand()%30,$te);
  22. }
  23. //首先要将文字转换成utf-8格式
  24. //$str=iconv("gb2312","utf-8","呵呵呵");
  25. //加入中文的验证
  26. //smkai.ttf是一个字体文件,为了在别人的电脑中也能起到字体作用,把文件放到项目的根目录,可以下载,还有本机C:\WINDOWS\Fonts中有
  27. imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
  28. //把字符串写在图片中
  29. //imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
  30. //输出图片
  31. header("Content-type:image/jpeg");
  32. imagejpeg($img);
  33. ?>

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. ?>

人气教程排行