php验证码

面向过程手写验证码实例

 该程序采用面向过程手写验证码:使用本地图片自动缩放比例作为背景图,有些代码注释掉是为了方便查看另一种方案,虽然代码不精华,但是对于新手熟悉使用php做验证码

还是很有帮助的。

<?php

define(__DIR__, dirname(__FILE__));   //定义常量获取当前的程序路径

//使用随机数模拟验证码
$coderand = null;
for ($i = 0; $i < 4; $i++) {
    $coderand .= dechex(mt_rand(0, 15));
}

//第一:设定标头,告诉浏览器你要生成的MIME 类型
header("Content-type: image/png");


//第二:创建画布
//$codeimg = imagecreatefrompng(__DIR__ . '/images/codeback.png');  //使用背景图创建一个画布,以后的操作都将基于此画布区域
//$codeimg = imagecreatetruecolor(100, 40);  //创建一个画布
list($imgwidht, $imgheight) = getimagesize(__DIR__ . '/images/codeback.png');   //本地电脑上的一张图片作为背景图
$_imgwidht = $imgwidht * 0.4;   //画布原始宽的40%;
$_imgheight = $imgheight * 0.25; //画布原始高的30%;
//创建一个新画布
$codeimg = imagecreatetruecolor($_imgwidht, $_imgheight);

//将原画布复制到新创建的画布上
$_codeimg = imagecreatefrompng(__DIR__ . '/images/codeback.png');   

//将原图重新采样拷贝到新图上
imagecopyresampled($codeimg, $_codeimg, 0, 0, 0, 0, $_imgwidht, $_imgheight, $imgwidht, $imgheight);

//获取画布颜色
$red = imagecolorallocate($codeimg, 255, 0, 0);
$white = imagecolorallocate($codeimg, 255, 255, 255);
$green = imagecolorallocate($codeimg, 75, 222, 26);
$blue = imagecolorallocate($codeimg, 0, 111, 173);


//第三:填充画布背景颜色
//imagefill($codeimg, 0, 0, $blue);  

//第四:绘制线条 
imageline($codeimg, 60, 0, 30, 60, $white);
imageline($codeimg, 30, 0, 50, 50, $white);
imageline($codeimg, 0, 00, 30, 30, $white);

//填充文字
//imagestring($codeimg, 5, 10, 10, $coderand, $white);  

//转换验证码为 utf-8 格式
$codetext = iconv('gbk', 'utf-8', $coderand);

$font = 'C:WindowsFontssimhei.ttf';  //系统字体路径(此路径为我电脑的字体库路径)
//采用系统提供的字体
imagettftext($codeimg, 20, mt_rand(0, 16), 20, 30, $red, $font, $codetext);

//第五:输出创建的画布
imagepng($codeimg);

//第六:销毁画布
imagedestroy($codeimg);
imagedestroy($_codeimg);


?>

效果图:

 

将php验证码封装成函数(虽然代码不精华,但是对于新手熟悉使用php做验证码还是很有帮助的。)

<?php

/**
 * _codeimg()是验证码函数
 * @access public 
 * @param int $_width 表示验证码的长度
 * @param int $_height 表示验证码的高度
 * @param int $_rnd_code 表示验证码的位数
 * @param bool $_flag 表示验证码是否需要边框 
 * @return void 这个函数执行后产生一个验证码
 */

//参数可不填,不填则使用默认
function _codeimg($_width = 75, $_height = 25, $_rnd_code = 4, $_flag = false) {
    $_nmsg = true;
    //创建随机码
    for ($i = 0; $i < $_rnd_code; $i++) {
        $_nmsg .= dechex(mt_rand(0, 15));
    }

    //保存在session
    $_SESSION['code'] = $_nmsg;

    //创建一张图像
    $_img = imagecreatetruecolor($_width, $_height);

    //白色
    $_white = imagecolorallocate($_img, 255, 255, 255);

    //填充
    imagefill($_img, 0, 0, $_white);

    if ($_flag) {
        //黑色,边框
        $_black = imagecolorallocate($_img, 0, 0, 0);
        imagerectangle($_img, 0, 0, $_width - 1, $_height - 1, $_black);
    }

    //随即画出6个线条
    for ($i = 0; $i < 6; $i++) {
        $_rnd_color = imagecolorallocate($_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
        imageline($_img, mt_rand(0, $_width), mt_rand(0, $_height), mt_rand(0, $_width), mt_rand(0, $_height), $_rnd_color);
    }

    //随即雪花
    for ($i = 0; $i < 100; $i++) {
        $_rnd_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
        imagestring($_img, 1, mt_rand(1, $_width), mt_rand(1, $_height), '*', $_rnd_color);
    }

    //输出验证码
    for ($i = 0; $i < strlen($_SESSION['code']); $i++) {
        $_rnd_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
        imagestring($_img, 5, $i * $_width / $_rnd_code + mt_rand(1, 10), mt_rand(1, $_height / 2), $_SESSION['code'][$i], $_rnd_color);
    }

    //输出图像
    header('Content-Type: image/png');
    imagepng($_img);

    //销毁
    imagedestroy($_img);
}


//调用
_codeimg();

?>

效果图:

 

原文地址:https://www.cnblogs.com/andyhxl/p/6544240.html