PHP开发验证码教程之干扰元素(点)
在上节内容中,我们已经完成了,在画布中显示4个随机数
代码如下:
<?php
//第一步 创建一个画布
$image = imagecreatetruecolor(100, 30); //创建一个宽为100高为30的黑色图像
$bgcolor = imagecolorallocate($image, 255, 255, 255); //为图像分配颜色
imagefill($image,0,0,$bgcolor); //给黑色的背景图像分配白色
//第二步,在这个图像上实现数字
for($i=0;$i<4;$i++){
$fontsize = 6; //字体大小
$fontcolor = imagecolorallocate($image,rand(1,120),rand(1,120),rand(1,120));
//设置字体的颜色 颜色我们给一个随机的值,画布为白色,0到120之间,颜色为深色
$fontcontent = rand(0,9); //设置内容是一个随机数
//现在需要把这个随机数添加到画布上去
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//输出图像
header("content-type:image/png");
imagepng($image);
//销毁资源
imagedestroy($img);
?>下面我们将在以上代码的基础上进行加入干扰元素
同样我们使用for循环加入点 加入200个点
for($i=0;$i<200;$i++){
//首先我们设置点的颜色
$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//设置点放在图像的什么位置上
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
下面我们来看一下完整代码:
<?php
//第一步 创建一个画布
$image = imagecreatetruecolor(100, 30); //创建一个宽为100高为30的黑色图像
$bgcolor = imagecolorallocate($image, 255, 255, 255); //为图像分配颜色
imagefill($image,0,0,$bgcolor); //给黑色的背景图像分配白色
//第二步,在这个图像上实现数字
for($i=0;$i<4;$i++){
$fontsize = 6; //字体大小
$fontcolor = imagecolorallocate($image,rand(1,120),rand(1,120),rand(1,120));
//设置字体的颜色 颜色我们给一个随机的值,画布为白色,0到120之间,颜色为深色
$fontcontent = rand(0,9); //设置内容是一个随机数
//现在需要把这个随机数添加到画布上去
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//设置干扰元素 点
for($i=0;$i<200;$i++){
//首先我们设置点的颜色
$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//设置点放在图像的什么位置上
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
//输出图像
header("content-type:image/png");
imagepng($image);
//销毁资源
imagedestroy($img);
?>
neue Datei
<?php
//第一步 创建一个画布
$image = imagecreatetruecolor(100, 30); //创建一个宽为100高为30的黑色图像
$bgcolor = imagecolorallocate($image, 255, 255, 255); //为图像分配颜色
imagefill($image,0,0,$bgcolor); //给黑色的背景图像分配白色
//第二步,在这个图像上实现数字
for($i=0;$i<4;$i++){
$fontsize = 6; //字体大小
$fontcolor = imagecolorallocate($image,rand(1,120),rand(1,120),rand(1,120));
//设置字体的颜色 颜色我们给一个随机的值,画布为白色,0到120之间,颜色为深色
$fontcontent = rand(0,9); //设置内容是一个随机数
//现在需要把这个随机数添加到画布上去
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//设置干扰元素 点
for($i=0;$i<200;$i++){
//首先我们设置点的颜色
$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//设置点放在图像的什么位置上
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
//输出图像
header("content-type:image/png");
imagepng($image);
//销毁资源
imagedestroy($img);
?>
Vorschau
Clear
- Kursempfehlungen
- Kursunterlagen herunterladen
Die Kursunterlagen stehen derzeit nicht zum Download zur Verfügung. Die Mitarbeiter organisieren es derzeit. Bitte schenken Sie diesem Kurs in Zukunft mehr Aufmerksamkeit
Auch Studierende, die diesen Kurs gesehen haben, lernen
Lassen Sie uns kurz über die Gründung eines Unternehmens in PHP sprechen
Kurze Einführung in die Web-Frontend-Entwicklung
Umfangreiche, praktische Tianlongbabu-Entwicklung eines Mini-Version-MVC-Frameworks, das die Enzyklopädie-Website mit peinlichen Dingen imitiert
Erste Schritte mit der praktischen PHP-Entwicklung: Schnelle PHP-Erstellung [Small Business Forum]
Anmeldebestätigung und klassisches Message Board
Wissenssammlung über Computernetzwerke
Schnellstart-Node.JS-Vollversion
Der Frontend-Kurs, der Sie am besten versteht: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Schreiben Sie Ihr eigenes PHP-MVC-Framework (40 Kapitel ausführlich/große Details/Muss gelesen werden, damit Neulinge vorankommen)
















