Location: PHPKode > scripts > DF_Captcha > class.captcha.php
<?php

    ob_start();
    session_start();
if(isset($_GET["b"]))
{
    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";
}
else
{
    $c = new DF_Captcha(372, 60, "fff");
    $c->noise_granular = 0;
    $c->noise_square = 0;
    $c->setLenCode(7);
    $c->drawImage();
    $_SESSION["code"] = $c->getCode();
}


class DF_Captcha
{
    private $image_resource;
    private $img_width;
    private $img_height;
    private $color_bg;
    public  $format = "png";
    public  $noise_square = 1;
    public  $noise_granular = 1;
    public  $text_font = "sans.ttf";
    public  $text_font_size;
    public  $text_color_mode = 1;
    public  $text_size_mode = 0.8;
    public  $text_rotation_mode = 10;
    private $text_length = 6;
    private $text_chars = "23456789abcdefghkmnpqrstuvwxyz";
    private $text_code = ""; 
    private $color_list = array();
    
    
    public function __construct($imgWidth = 300, $imgHeight = 300, $colorBg = "fff")
    {
        $this->img_width = (int)$imgWidth;
        $this->img_height = (int)$imgHeight;
        $this->image_resource = imagecreatetruecolor($this->img_width, $this->img_height);
    		list($r, $g, $b) = $this->html2rgb($colorBg);
        $this->color_bg = imagecolorallocate($this->image_resource, $r, $g, $b);
        imagefill($this->image_resource, 0, 0, $this->color_bg);
        $this->text_font_size = ((int)$this->img_height) * 0.6;
    }   
    public function __destruct()
    {
        imagedestroy($this->image_resource);
    }
    public function setLenCode($len)
    {
        if($len > 25){$len = 25;}
        $this->text_length = (int)$len;
    }
    public function getCode()
    {
        return $this->text_code;
    }
    public function drawImage()
    {
        $this->drawNoise();
        $this->writeCode();
        imageantialias($this->image_resource, true);
    		imageinterlace($this->image_resource, true);
        if($this->format == "png")
        {      
            header("Content-type: image/png");
            imagepng($this->image_resource);
        }
        else
        {
            header("Content-type: image/jpeg");
            imagejpeg($this->image_resource, "", 75);
        }
    }
    private function drawNoise()
    {
        if($this->noise_square > 0) 
        {
            imagesetthickness($this->image_resource, 1);
            for($i = 0; $i <= $this->img_width; $i += ($this->img_height/5)) 
            {
                imageline($this->image_resource, $i, 0, $i, $this->img_height, $this->color_list["black"]);
            }
            for($i = 0; $i <= $this->img_height; $i += ($this->img_height/5)) 
            {
                imageline($this->image_resource, 0, $i, $this->img_width, $i, $this->color_list["black"]);
            }
        }
        if($this->noise_granular > 0) 
        {
            for($i = 1; $i < ($this->img_width * $this->img_width/10); $i++)
            {
                $cor_x = mt_rand(1, $this->img_width);
                $cor_y = mt_rand(1, $this->img_height);
                imagesetpixel($this->image_resource, $cor_x, $cor_y, $this->color_list["black"]);
            }
        }
    }
    private function createListColor()
    {
        $this->color_list["white"] = imagecolorallocate($im, 255, 255, 255);
        $this->color_list["grey"] = imagecolorallocate($im, 238, 238, 238);
        $this->color_list["black"] = imagecolorallocate($im, 0, 0, 0);
        $this->color_list["red"] = imagecolorallocate($im, 255, 0, 0);
        list($r, $g, $b) = $this->html2rgb("0000ff");
        $this->color_list["blue"] = imagecolorallocate($this->image_resource, $r, $g, $b);
    }
    private function writeCode()
    {
        $pass = "";
        for($i = 0; $i < $this->text_length; $i++) 
        {
            $securityCode[$i] = substr($this->text_chars, mt_rand(0, strlen($this->text_chars) - 1), 1);
            $pass = $pass.$securityCode[$i];
            $rand = mt_rand(0, 1);
            if($i == 1){$rand = 1;}
            if($rand > 0) 
            {
                $pass2 = $pass2.$securityCode[$i]; 
                $select[$i] = $i;
            } 
            else
            { 
                $select[$i] = -1;
            }
        }
        $this->text_code = $pass;
        $x = $this->img_width/18;
        for($i = 0; $i < $this->text_length; $i++) 
        {
            $color = $this->color_list["black"];
            if($this->text_color_mode > 0){$color = imagecolorallocate($this->image_resource, mt_rand(0, 250), mt_rand(0, 250), mt_rand(0, 250));}
            $textbox = imagettfbbox($this->text_font_size, 0, $this->text_font, $securityCode[$i]) or exit("There is an error in imagettfbbox function!");
            $y = ($this->height - $textbox[5])/2;
            $w = abs($textbox[4] - $textbox[0]);
             
            $Size = mt_rand($this->text_font_size * $this->text_size_mode, $this->text_font_size);
            $Angle = mt_rand(-$this->text_rotation_mode, $this->text_rotation_mode);
            $y = $this->img_height - $this->img_height/4;
            $x = $x + ($w*1.1);
            if($subset < 1){ImageTtfText($this->image_resource, $Size, $Angle, $x-$w, $y, $color, $this->text_font, $securityCode[$i]);} 
            else
            {
                if($select[$i] > -1){imagettftext($this->image_resource, $Size, $Angle, $x-$w, $y, $this->color_list["red"], $this->text_font, $securityCode[$i]);}
                else{imagettftext($this->image_resource, $Size, $Angle, $x-$w, $y, $color, $this->text_font, $securityCode[$i]);}
            }
        }
    }
    private function html2rgb($color)
    {
        if($color[0] == "#")        {$color = substr($color, 1);}    
        if(strlen($color) == 6)     {list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]);}
        elseif(strlen($color) == 3) {list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);}
        else                        {return false;}
        $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);    
        return array($r, $g, $b);
    }
}
?>
Return current item: DF_Captcha