php生成gif图的验证码

php生成gif图的验证码

  • 作者:geticsen
  • 时间:2019-07-29
  • 140人已阅读
简介 php生成gif图的验证码

1使用了php的库

下载地址:

/php-gif

使用方法官网上有

说一下关于image的问题,你的图片大小要设置成大小合适的分辨率宽高我用的是200*100

下面是生成gif图的代码增加了字体(你可以在自己的Windows/fonts文件夹下面复制)还有文字的随机颜色文字的随机波动位置


 function createCheckCode()
{
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        // Output as a GIF image
        header ('Content-type:image/gif');
        // Initialize a new GIFGenerator object
        $gif = new GIFGenerator();
        // Create a multidimensional array with all the image frames
        $num=[];
        $ans=0;
        for($i=0;$i<5;$i++){
            $num[$i]=rand(0,5);
            $ans+=$num[$i];
        }
        $font=[
            0=>"AxureHandwriting-Bold.otf",
            1=>"AxureHandwriting-Italic.otf",
            2=>"Lato-Light.ttf",
            3=>"AllCaps.ttf",
            4=>"Dani.ttf",
            5=>"daniel.ttf",
            6=>"KidsFirstPrintFont.ttf",
            7=>"VIVACS.TTF",
            8=>"P22Royalist.ttf"
        ];
        $color=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
        $imageFrames = array(
            'repeat' => true,
            'frames' => array(
                array(
                    'image' => './images/temp.jpg',
                    'text' => array(
                        array(
                            'text' => $num[0].'+'.$num[1].'+'.$num[2],
                            'fonts' => './fonts/'.$font[rand(0,count( $font)-1)],
                            'fonts-size' => 30,
                            'angle' => 0,
                            'fonts-color' => '#'.$color[rand(0,15)].$color[rand(0,15)].$color[rand(0,15)],
                            'x-position' =>50+rand(-50,50),
                            'y-position' =>50+rand(-10,10)
                        )
                    ),
                    'delay' => 100+rand(-50,50)
                ),
                array(
                    'image' => './images/temp.jpg',
                    'text' => array(
                        array(
                            'text' => '+'.$num[3].'+'.$num[4].'=? ',
                            'fonts' => './fonts/'.$font[rand(0,count( $font)-1)],
                            'fonts-size' =>30,
                            'angle' => 0,
                            'fonts-color' => '#'.$color[rand(0,15)].$color[rand(0,15)].$color[rand(0,15)],
                            'x-position' => 50+rand(-50,50),
                            'y-position' => 50+rand(-10,10)
                        )
                    ),
                    'delay' => 100+rand(-50,50)
                )
            )
        );
        echo $gif->generate($imageFrames);
        Session::set('checkCodeAnswer',$ans);
}


文章评论

Top