Captcha Helper for codeigniter

From Code Trash
Jump to: navigation, search

Captcha helper php file

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function captcha($t,$hex=NULL,$fh=NULL)
{
//session_start(); 
//$text = rand(10000,99999); 
 
	$R=120; $G=83; $B=54;
	if($hex)
	{
		$hex = str_split($hex,2);
		$R = hexdec($hex[0]);
		$G = hexdec($hex[1]);
		$B = hexdec($hex[2]);
	}
 
	$FR = 255; $FG = 255; $FB = 255;
 
	if($fh)
	{
		$fh = str_split($fh,2);
		$FR = hexdec($fh[0]);
		$FG = hexdec($fh[1]);
		$FB = hexdec($fh[2]);
	}
 
    $code = '';
	$charset = 'A2B5C4D2E6F3G8H47J5K96M2N7P8Q8R9ST3UV7WX4YZ';
	$len=5;
    for($i = 1, $cslen = strlen($charset); $i <= $len; ++$i) {
      $code .=  strtolower($charset{rand(0, $cslen - 1)} );
    }
$text = $code;
 
$t->session->set_userdata('spamcode',$text);
 
$height = 20; 
$width = 65; 
 
$image_p = imagecreate($width, $height); 
$black = imagecolorallocate($image_p, $R,$G,$B); 
$white = imagecolorallocate($image_p, $FR,$FG,$FB); 
$font_size = 14; 
 
imagestring($image_p, $font_size, 7, 1, $text, $white);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Content-Type: image/jpeg");
imagejpeg($image_p, null, 80); 
} ?>


Function in the controller

function captcha($color=NULL,$fcolor)
{
	captcha($this,$color,$fcolor);
}


HTML Code - for the image

<img src='http://localhost/site/controller/function/backgroundhexcolor/foregroundhexcolor/' />
<!-- for example -->
<img src='http://localhost/site/contactus/captcha/CECECE/303030/' />
 
<!-- you can send the last two hex values or you can skip them. -->
<!-- If skipped the function will take a default value which is already specified -->