• 技术文章 >php教程 >php手册

    php 复杂生成验证码图片

    2016-06-13 09:48:12原创356
    function image3($length=4,$type='png',$width=180,$height=60,$fontface='fpnf.ttf',$verifyName='verify') {
    $code = $this->rand_string($length,4);
    $width = ($length*25)>$width?$length*25:$width;
    $authCode = new Zend_Session_Namespace('Auth_Code');
    $authCode->imagecode = $randval;

    $im=imagecreatetruecolor($width,$height);
    $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
    $bkcolor=imagecolorallocate($im,250,250,250);
    imagefill($im,0,0,$bkcolor);
    @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
    // 干扰
    for($i=0;$i<15;$i++){
    $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
    }
    for($i=0;$i<255;$i++){
    $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
    }
    if(!is_file($fontface)) {
    $fontface = dirname(__FILE__)."//m.sbmmt.com/m/".$fontface;
    }
    for($i=0;$i<$length;$i++){
    $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
    $codex= substr($code,$i,1);
    imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
    }
    $this->output($im,$type);
    }

    function output($im,$type='png')
    {
    header("Content-type: image/".$type);
    $ImageFun='Image'.$type;
    $ImageFun($im);
    imagedestroy($im);
    }

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    上一篇:php上传图片加水印(图片水印,文字水印) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• php操作mysqli(示例代码)• php+ajax导入大数据时产生的问题处理• mysql 数据备份类代码• PHP用代码实现文件下载
    1/1

    PHP中文网