May 14, 2008 – 7:04 pm
Yesterday I took one hour to study PHP GD lib.I read this book's chapter which is about PHP Advanced Pic Technology.The picture which can descripe this book is below.
After reading it,I felt it is easy to operate pic using PHP GD.When I home,I suddenly wanted to write a PHP Class which includes Identifying Code Pic's function.Now I published this class.Thank you for pointing out my issue and leaving your comment.
/*
*@date 20080514
*@author hluan
*@package Identifying.Code.Pic.Class
*@attention Before using this class,you should use session_start();
*/
//session_start();
class icode
{
function __construct(){
header('Content-Type:image/png');
}
function __destruct(){
imagedestroy($png);
}
/**************************************************
*@len The length of the identifying code.
*@type 1:only number identifying code;
* 2:only letter identifying code;
* 0:number and letter identifying code.
*@attention We store identifying code in $_SESSION['icode'].
**************************************************/
function getcode($len,$type){
switch ($type){
case 1;
$str = "1234567890";
break;
case 2;
$str = "abcdefghijklmnopqrstuvwxyz";
break;
case 0;
$str = "1234567890abcdefghijklmnopqrstuvwxyz";
break;
}
$result = "";
$length = strlen($str) - 1;
$num = 0;
for ($i=0;$i<$len;$i++){
$num = rand(0,$length);
$a = $str[$num];
$result = $result.$a;
}
/*Before using it,you should use session_start()*/
$_SESSION['icode'] = $result;//Store in session.
//echo "session:".$_SESSION['icode'];
//die();
$png = imagecreate(60,30);
$white = imagecolorallocate($png,255,255,255);//background
$red = imagecolorallocate($png,255,0,0);
$blue ...
Posted in PHP GD, PHP | No Comments »