PHP:现有图片验证码类

文章来源:http://www.cnblogs.com/hello-tl/p/7593022.html

<?php
class TL_Captcha_img{
    private $image; //验证码图片
    private $captch_code; //验证码信息
    public function __construct($table){
        session_start();
        if(is_array($table)){
            $index = rand(0,count($table)-1);
            //获取随机的value
            $table1 = array_keys($table);
            $value = $table1[$index];
            $_SESSION['authcode'] = $this->captch_code;
            //获取随机的键值
            $key = array_flip($table);
            $key =  $key[$value];
            $this->image = dirname(__FILE__).'/'.$key.'.png';
        }
    }
    public function __destruct() {
        $this->image = file_get_contents($this->image);
        header('content-type:image/png');
        echo $this->image;
        imagedestroy($this->image);
    }
}
// $table = array(
//      'sql' => 'sql',
//      'png' => 'png',
//      'jpg' => 'jpg',
//      'gif' => 'gif',
//      'css' => 'css',
//      'html' => 'html',
//      'js' => 'js',
//      'php' => 'php',
//      'txt' => 'txt',
//      'word' => 'word',
//      'excal' => 'excal',
//      'img' => 'img',
// );
// $Captcha_img = new Captcha_img($table);

文章来源:http://www.cnblogs.com/hello-tl/p/7593022.html