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图象
/*
@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图象
相关文章推荐
- php生成复杂验证码(倾斜,正弦干扰线,黏贴,旋转)
- PHP 验证码可设置字体(大小),可换背景图片,可扭曲角度
- PHP生成带有干扰线的验证码,干扰点、字符倾斜
- php验证码函数 使用imagestring() imagefttext()设置字体大小
- PHP imagechar() 图形验证码 字体太小问题
- php生成验证码
- 随机生成字体大小
- 基于GD库的php验证码类(支持中英文字体、背景、干扰点线、扭曲…….)
- 如何让Android TextView(包括子类)的字体大小随着字数的增加而变化,以显示全部内容
- PHP生成不同颜色、不同大小的tag标签函数
- APP字体大小,不随系统的字体大小变化而变化的方法
- php生成验证码-原理版
- 【php】随机验证码的生成
- php生成随机验证码
- 如何用php生成扭曲及旋转的验证码图片
- PHP生成带有雪花背景的验证码
- PHP GD 生成图片验证码+session获取储存验证码
- PHP生成自适应大小的缩略图类及使用方法分享
- PHP初学笔记-验证码生成
- 转载 支持中文字母数字、自定义字体php验证码程序