PHP开发验证码教程之添加内容
在上节内容中我们已经制作好了一个画布
下面我们把分配的颜色改成白色,代码如下:
<?php
//第一步 创建一个画布
$image = imagecreatetruecolor(100, 30); //创建一个宽为100高为30的黑色图像
$bgcolor = imagecolorallocate($image, 255, 255, 255); //为图像分配颜色
imagefill($image,0,0,$bgcolor); //给黑色的背景图像分配白色
//输出图像
header("content-type:image/png");
imagepng($image);
//销毁资源
imagedestroy($img);
?>接下来我们要添加内容了,例如添加4个随机数
那么我们就要使用循环来操作了
for($i=0;$i<4;$i++){
//首先我们先设置下字体的大小
$fontsize = 6 ;
//然后我们设置一下文字的颜色,
$fontcolor = imagecolorallocate($image,rand(1,120),rand(1,120),rand(1,120));
// 设置内容是一个0-9的随机数
$fontcontent = rand(0,9);
//接下来我们要把文字填充到画布上,但是填充到什么位置呢,所以我们要设置坐标
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
这样我们就在画布上填充了0-9的数字了,完整代码如下:
<?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);
?>
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);
}
//输出图像
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)
















