发新话题
打印

php 中文验证码

php 中文验证码

<?php
/*
    fangzhen 2015.10.4 $
*/

if(!defined('IN_UCHOME')) {
    exit('Access Denied');
}
/*定义头文件为图片*/
header("Content-type: image/PNG");
/*调用生成验证码函数*/
getCode(4, 150, 50);
/**
 * 定义生成验证码图片函数
 * @param int $num 生成验证码个数
 * @param int $w 图片宽
 * @param int $h 图片高
 */
function getCode($num, $w, $h) {
    $w = $w;
    $h = $h;
    /* 字体文件:注意路径
    * 如果没有字体文件,是无法输入显示图片的
    * */
    $fontface = dirname(__FILE__) . '/font/msyhbd.ttf';   
    $str = "日历扩展包含了简化不同日历格式间的转换的函数脚本运行的服务器上获取日期和时间并进行格式化获得关于目录及其内容的信息对错误进行处理和记录集成开发环境是一种集成了软件开发过程中所需主要工具的集成开发环境其功能包括但不仅限于代码高亮代码补全调试构建版本控制等程序开发快运行快技术本身学习快嵌入于因为可以被嵌入于语言它相对于其他语言编辑简单软件开发我实用性强更适合初学者";
    /**
    * 字符编码转换 UTF-8 == GBK
    * 如果不转换,图片将无法输出显示
    */
    $str = iconv('utf-8','gbk',$str);
    /*生成验证码*/
    $code="";
    for($i=0;$i<$num;$i++){
    $Xi=mt_rand(0,strlen($str)/2);
    if($Xi%2) $Xi+=1;
    $code.=substr($str,$Xi,2);
    }
    /*创建图片*/
    $im=imagecreatetruecolor($w,$h);
    $bkcolor=imagecolorallocate($im,250,250,250);
    imagefill($im,0,0,$bkcolor);
    /*创建干扰线等*/
    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,$w),mt_rand(-10,$h),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,$w),mt_rand(0,$h),$fontcolor);
    }
    /*将验证码写入到图片中*/
    for($i=0;$i<4;$i++){
    $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    $codex=iconv("GB2312","UTF-8",substr($code,$i*2,2));
    imagettftext($im,mt_rand(14,18),mt_rand(-60,60),30*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
    }
    /*输入图片*/
   
    imagepng($im);
    imagedestroy($im);
}
?>

TOP

发新话题