1 - 下载 Swoole Loader 请下载 WINNT 系统 PHP-7.4.33 版本 非线程安全 的 swoole_loader 扩展,点击直达下载页面 2 - 安装 Swoole Loader 将刚才下载的 swoole_loader 扩展文件( swoole_loader.dll )上传到当前 PHP 的扩展安装目录中: ext......
PHP生成验证码:[5]php验证码
php生成验证码,php验证码,php怎样生成验证码?
工具/原料
- 这个验证码较实用,大家可以应用到项目中。
方法/步骤
1. 代码:
?php
/*设置文件头为图片输出*/
Header("Content-type: image/JPEG");
/*调用生成验证码函数*/
$checkcode = make_rand(4);
/**
* 生成验证码字符
* @param int $length 验证码字符长度
* @return string
*/
function make_rand($length="32"){
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$result="";
for($i=0;$i$length;$i ){
$num[$i]=rand(0,25);
$result.=$str[$num[$i]];
}
return $result;
}
2. 代码:
/*调用输出验证码图片函数*/
getAuthImage($checkcode, 160, 40);
/**
* 生成验证码图片
* @param string $text 验证码字符
*/
function getAuthImage($text, $w, $y) {
/*设置图片的宽度和高度*/
$im_x = $w;
$im_y = $y;
/*创建图片*/
$im = imagecreatetruecolor($im_x,$im_y);
$text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$tmpC0=mt_rand(100,255);
$tmpC1=mt_rand(100,255);
$tmpC2=mt_rand(100,255);
$buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
imagefill($im, 16, 13, $buttum_c);
3. 代码:
/*字体文件*/
$font = 't1.ttf';
for ($i=0;$istrlen($text);$i )
{
$tmp =substr($text,$i,1);
$array = array(-1,1);
$p = array_rand($array);
$an = $array[$p]*mt_rand(1,10);//角度
$size = 28;
imagettftext($im, $size, $an, 15 $i*$size, 35, $text_c, $font, $tmp);
}
/*将字符写入文件中*/
$distortion_im = imagecreatetruecolor ($im_x, $im_y);
imagefill($distortion_im, 16, 13, $buttum_c);
for ( $i=0; $i$im_x; $i ) {
for ( $j=0; $j$im_y; $j ) {
$rgb = imagecolorat($im, $i , $j);
if( (int)($i 20 sin($j/$im_y*2*M_PI)*10) = imagesx($distortion_im)&& (int)($i 20 sin($j/$im_y*2*M_PI)*10) >=0 ) {
imagesetpixel ($distortion_im, (int)($i 10 sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
}
}
}
4. 代码:
/*干扰元素点的数量*/
$count = 160;
/*创建干扰元素点*/
for($i=0; $i$count; $i ){
$randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
}
/*创建干扰线条*/
$rand = mt_rand(5,30);
$rand1 = mt_rand(15,25);
$rand2 = mt_rand(5,10);
for ($yy=$rand; $yy= $rand 2; $yy ){
for ($px=-80;$px=80;$px=$px 0.1)
{
$x=$px/$rand1;
if ($x!=0)
{
$y=sin($x);
}
$py=$y*$rand2;
imagesetpixel($distortion_im, $px 80, $py $yy, $text_c);
}
}
5. 代码:
/*以PNG格式将图像输出到浏览器*/
ImagePNG($distortion_im);
/*销毁图像*/
ImageDestroy($distortion_im);
ImageDestroy($im);
}
6.最终输入结果如图所示:
################################
## ##
## 纯属凑字 ##
## ##
################################
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
标签: PHP
相关文章