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

这篇博客介绍了一种使用PHP生成验证码的方法,包括设置字体大小、颜色、角度和干扰元素,使得验证码具有字体变化和倾斜效果,增强安全性。生成的验证码以.png格式输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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图象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值