![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
<?php
//image.php
//产生随机字符串
function randStr($len=6,$format='CHAR') {
switch($format) {
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
case 'NUMBER':
$chars='0123456789'; break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
$string="";
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
//画图
function image($code,$entropyLevel=15)
{
$type = 'gif'; // 格式
$width= 300; // 宽
$height= 80; // 高
//所用字体
$fontFileArray[0]='..\font\b.ttf';
$fontFileArray[1]='..\font\b.ttf';
$fontFileArray[2]='..\font\c.ttf';
$fontFileArray[3]='..\font\d.ttf';
$fontFileArray[4]='..\font\e.ttf';
$fontFileArray[5]='..\font\g.ttf';
header("Content-type: image/".$type);
$im = @imagecreate($width,$height);
ImageColorAllocate($im,255,255,255); //背景
// 画字符
$codeArray = str_split($code);
$strX=12;
for ($i=0;$i
$strColor= ImageColorAllocate($im,rand(0,222),rand(0,222),rand(0,222));//字体颜色
$strY = rand(60,70); // y座标
$angle = rand(-30,30); // 倾斜角度
$fontSize = rand(35,40); // 字体大小
$arraySize = count($fontFileArray);
$fontFileArrayKey=rand(0,$arraySize-1);
$fontStyle=$fontFileArray[$fontFileArrayKey];
ImageTTFText($im, $fontSize, $angle, $strX, $strY, $strColor, $fontStyle, $codeArray[$i]);
$strX =$strX+rand(42,50); // x座标++
}
// 画干扰线条
for ($i=0;$i<$entropyLevel;$i++)
{
$lineColor= ImageColorAllocate($im,rand(0,255),rand(0,255),rand(0,255));//线条颜色
imageline($im, rand (0,200), rand (0,60), rand (0,$width), rand (0,$height), $lineColor);
//imagearc($im, rand (0,200), rand(0,60), rand (0,$width), rand(0,$height), rand(-360,360), rand(-360,360), $lineColor);
}
$ImageTypeFunction='Image'.$type;
$ImageTypeFunction($im);
@ImageDestroy($im);
}
function setCodeSessionName($codeName = 'safeCode'){
$code = randStr();
session_start();
session_register($codeName);
$_SESSION[$codeName]=$code;
image($code);
}
setCodeSessionName();
?>
需下载一些文字
//所用字体
$fontFileArray[0]='..\font\b.ttf';
$fontFileArray[1]='..\font\b.ttf';
$fontFileArray[2]='..\font\c.ttf';
$fontFileArray[3]='..\font\d.ttf';
$fontFileArray[4]='..\font\e.ttf';
$fontFileArray[5]='..\font\g.ttf';
引用代码方式


