Hi, I'm trying to get a form image verification program to work (everytime) but occasionally it happens that i get garbled text in the image generated through GD. I was wondering if this was a GD issue or MCRYPT or worse my coding. I'm attaching the code below...
// functions.php
function get_number($size){
$characters = "BCDFGHJKLMNRSTVXY2457";
$output = "";
for ($i=0; $i<$size; $i++)
$output .= $characters{rand()%(strlen($characters))};
return $output;
}
function encrypt ($input) {
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$crypttext = mcrypt_encrypt(MCRYPT_3DES, SECRET_CODE, $input, MCRYPT_MODE_ECB, $iv);
$crypttext = urlencode($crypttext);
return $crypttext;
}
function decrypt ($input) {
$input = urldecode ($input);
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = rtrim(mcrypt_decrypt(MCRYPT_3DES, SECRET_CODE, $input, MCRYPT_MODE_ECB, $iv));
return $decrypttext;
}
//image.php
function create_image ($input) {
$img = imagecreatefrompng("images/bg.png");
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$i=1;
while ($i<=strlen($input)) {
Imagettftext($img, 20,rand(-30,30),$i*25,33,$black,'courbd.ttf',$input{$i-1});
$i++;
}
imagepng($img);
imagedestroy($img);
}
create_image (decrypt($_GET['q']));
Here's how it works...
I'm calling the image.php program with a
?q=encrypt(get_number(5))
where create_image decrypts the input text and generates the desired image.
Sometimes (once in say 10-15 tries) there is garbled text on the image generated by GD.
Any help would be nice.