验证码将画布划分为需要的几份

2016-12-04  本文已影响0人  孤岛渔夫
    <?php 
    // 验证码
    // 
    // 1.创建真彩图
        $img = imagecreatetruecolor(160,50);
    // 2.分配颜色
        $back = imagecolorallocate($img, mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
        $font_color = imagecolorallocate($img, mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
    // 3.填充颜色
        imagefill($img, 0,0, $back);
    // 4.画图
        $str = implode(array_rand(array_flip(range('0','9')),4));

        // 将画布平均分成4份
        $w = 160/4;

        for($i=0; $i<4; $i++){
            $x = $i*40;     

            imagettftext($img, 15, mt_rand(0,90) ,$x+20,mt_rand(20,40),  $font_color, 'Arvo-Regular.ttf', $str[$i]);
        }


    // 5.保存或者输出图片
        header('content-type:image/jpeg');
        imagejpeg($img);
    // 6.销毁资源
        imagedestroy($img);


 ?>
上一篇下一篇

猜你喜欢

热点阅读