PHP学习之验证码(1)简易验证码

验证码(1)

  1. str_shuffle() 函数随机打乱字符串中的所有字符。
  2. array_merge — 合并一个或多个数组
  3. join — 别名 implode()
  4. imagechar — 水平地画一个字符,imagechar( resource $image, int $font, int $x, int $y, string $c, int $color)
  5. imagecharup — 垂直地画一个字符imagecharup( resource $image, int $font, int $x, int $y, string $c, int $color)
  6. imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text)
    imagettftext — 用 TrueType 字体向图像写入文本,TrueType是由美国苹果公司和微软公司共同开发的一种电脑轮廓字体(曲线描边字)类型标准。这种类型字体文件的扩展名是.ttf,类型代码是tfil。
  7. //添加像素当做干扰元素
    imagesetpixel($image, $x, $y, $color)画一个单一像素。
  8. //绘制线段当做干扰元素
    imageline($image, $x1, $y1, $x2, $y2, $color)
  9. //绘制圆弧
    imagearc( resource $image, int $cx, int $cy, int $w, int $h, int $s, int $e, int $color)
    imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
  10. 以下为一个简单验证码实例
    <?php
    //简易验证码的制作
    //创建一个画布
    $width=200;
    $height=40;
    $image=imagecreatetruecolor($width, $height);
    //为画布填充颜色
    $white=imagecolorallocate($image, 255, 255, 255);
    imagefilledrectangle($image, 0, 0, $width, $height, $white);
    $string=join('',array_merge(range(0,9),range('a','z'),range('A','Z')));
    //获取字体宽度
    $textWidth=imagefontwidth(28);
    //获取字体高度
    $textHeight=imagefontheight(28);
    // echo $textWidth.'w'.$textHeight;exit();
    $length=4;
    for($i=0; $i < $length; $i++) {
    $size=mt_rand(20,28);
    $angle=mt_rand(-15,15);
    // $x=20+40$i;
    // $y=30;
    $x=($width/$length)
    $i+$textWidth;
    $y=mt_rand($height/2,$height-$textHeight);
    $color=imagecolorallocate($image, mt_rand(0,255), mt_rand(0,255),mt_rand(0,255));
    $fontFile='fonts/2.ttc';
    $text=str_shuffle($string)[0];
    imagettftext($image, $size, $angle, $x, $y, $color, $fontFile, $text);
    }
    //添加干扰元素
    //添加像素
    for($i=0;$i<50;$i++){
    imagesetpixel($image,mt_rand(0,200),mt_rand(0,40),$color);
    }
    //绘制线段当做干扰元素
    for ($i=0;$i<4;$i++){
    imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$color);
    }
    //绘制圆弧
    for ($i=0;$i<5;$i++){
    imagearc($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width/2),mt_rand(0,$height/2),mt_rand(0,360),mt_rand(0,360),$color);
    }
    //设定向浏览器输出图片类型
    header('content-type:image/png');
    //输出图像
    imagepng($image);
    //销毁资源
    imagedestroy($image);
    ?>

转载于:https://www.cnblogs.com/h-s-l/p/10883231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值