search
  • Sign In
  • Sign Up
Password reset successful

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

Interference elements (line) in PHP development verification code tutorial

In the previous section, we have used to add interference elements (dots)

Next we will add interference elements (lines)

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

?>

In the above code, we have added interference elements dots

Let’s take a look at adding interference elements, line

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

//First we set the line in the loop The number of lines is 5

//Set the color of the line, the color of the text is between 1 and 120, then the color of our line is lighter than the color of the text, set 80, 220

$linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));

//Then set the position of the line

imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);

}

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

    for($i=0;$i<5;$i++){
        $linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
        //设置线的颜色 让线的颜色变得更浅一点,80到220
        imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
    }


    //输出图像
    header("content-type:image/png");
    imagepng($image);

    //销毁资源
    imagedestroy($img);

?>

With the above code, we have created a simple verification code

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); } for($i=0;$i<5;$i++){ $linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220)); //设置线的颜色 让线的颜色变得更浅一点,80到220 imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor); } //输出图像 header("content-type:image/png"); imagepng($image); //销毁资源 imagedestroy($img); ?>
Reset Code
Automatic operation
submit
Preview Clear