
Originally Posted by
earl-grey
They don't have to, but I'd recommend to use a file per class.
Hello again, i just rewrite some parts of the Class, just tell if it is good enough or how to improve it 
Here is the first library of the class:
ctCaptcha.lib.php
PHP Code:
<?
session_start();
require_once('ctCaptchaImg.lib.php');
class ctCaptcha
{
var $captchaImg; //IMAGEN
var $iAncho; //ANCHO DEL CUADRO
var $iAlto; //ALTO DEL CUADRO
var $cadena;
var $captcha_id;
//CONSTRUCTOR DE LA CLASE
function ctCaptcha($ancho=200,$alto=50,$captcha_id='session_captcha')
{
//DEFINIMOS ANCHO Y ALTO
$this -> iAncho = $ancho;
$this -> iAlto = $alto;
$this -> captcha_id = $captcha_id;
//INICIALIZAMOS NUESTRA IMAGEN
//$this -> captchaImg = imagecreate($this -> iAncho, $this -> iAlto);
//RELLENAMOS NUESTRO CAPTCHA
//$this -> setBox();
}
function Create()
{
$this -> captchaImg = new captchaImg($this -> iAncho, $this -> iAlto,$this -> captcha_id);
//echo "<img src=\"{$this -> captchaImg}\" id=\"imgCaptcha\">";
}
function Refresh()
{
$_SESSION[$this -> captcha_id] = "";
$this -> Create();
}
}
?>
then the class which create the image...
ctCaptchaImg.lib.php
PHP Code:
<?
class captchaImg
{
var $captchaImg;
var $iAncho; //ANCHO DEL CUADRO
var $iAlto; //ALTO DEL CUADRO
var $captcha_id;
var $cadena;
function captchaImg($ancho, $alto, $captcha_id){
$this -> iAncho = $ancho;
$this -> iAlto = $alto;
$this -> captcha_id = $captcha_id;
$this -> captchaImg = imagecreate($this -> iAncho, $this -> iAlto);
$this -> setBox();
$this -> show();
}
function setBox()
{
$background_color = imagecolorallocate($this -> captchaImg, 255, 255, 255);
$black_color = imagecolorallocate($this -> captchaImg, 0, 0, 0);
$border = imagerectangle ( $this -> captchaImg, 0, 0, $this -> iAncho-1, $this -> iAlto-1, $black_color );
$this -> cadena = $_SESSION[$this -> captcha_id] ? $_SESSION[$this -> captcha_id] : $this -> getStr();
$txt_size = 20;
$txt_font = "../lib/ARLRDBD.TTF";
for ($i=0;$i<strlen($this -> cadena);$i++)
{
$txt_angle = 10 * $i + rand(-50,50);
$xx = (40 * $i) + 20;
$yy = 30;
$color = imagecolorallocate($this -> captchaImg, rand(0,190), rand(0,190), rand(0,190));
imagettftext ( $this -> captchaImg, $txt_size, $txt_angle, $xx, $yy, $color, $txt_font, $this -> cadena[$i] );
}
}
function getStr($limit=5)
{
$haystack = "0123456789";
//$haystack .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$cadena = "";
for ($i=0;$i<$limit;$i++)
{
$cadena .= $haystack[ceil(rand(0,strlen($haystack)-1))];
}
$_SESSION[$this -> captcha_id] = $cadena;
return $cadena;
}
function show()
{
header ("Content-type: image/png");
header ("Pragma: no-cache");
imagepng ( $this -> captchaImg );
}
}
?>
and the example that you can see here working:
http://www.codeteam.net/ctclasses/Captcha.php
PHP Code:
<script type="text/javascript">
function refreshCaptcha(id)
{
obj = document.getElementById(id);
obj.src = 'bin/ctCaptcha.bin.php?reload=true&id=' + Math.random()*100;
}
</script>
<?
//require ("lib/ctCaptcha.lib.php");
echo "Testing Captcha<br>";
echo "<img src=\"bin/ctCaptcha.bin.php\" id=\"imgCaptcha\"><br />";
echo "<a href=\"javascript:refreshCaptcha('imgCaptcha')\">Refresh</a>";
?>
and this is the image bin... (the one that is called for the img tag)
PHP Code:
<?php
include ("../lib/ctCaptcha.lib.php");
$myCaptcha = new ctCaptcha();
if (empty($_GET['reload']))
{
$myCaptcha -> Create();
}else{
$myCaptcha -> Refresh();
//header("location: ".$_SERVER['HTTP_REFERER']);
}
?>
Bookmarks