温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中怎么生成图形验证码

发布时间:2021-06-30 16:06:12 阅读:190 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

今天就跟大家聊聊有关php中怎么生成图形验证码,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

我们先来检查一下自己的php是不是打开了gd库。

<?php
if(extension_loaded('gd')) {
  echo '你可以使用gd<br>';
  foreach(gd_info() as $cate=>$value)
    echo "$cate$value<br>";
}else
  echo '你没有安装gd扩展';
?>


如果有返回信息就正确可以常用使用了
例1

<?php
/**
 * vCode(m,n,x,y) m个数字  显示大小为n   边宽x   边高y
 * 自己改写记录session $code
 */
session_start(); 
vCode(415); //4个数字,显示大小为15
function vCode($num 4$size 20$width 0$height 0{
 !$width && $width $num $size 4 / 5 + 5;
 !$height && $height $size 10; 
 // 去掉了 0 1 O l 等
 $str "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
 $code '';
 for ($i 0$i $num$i++) {
  $code .= $str[mt_rand(0strlen($str)-1)];
 } 
 // 画图像
 $im imagecreatetruecolor($width$height); 
 // 定义要用到的颜色
 $back_color imagecolorallocate($im235236237);
 $boer_color imagecolorallocate($im118151199);
 $text_color imagecolorallocate($immt_rand(0200), mt_rand(0120), mt_rand(0120)); 
 // 画背景
 imagefilledrectangle($im00$width$height$back_color); 
 // 画边框
 imagerectangle($im00$width-1$height-1$boer_color); 
 // 画干扰线
 for($i 0;$i 5;$i++) {
  $font_color imagecolorallocate($immt_rand(0255), mt_rand(0255), mt_rand(0255));
  imagearc($immt_rand(- $width$width), mt_rand(- $height$height), mt_rand(30$width 2), mt_rand(20$height 2), mt_rand(0360), mt_rand(0360), $font_color);
 } 
 // 画干扰点
 for($i 0;$i 50;$i++) {
  $font_color imagecolorallocate($immt_rand(0255), mt_rand(0255), mt_rand(0255));
  imagesetpixel($immt_rand(0$width), mt_rand(0$height), $font_color);
 } 
 // 画验证码
 @imagefttext($im$size 05$size 3$text_color'c:\WINDOWS\Fonts\simsun.ttc'$code);
 $_SESSION["VerifyCode"]=$code; 
 header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
 header("Content-type: image/png;charset=gb2312");
 imagepng($im);
 imagedestroy($im);
}
?>


例2
使用PHP,结合session和GD库扩展开发的一个生成验证码的例子(w3c推荐),可以很方便的用于项目中。而且样式美观

//首先开启session
session_start();
//定义前台显示验证码长&宽
$image_width 120;
$image_height 40;
$characters_on_image 6;
$font './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters '23456789bcdfghjkmnpqrstvwxyz';
$random_dots 10;
$random_lines 30;
$captcha_text_color="0x142864";
$captcha_noice_color "0x142864";
//定义要生成验证码的字符串
$code '';
$i 0;
while ($i $characters_on_image) {
$code .= substr($possible_lettersmt_rand(0strlen($possible_letters)-1), 1);
$i++;
}
$font_size $image_height 0.75;
$image = @imagecreate($image_width$image_height);
/* setting the background, text and noise colours here */
$background_color imagecolorallocate($image255255255);
$arr_text_color hexrgb($captcha_text_color);
$text_color imagecolorallocate($image$arr_text_color['red'],
        $arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color hexrgb($captcha_noice_color);
$image_noise_color imagecolorallocate($image$arr_noice_color['red'],
        $arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for$i=0$i<$random_dots$i++ ) {
imagefilledellipse($imagemt_rand(0,$image_width),
 mt_rand(0,$image_height), 23$image_noise_color);
}
/* generating lines randomly in background of image */
for$i=0$i<$random_lines$i++ ) {
imageline($imagemt_rand(0,$image_width), mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox imagettfbbox($font_size0$font$code);
$x = ($image_width $textbox[4])/2;
$y = ($image_height $textbox[5])/2;
imagettftext($image$font_size0$x$y$text_color$font $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//设置session,做验证
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
  $int hexdec($hexstr);
  return array("red" => 0xFF & ($int >> 0x10),
               "green" => 0xFF & ($int >> 0x8),
               "blue" => 0xFF & $int);
}

看完上述内容,你们对php中怎么生成图形验证码有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×