QY千亿国际

  • 当前位置: 首页 / 建站百科 / 常见问题
  • php 图像验证码的创建

    时间:2019-12-05 浏览量: 收藏

    php 图像验证码的创建(1)

    php创建图像验证码源码:

    <?php 
    /**
     * 图像验证码
     * 1、生成验证码字符串
     * 2、创建画布大小,填充颜色
     * 3、创建杂色到画布
     * 4、创建干扰线到画布
     * 5、创建字符串到画布上
     * 6、输出图像
     */
    
    //0-9 a-z A-Z
    
    $arr = array_merge(range(0,9),range('a','z'),range('A','Z'));
    
    //打乱数组
    shuffle($arr);
    
    //从数组中从下标0开始,取出4个元素
    $arrlen = array_splice($arr,0,4);
    
    //将数组转成字符串
    $text = implode($arrlen);
    
    
    //创建画布
    $width = 120;
    $height = 40;
    $image = imagecreatetruecolor($width, $height);
    $reccolor = imagecolorallocate($image,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
    imagefilledrectangle($image,0,0,$width,$height,$reccolor);
    
    //添加杂色到画布
    for ($i=1;$i<=450;$i++)
    {
    	$pixcolor = imagecolorallocate($image,mt_rand(60,160),mt_rand(60,160),mt_rand(60,160));
    	imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height), $pixcolor);
    }
    
    //将字符串写到画布
    $ttfpath = dirname(__FILE__).'/arial.ttf';
    $ttfcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    imagettftext($image,32,0,5,35, $ttfcolor, $ttfpath, $text);
    
    //创建干扰线到画布
    for ($i=1;$i<=15;$i++)
    {
    	$linecolor = imagecolorallocate($image,mt_rand(30,150),mt_rand(30,150),mt_rand(30,150));
    	imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$linecolor);
    }
    
    
    //输出图像
    header("Content-Type:image/png");
    imagepng($image);
    imagedestroy($image);


    顶一下
    (0)
    0.00%
    踩一下
    0
    0.00%

    QY千亿国际网络

    您已成功复制微信号,去微信立即添加好友!

    打开微信

    Wechart

    点击下方复制按钮,复制微信号!

    jzm866081

    //lqelsmm.com
    厦门QY千亿国际网络科技有限公司

    点击这里给我发消息
    嘿!有什么能帮到您的吗?
    QY千亿国际 QY千亿国际 QY千亿国际 QY千亿国际 qy千亿·体育 qy千亿·体育 qy千亿·体育 qy千亿·体育