הקוד הבא שברצוני לשתף אותכם בו הוא מחלקה שיצרתי בשפת PHP ליצירת CAPTCHA . מכיוון שידעתי שלא לכולכם יש סבלנות להתעסק עם ביצוע הגדרות אז המחלקה מגיעה עם ערכים כברירת מחדל . הערך היחידי שאתם חייבים לשנות הוא שם קובץ הפונט . מכיוון שלא רציתי להפר זכויות יוצרים אז לא השתמשתי בפונט אריאל שמגיע עם חלונות, אלא עם פונט שניתן לשימוש חופשי ברישיון GPL . פונט כזה ניתן למצוא ב
פה . למי שכן מעוניין לבצע שינוי הגדרות הדבר אפשרי ואף בניתי מתודות (פונק' במחלקה ) שנועדו למטרה זו .
קוד המחלקה :
<?php
header('Content-Type: image/png');
session_start();
class Captcha{
/**
* the image object
* @access private
* @var Object;
*/
private $Im;
/**
* image properties
* @access private
* @var Array
*/
private $Image = array('Color','Height' => 36,'Width' => 122);
/**
* image border properties
* @access private
* @var Array
*/
private $Border = array('Color','Size' => 1);
/**
* image string properties
* @access private
* @var Array
*/
private $String = array('Angel' => 0,'Color','Length' => 5,'Text' => '',
'X_Pos' => 33,'Y_Pos' => 24);
/**
* string font properties
* @access private
* @var Array
*/
private $Font = array('Size' => 16,'File' => "3.ttf");
/**
* image,border and text rgb colors
* @access private
* @var Array
*/
private $RGB = array('Image' => '255,255,255','Border' => '0,0,0','Text' => '0,0,0');
/**
* set the image Height
* @access public
* @param int $ImageHeight
* @return void
*/
public function set_Image_Height($ImageHeight){
$this->Image['Height'] = $ImageHeight;
}
/**
* set the image Height
* @access public
* @param int $ImageWidth
* @return void
*/
public function set_Image_Width($ImageWidth){
$this->Image['Width'] = $ImageWidth;
}
/**
* set the Border Size
* @access public
* @param int $BorderSize
* @return void
*/
public function set_Border_Size($BorderSize){
$this->Border['Size'] = $BorderSize;
}
/**
* set the Border Size
* @access public
* @param int tAngle;//The angle in degrees,
* //with 0 degrees being left-to-right reading text
* @return void
*/
public function set_String_Angel($String_Angel){
$this->String['Angle'] = $String_Angel;
}
/**
* set the string length
* @access public
* @param int $String_Length
* @return void
*/
public function set_String_Length($String_Length){
$this->String['Length'] = $String_Length;
}
/**
* set the String X_Pos
* @access public
* @param int $String_X_Pos
* @return void
*/
public function set_String_X_Pos($String_X_Pos){
$this->String['X_Pos'] = $String_X_Pos;
}
/**
* set the String Y_Pos
* @access public
* @param int $String_Y_Pos
* @return void
*/
public function set_String_Y_Pos($String_Y_Pos){
$this->String['Y_Pos'] = $String_Y_Pos;
}
/**
* set the Font Size
* @access public
* @param int $FontSize
* @return void
*/
public function set_Font_Size($FontSize){
$this->Font['Size'] = $FontSize;
}
/**
* set the Font File
* @access public
* @param int $FontFile
* @return void
*/
public function set_Font_File($FontFile){
/**
* @var Object ; \\text Font File;
*/
$this->Font['File'] = $FontFile;
}
/**
* set the Image Color
* @access public
* @param int $ImageRgb
* @return void
*/
public function set_Image_Color($ImageRgb){
$this->RGB['Image'] = $ImageRgb;
}
/**
* set the Border Color
* @access public
* @param int $BorderRgb
* @return void
*/
public function set_Border_Color($BorderRgb){
$this->RGB['Border'] = $BorderRgb;
}
/**
* set the Text Color
* @access public
* @param int $TextRgb
* @return void
*/
public function set_Text_Color($TextRgb){
$this->RGB['Border'] = $TextRgb;
}
/**
* Make Border to the Image
* @access private
* @return void
*/
private function MakeBorder(){
$this->Im = imagecreate($this->Image['Width'], $this->Image['Height']);
$RGB = explode("," , $this->RGB['Border']);
$this->Border['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
imagerectangle($this->Im, 0, 0, $this->Image['Width'],
$this->Image['Height'], $this->Border['Color'] );
}
/**
* make the image
* @access private
* @return void
*/
private function MakeImage(){
$this->MakeBorder();
$RGB = explode(",", $this->RGB['Image']);
$this->Image['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
imagefilledrectangle($this->Im,$this->Border['Size'] , $this->Border['Size'] ,
$this->Image['Width'] -($this->Border['Size'] +1),
$this->Image['Height']-($this->Border['Size'] +1),
$this->Image['Color']);
}
/**
* make the string for the image
* @access private
* @return void
*/
private function MakeString(){
$this->MakeImage();
for ($i=0;$i<$this->String['Length'];$i++){
/**
* 224-250 is the range of hebrew letters
* @var string
*/
$this->String['Text'] .= iconv("WINDOWS-1255","UTF-8",chr(rand(224,250)));
}
}
/**
* starts session which contains the image string encripted
* @access private
* @return void
*/
private function MakeSession(){
$this->MakeString();
$_SESSION['Captcha'] = hash('sha256',$this->String['Text']);
}
/**
* create the captcha
* @access public
* @return void
*/
public function MakeCaptcha(){
$this->MakeSession();
//change the text encoding;
$this->String['Text'] = iconv( "UTF-8", "WINDOWS-1255",$this->String['Text']);
$this->String['Text'] = iconv( "WINDOWS-1255", "UTF-8", hebrev($this->String['Text']));
$RGB = explode(",", $this->RGB['Text']);
$this->String['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
//create text in the image from ttf file
imagettftext($this->Im, $this->Font['Size'],
$this->String['Angel'], $this->String['X_Pos'],
$this->String['Y_Pos'], $this->String['Color'],
$this->Font['File'], $this->String['Text']);
imagepng($this->Im);
imagedestroy($this->Im);
}
}
?>
כדי להשתמש במחלקה יש להגדיר ככה :
<?php
$cap = new Captcha;
$cap->MakeCaptcha();
?>
ניתן לשנות את הגדרות הבסיס באופן הבא :
<?php
$cap = new Captcha;
$cap->set_Border_Color('200,100,100');
// here we define the font file name
$cap->set_Font_File("font_file_name.ttf");
$cap->set_Border_Size(3);
$cap->MakeCaptcha();
?>
כדי לבצע את ההשוואה נעשה כך :
<?php
if (hash('sha256',$_REQUEST['User_CaptCha']) === $_SESSION['Captcha'] ){
//here we process the data if the auth is o.k.
}
?>