Added content to the PHP development verification code tutorial
In the previous section we have already created a canvas
Now we change the assigned color to white, the code is as follows:
<?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);
?>Next we need to add content For example, if you add 4 random numbers
, then we have to use a loop to operate
for($i=0;$i<4;$i++){
imagecolorallocate($image,rand(1,120),rand(1,120),rand(1,120));
// The setting content is a random number from 0 to 9
$fontcontent = rand( 0,9);
through i*100/4)+rand(5,10);
$y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent, $fontcolor);
}
In this way, we fill the canvas with numbers 0-9. The complete code is as follows:
<?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);
?>















The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ 