日期:2011-09-14  浏览次数:20532 次

 

看到PHP100视频34讲写的php验证码类,支持自定义图片大小、字符个数、字符类型、字符大小、图片类型、汉字验证码、干扰线等,注释详细

字体文件是微软雅黑,和验证码文件同目录,文件名msyh.ttf,可以从C:\windows\fonts目录找到,复制到同目录即可

<?php

/**

 * @version        Id:  imgcode.php 2012-11-30   k

 * @package        imgcode.php

 * @Purview           default

 * @link           http://www.3oom.com

 */

    $img = new img_code();

    $img->show();

 

class img_code{

    private $img_type = 'png';//输出图片类型 png,gif,jpg

    private $line      = TRUE;//是否增加干扰线

    private $text      = '2';//验证码类型:0数字,1字母,2汉字

    private $text_size= 20;//字体大小

    private $length      = 4;//字符串长度

    private $width      = 120;//图片宽度,像素

    private $height      = 40;//图片高度,像素

    private $font_file= 'msyh.ttf';//字体文件

    public  $img      = '';

   

    /*

    * 创建图片

    */

    private function img_create(){

        $this->img = imagecreate($this->width, $this->height);

        imagecolorallocate($this->img, 255,255,255);

 

    }

    /*

    * 显示图片

    */

    public function show(){

       

        @session_start();

        $this->img_create();

        if($this->line){

        $this->img_line();

        }

        $this->img_text();

        $this->img_header();

       

        imagedestroy($im);

        exit();

    }

 

    /*

    * 生成字符串

    */

    private function img_text(){

        $rand_string = '';

        $im = &$this->img;

        $fontColor[]  = imagecolorallocate($im, 0x15, 0x15, 0x15);

        $fontColor[]  = imagecolorallocate($im, 0x95, 0x1e, 0x04);

        $fontColor[]  = imagecolorallocate($im, 0x93, 0x14, 0xa9);

    &n