您的位置:首页 > 编程语言 > PHP开发

php 验证码 生成 字体变化 可高低 倾斜 大小

2006-01-11 14:03 543 查看
<?php

/*
@author sanshi
QQ:35047205
Email:sanshi0815@tom.com
MSN:sanshi0815@tom.com
*/

$string=md5(rand(1,1000));
$string=substr($string,0,4);
$len=strlen($string);
$bordercolor = "#000000";
$bgcolor = "#ffffff";//背景色
$height = 30;
$width = 60;

$image = imageCreate($width, $height);
//画边框
$bordercolor = getcolor($image,$bordercolor);
imagefilledrectangle($image,0,0,$width+1,$height+1,$bordercolor);
//画背景
$back = getcolor($image,$bgcolor);
imagefilledrectangle($image,1,1,$width-2,$height-2,$back);
//画干扰元素
$num = 200; //干扰元素的个数
setnoise($image,$width,$height,$num);
//字体大小
$size = ceil($width / $len);
//写字

for($i=0;$i<$len;$i++)
{
$TempText=substr($string,$i,1);
//字体颜色
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
//取得随机大小
$randsize =rand($size-$size/6,$size+$size/6);
//取得字体
$font = rand(1,1).".ttf";
//取得角度
$randAngle = rand(-15,15);
//取得每次的位置
$x=8+($width-$width/8)/$len*$i;
//echo "<br>";
//取得每次的高度
$y=rand($height-3,$height-10);
imagettftext($image,$randsize,$randAngle,$x,$y,$textColor,$font,$TempText);
}
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
//取得色彩
function getcolor($image,$color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
//画干扰点
function setnoise($image,$width,$height,$noisenum)
{
for ($i=0; $i<$noisenum; $i++){
//分配颜色
$randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
//画点
imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
?>



图片看不到,生成的.png图象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: