首页 > php代码 > php 中英文验证码程序

php 中英文验证码程序

<?php
$code = "";
for ($i = 0; $i < 4; $i++) {
    $Xi = mt_rand(0, strlen($str) / 2);
    if ($Xi % 2) $Xi+= 1;
    $code.= substr($str, $Xi, 2);
}
$_SESSION['code'] = $code; //赋值给SESSION
$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(20, 24) , mt_rand(-60, 60) , 40 * $i + 20, mt_rand(30, 35) , $fontcolor, $fontface, $codex);
}
imagepng($im);


文章地址:http://www.phprm.com/code/2fcc372dca3eb1e1c2933575f815b70e.html

转载随意^^请带上本文地址!

标签:none

发表留言