|
发表于 2008-3-9 14:22:13
|
显示全部楼层
我好久没用CI, Captcha库都不记得是否使用过, 所以下面的代码调试不出来,是正常的;-) 但修正应该不是什麽难事.
我不喜欢生成图片然后再删除这样的东西,自己参照别人写了这个,保存到app目录的libraries目录里,这里假设文件名是CaptchaImage.php
PHP复制代码 class CaptchaImage {
var $length;
var $string;
var $ttfFont;
var $charWidth;
//var $background_colour;
//var $text_colour;
//var $colours = array();
//var $shadow_height;
//var $shadow_dark;
/**
* PHP4 constructor.
*
* @access public
* @param string $length captcha string length
* @param string $string captcha string
* @return void
*/
function __construct ($length = 7, $string = null){
$this->length = $length;
$this->ttfFont = './App/fonts/captcha.ttf';
$this->charWidth = 12;
$this->font_size = 9;
if (!isset($string)){
$this->mystring = $this->randomString($length);
} else {
$this->mystring = $string;
}
//$this->background_colour = 'FFFFFF';
//$this->text_colour = '000000';
//$this->colours = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
//$this->shadow_height = 16;
//$this->shadow_dark = true;
}
function setFont ($ttfpath, $ttfsize = 8){
if(file_exists($ttfpath)){
$this->ttfFont = $ttfpath;
$this->font_size = $ttfsize;
} else {
$this->ttfFont = 2;
//echo 'font can\'t be found';
}
}
function randomString ($length){
//alphanumeric array widthout ambiguous chars
$alphanumericArr = array ('A','B','C','D','E','F','G','H','L','M','P','R','2','3','8','9');
shuffle($alphanumericArr);
$alphanumericArr = array_slice($alphanumericArr, 0, $length-1);
$string = join("",$alphanumericArr);
return $string;
}
function draw (){
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type: image/png");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
$length = $this->length * $this->charWidth + 20;
$height = 25;
$image = imagecreate($length, $height);
$background = imagecolorallocate($image, 00, 0, 0);
$textcolor = imagecolorallocate($image, 255,255,255);
$linecolor = imagecolorallocate($image, 0, 0, 255);
$border_color = ImageColorAllocate($image, 153, 102, 102);
$this->DrawLines($image, $length, $height);
imagettftext($image, $this->font_size, 0, 4, 18,
$textcolor,
$this->ttfFont,
$this->mystring);
// -----------------------------------
// Create the border
// -----------------------------------
imagerectangle($image, 0, 0, $length-1, $height-1, $border_color);
// -----------------------------------
// Generate the image
// -----------------------------------
imagepng($image);
//echo $this->ttfFont;
exit();
}
function getString (){
return $this->mystring;
}
function _colourHex ($img, $HexColorString){
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function _colourHexshadow ($img, $HexColorString, $mork){
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if($mork){
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
} else {
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function DrawLines ($oImage, $length, $height) {
for ($i = 0; $i < 48; $i++) {
// allocate colour
if (true) {
$iLineColour = imagecolorallocate($oImage, rand(90, 100), rand(100, 140), rand(120, 130));
} else {
$iRandColour = rand(100, 250);
$iLineColour = imagecolorallocate($oImage, $iRandColour, $iRandColour, $iRandColour);
}
//imagecolorallocatealpha($oImage, rand(90, 100), rand(100, 140), rand(120, 130), rand(100, 120));
// draw line
imageline($oImage, rand(0, $length), rand(0, 0), rand(0, $length), rand($height, $height), $iLineColour);
}
}
} 复制代码
控制器
PHP复制代码 <?php if (! defined ('BASEPATH')) die('No direct script access allowed.');
class Captcha extends Controller {
function __construct () {
parent ::Controller();
}
function index () {
$this->load->view("v_captcha");
}
function draw (){
$this->load->library('captchaimage');
ob_start();
//session_start();
$this->session->set_flashdata('captcha', $this->captchaimage->getString());
$this->captchaimage->setFont('./App/fonts/captcha.ttf',25); //这个字体自己google一个,下载到你本地目录!!!
//echo file_exists($ttfpath)
$this->captchaimage->draw();
//echo time(); //needed to force image expiration
//session_write_close();
ob_flush();
// return;
}
function valid ()
{
if($this->input->post("security_code")==$this->session->get("captcha")){
echo "good, u key the right words";
}else{
echo "bad man, try again.";
}
}
}
?> 复制代码
视图v_captcha.php
HTML复制代码 <form method="post" action="captcha/valid">
<input type="text" name="security_code" maxlength="6"/>
<img src="./draw" title="Security Code" alt="Code" />
<button type="submit" />
</form> 复制代码 |
|