PHP生成验证码---使用画布

文本编码格式:

<?php
header('content-type:text/html;charset=utf-8');

图片编码格式

header(‘content-type:image/png’)
header ( ‘Content-Type: image/gif’ );
header ( ‘Content-Type: image/jpeg’ );

完整代码

<?php
//案例:生成验证码
header('content-type:image/png');
//编辑一个字符串,去掉不容易识别的i,l,o,I,L,O
$str = "abcdefghjkmnpqrstuvwyzABCDEFGHJKMNPQRSTUVWXYZ0123456789";

//画布
$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);

//颜色
$color = imagecolorallocate($img,0xcc,0xcc,0xcc);

//填充
imagefill($img,0,0,$color);

//画噪点
for($i=0;$i<100;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x = rand(0,$width);
   $y = rand(0,$height);
   imagesetpixel($img,$x,$y,$color);
}

//画噪线
for($i=0;$i<30;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x1 = rand(0,$width);
   $y1 = rand(0,$height);
   $x2 = rand(0,$width);
   $y2 = rand(0,$height);
   imageline($img,$x1,$y1,$x2,$y2,$color);
}

//画文字
$len = strlen($str);
$font = 'simsunb.ttf';
for($i=0;$i<4;$i++){
	$color = imagecolorallocate($img,255,0,0);
    $index = rand(0,$len);
    $chr = substr($str,$index,1);
    $x = 20 + $i * 40;
    $y = 80;
    imagettftext($img,50,rand(-70,70),$x,$y,$color,$font,$chr);
}

//输出画布
imagepng($img);

//销毁画布
imagedestroy($img);

输出结果

在这里插入图片描述

分析

imagecreatetruecolor
imagecreatetruecolor — 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int $width , int $height );
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像
第一步:

//画布
$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);

注:宽度,高度可以在()里设置定值。
也可以在声明函数前设置变量,在()中使用

imagecolorallocate
imagecolorallocate — 为一幅图像分配颜色
说明
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。 red , green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。 imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。
第二步:

设置颜色
$color = imagecolorallocate($img,0xcc,0xcc,0xcc);

imagefill
imagefill — 区域填充
说明
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image 图像的坐标 x , y (图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
第三步:

//填充
imagefill($img,0,0,$color);

imagesetpixel
imagesetpixel — 画一个单一像素 (噪点)
说明
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 图像中用 color 颜色在 x , y 坐标(图像左上角为 0,0)上画一个点。
第四步:

//画噪点
for($i=0;$i<100;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x = rand(0,$width);
   $y = rand(0,$height);
   imagesetpixel($img,$x,$y,$color);
}

注:在for循环里设置颜色,xy位置。
用rand随机函数设置噪点的位置和随机颜色

imageline
imageline — 画一条线段
说明
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color 颜色在图像 image 中从坐标 x1 , y1 到 x2 , y2 (图像左上角为 0, 0)画一条线段
第五步:

//画噪线
for($i=0;$i<30;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x1 = rand(0,$width);
   $y1 = rand(0,$height);
   $x2 = rand(0,$width);
   $y2 = rand(0,$height);
   imageline($img,$x1,$y1,$x2,$y2,$color);
}

注:在for循环里设置颜色,随机位置
使用rand函数设置线的颜色,和位置
x1,x2,y1,y2设置线段的起始点

第六步:
imagettftext
imagettftext — 用 TrueType 字体向图像写入文本
说明
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
使用 TrueType 字体将 指定的 text 写入图像。

//画文字
$len = strlen($str);
$font = 'simsunb.ttf';
for($i=0;$i<4;$i++){
	$color = imagecolorallocate($img,255,0,0);
    $index = rand(0,$len);
    $chr = substr($str,$index,1);
    $x = 20 + $i * 40;
    $y = 80;
    imagettftext($img,50,rand(-70,70),$x,$y,$color,$font,$chr);
}

注:用strlen函数输出$str的长度
使用substr函数返回相应的字节数
引用i设置移动文字的位置
simsunb.ttf:在C盘:\Windows文件\Fonts下,可以将其复制到当前文件所在的文件夹

imagepng
imagepng — 以 PNG 格式将图像输出到浏览器或文件
说明
bool imagepng ( resource $image [, string $filename ] )
imagepng() 将 GD 图像流( image )以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
第七步:

//输出画布
imagepng($img);

imagedestroy
imagedestroy — 销毁一图像
说明
bool imagedestroy ( resource $image )
imagedestroy() 释放与 image 关联的内存。 image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor() 。
第八步:

//销毁画布
imagedestroy($img);
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值