SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding noise to captcha

    I have made a simple captcha class today, it's already functional but I'm thinking of some clever way to add some random geometrical shapes in order to create a noise.

    Should I just flood the captcha with some random lines?

    Also, I suppose the captcha text should be stored in the session, am I right?

    Here is what I have so far:

    PHP Code:
    <?php

    /*
     @desc Creates captcha verification image
     @author Richard Knop
    */
    class Captcha
    {
        private 
    $length;

        private 
    $string = array();
        
        private 
    $image;
        
        public function 
    generateString($length$base '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM')
        {
            
    $this->length $length;
            for (
    $i 0$max strlen($base) - 1$i $length$i++)
            {
                
    $this->string[] = $base[rand(0,$max)];  
            }
        }
        
        public function 
    draw($fontSize)
        {
            
    header("Content-type: image/png");
            
            
    $width $fontSize $this->length;
            
    $height $fontSize;
            
            
    $this->image imagecreatetruecolor($width$height);
            
            
    $r rand(0127);
            
    $g rand(0127);
            
    $b rand(0127);
            
    $bgColor imagecolorallocate($this->image$r$g$b);
            
    imagefill($this->image00$bgColor);
            
            
    $font 'arial.ttf';
            
    $partialWidth $width $this->length;
            for (
    $i 0$i $this->length$i++)
            {
                
    $angle rand(-4545);
                
    $bbox imagettfbbox($fontSize$angle$font$this->string[$i]);
                
    $w $bbox[2] - $bbox[0];
                
    $h $bbox[1] - $bbox[7];

                
    $x $i $partialWidth + ($partialWidth $w) / 2;
                
    $y $height - ($height $h) / 2;
                
    $color imagecolorallocate($this->imagerand(192255), rand(192255), rand(192255));
                
    imagettftext($this->image$fontSize$angle$x$y$color$font$this->string[$i]);
            }
            
            
    $lineColor imagecolorallocate($this->image255 $r255 $g255 $b);
            for (
    $increment $height 20$i $increment$i $height$i += $increment)
            {
                
    imageline($this->image0$i$width$i$lineColor);
            }
            
            
    imagepng($this->image);
            
    imagedestroy($this->image);
        }
        
        public function 
    getString()
        {
            return 
    implode(''$this->string);
        }
    }

    $captcha = new Captcha();
    $captcha->generateString(5);
    $captcha->draw(20);
    Just copy and paste the code to test it, if interested.
    Last edited by risoknop; Dec 28, 2008 at 18:42.

  2. #2
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Added some basic noise (random lines) but it's not exactly very brilliant... any ideas?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •