search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Interference elements (points) in PHP development verification code tutorial

In the previous section, we have finished displaying 4 random numbers in the canvas.

The 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);

?>

Next we will add interference based on the above code. Element

Similarly we use a for loop to add points to add 200 points

for($i=0;$i<200;$i++){

/ /First we set the color of the point

$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//Set where the point is placed in the image Up
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);

}

Let’s take a look at the complete code:

<?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);

?>


new file
<?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); ?>
Reset Code
Automatic operation
submit
Preview Clear