Mój pierwszy własny kod captcha, napisany kilka miesięcy temu, przy okazji pisania katalogu stron ( nie skończnego niestety ).
Nie powiem, żeby wyglądało to rewelacyjnie, ale to też m.in. z winy mojego graficznego bezguścia i nieumiejętności dobrania nawet kilku kolorów do siebie. Niemniej sam kod wykorzystywałem i był na tyle funkcjonalny, że użyteczny :) Nie zmienia to faktu, że pewnie trochę rzeczy dałoby się poprawić…
class Token
{
protected $text = ”;
protected $text_color;
protected $bg_color;
protected $font_name = ‘4mini.ttf’;
protected $font_size = 20;
protected $type = ‘image/png’;
protected $height;
protected $width;
protected $id_image;
protected $filename;
public function __construct( $width = 150, $height = 45 )
{
$this -> width = $width;
$this -> height = $height;
$this -> id_image = imagecreatetruecolor( $this -> width, $this -> height );
$this -> setColor();
$this -> setBgColor();
}
public function setColor( $r = 233, $g = 14, $b = 91 )
{
$this -> text_color = imagecolorallocate( $this -> id_image, $r, $g, $b );
}
public function setBgColor( $r = 128, $g = 128, $b = 128 )
{
$this -> bg_color = imagecolorallocate( $this -> id_image, $r, $g, $b );
imagefilledrectangle( $this -> id_image, 0, 0, $this -> width - 1, $this -> height - 1, $this -> bg_color );
}
public function setFont( $font_name, $font_size )
{
$this -> font_name = $font_name;
$this -> font_size = $font_size;
}
public function setText( $text = ” )
{
$this -> text = $text;
}
public function setRandomText( $count = 5 )
{
$chars = array( ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘0′, ‘1′, ‘2′, ‘3′, ‘4′ ,’5′ ,’6′ ,’7′ ,’8′ ,’9′ );
$el_number = count( $chars );
for ( $i = 0; $i text .= $chars[rand(0, $el_number - 1)];
}
}
public function getText()
{
return $this -> text;
}
private function pseudoDistortion()
{
$el_number = strlen( $this -> text );
$black = imagecolorallocate( $this -> id_image, 0, 0, 0 );
for ( $i = $el_number - 1; $i >= 0; –$i )
{
$angle = ( $i % 2 == 0 ) ? 10 : -10 ;
imagettftext( $this -> id_image, $this -> font_size, 180 - $angle, 10 + 25 * $i, 25 , $black, $this -> font_name, $this -> text[$i] );
}
}
public function getToken( $distortion = false )
{
//header(”Content-type: “.$this -> type );
$el_number = strlen( $this -> text );
if ( $distortion ) $this -> pseudoDistortion();
for ( $i = 0; $i id_image, $this -> font_size, $angle, 10 + 25 * $i, 30 , $this -> text_color, $this -> font_name, $this -> text[$i] );
}
$this -> file_name = ‘opt/image’.rand(10000,99999).’.png’;
imagepng( $this -> id_image, $this -> file_name );
return $this -> file_name;
}
public function __toString()
{
$this -> getToken();
}
public function compare( $compare_text )
{
// $compare_text is string, $this -> text is array of char
$count = count( $this -> text );
if ( strlen( $compare_text ) == $count )
{
for ( $i = 0; $i text[$i] ) return false;
}
return true;
}
return false;
}
public function __destruct()
{
imagedestroy( $this -> id_image );
//unlink( $this -> file_name );
}
}