Commit cdb9128c authored by Arnab Nandi's avatar Arnab Nandi

TTF now works properly, you'll have to supply your own TTF files to test.

parent a766c24e
......@@ -41,33 +41,46 @@ function captcha_menu($may_cache) {
function captcha_settings() {
//check for GD
if (!function_exists(imagecreate))
if (!function_exists(imagecreate))
form_set_error('No GD', t('Image library not available. Captcha needs the GD library extension to be installed. Please install GD.'));
else {
$fonts_path = variable_get("captcha_fonts_path", "");
//check for TTF support
if (!function_exists(imagettftext) || !function_exists(imagettfbox))
if (!function_exists(imagettftext))
form_set_error('No TTF support', t('Your image library does not seem to have TrueType font support. Captcha will work, but will use the default inbuilt font.'));
else {
//check for valid font path
if ($fonts_path!="" && !is_dir($fonts_path))
form_set_error('Invalid font path', t('The current font path is invalid. The default font will be used.'));
}
}
$output .= form_checkbox(t('Check during user registration'), 'captcha_user_register', 'true', _captcha_istrue("captcha_user_register", "true"), 'If enabled, users will be asked to recognize an image during user registration.');
$output .= form_checkbox(t('Check during anonymous comments'), 'captcha_comment_anonymous', 'true', _captcha_istrue("captcha_comment_anonymous"), 'If enabled, anonymous users will be asked to recognize an image while posting .');
$output .= form_checkbox(t('Check during registered user comments'), 'captcha_comment_registered', 'true', _captcha_istrue("captcha_comment_registered"), 'If enabled, registered users will be asked to recognize an image while posting .');
$output .= form_textfield(t('TrueType Fonts Path'), 'captcha_fonts_path', $fonts_path, 30, 255, 'Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.');
$output .= form_textfield(t('TrueType Fonts Path'), 'captcha_fonts_path', $fonts_path, 30, 255, 'Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.');
if (isset($fonts_path)) {
$imagefontinfo .= t('Number of fonts found: ').count(_captcha_font_list());
}
$gdinfo = gd_info();
$imagefontinfo .= '<br />'.t('GD Version: ').$gdinfo["GD Version"];
$imagefontinfo .= '<br />'.t(' FreeType Support: ');
$imagefontinfo .= ($gdinfo["FreeType Support"]==true) ? 'True' : 'False';
$imagefontinfo .= '<br />';
$output .= form_item("Image and font information", '', $imagefontinfo);
return $output;
}
......@@ -84,7 +97,7 @@ function captcha_user($type, &$edit, &$newuser, $category = NULL) {
switch ($type) {
case 'register':
// Add two items to the resigtration form.
$output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
$output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, t('Please type in the letters/numbers that are shown in the image above.'), NULL, TRUE);
......@@ -112,7 +125,7 @@ function captcha_user($type, &$edit, &$newuser, $category = NULL) {
*/
function captcha_comment($op,$edit) {
global $user;
// check if captcha is enabled for form type
if ($user->uid == 0 && !_captcha_istrue("captcha_comment_anonymous")) return;
if ($user->uid != 0 && !_captcha_istrue("captcha_comment_registered")) return;
......@@ -135,7 +148,7 @@ function captcha_comment($op,$edit) {
//invalidate captcha after one comment insert
$_SESSION['captcha_comment_correct'] = '';
break;
case 'form':
$form_html = "";
if ($_SESSION['captcha_comment_correct']!='ok') {
......@@ -145,13 +158,13 @@ function captcha_comment($op,$edit) {
}
return $form_html;
}
}
}
/**
* Returns a random string for use in a captcha
*/
function _captcha_code() {
$consts='bcdgjxvmnprst';
$vowels='aeiou';
......@@ -183,7 +196,7 @@ function _captcha_istrue($variable, $default="true") {
*/
function _captcha_font_list() {
$fontdir = variable_get("captcha_fonts_path", "");
$filelist = array();
if ($handle = opendir($fontdir)) {
while ($file = readdir($handle)) {
......@@ -194,16 +207,19 @@ function _captcha_font_list() {
}
return $filelist;
}
}
/**
* Prints an image containing a captcha code.
*/
function _captcha_image() {
//if we don't have GD functions, we can't generate the image
// there are a few hard coded functions I'd like to eliminate here,
// but for the time being we'll let them be.
//if we don't have GD2 functions, we can't generate the image
if (!function_exists('imagecreatetruecolor')) return;
// Set headers
header('Expires: Mon, 01 Jan 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
......@@ -213,76 +229,68 @@ function _captcha_image() {
header('Content-type: image/png');
$string = _captcha_code();
//set up image, the first number is the width and the second is the height
$im = imagecreatetruecolor(120, 20);
//creates two variables to store color
// set up image, the first number is the width and the second is the height
$im = imagecreatetruecolor(180, 80);
// creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
$foreground = imagecolorallocate($im, rand(0, 80), rand(0, 80), rand(0, 80));
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
//fill image with bgcolor
// fill image with bgcolor
imagefill($im, 0, 0, $background);
//writes string
if (function_exists(imagettftext) && function_exists(imagettfbox) && is_dir($fonts_path)) {
// Get truetype font list
$fonts = _captcha_font_list();
if (count($fonts) > 0) {
// write text using a truetype font
$charSize = 20; // font size
$charWidth = 0; // width of previous character
$x = 20; // initial x position
// iterate over characters
for ($i=0;$i<strlen($string);$i++) {
// define angle and position of character based on previous character dimension
$x += $charWidth * rand(1.6,1.8);
$y = rand(30,50);
$charAngle = rand(-25,25);
$char = substr($code,$i,1);
// select random font
$font = $fonts[rand(0,count($fonts)-1)];
// draw character
imagettftext($im,$charSize,$charAngle,$x,$y,$textColor,$font,$char);
print $font;
// capture character dimensions to increment x position
//$bbox = imagettfbbox($charSize,$charAngle,$font,$char);
$charWidth = max($bbox[0],$bbox[2],$bbox[4],$bbox[6]) - min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
}
}
else {
// write text using a built-in font
$x = 40;
$y = 25;
for ($i=0;$i<strlen($code);$i++) {
imagestring($img,5,$x,$y,substr($code,$i,1),$textColor);
$x += 30;
}
// Get truetype font list
$fonts = _captcha_font_list();
// writes string
if (function_exists(imagettftext) && count($fonts) > 0) {
// write text using a truetype font
$charSize = 24; // font size
$charWidth = 0; // width of previous character
$x = 10; // initial x position
$y = 30;
// iterate over characters
for ($i=0;$i<strlen($string);$i++) {
// define angle and position of character based on previous character dimension
$x += ($charWidth * rand(1.0, 1.6));
$y += rand(-5,5);
$charAngle = rand(-5,5);
$charSize += rand(-2,2);
$char = substr($string,$i,1);
// select random font
$font = $fonts[rand(0,count($fonts)-1)];
// draw character
imagettftext($im,$charSize,$charAngle,$x,$y,$foreground,$font,$char);
// capture character dimensions to increment x position
$bbox = imagettfbbox($charSize,$charAngle,$font,$char);
$charWidth = max($bbox[0],$bbox[2],$bbox[4],$bbox[6]) - min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
}
}
else {
imagestring($im, 5, 10, 0, $string, $foreground);
// write text using a built-in font
$x = 10;
$y = 0;
for ($i=0;$i<strlen($string);$i++) {
imagestring($im,5,$x,$y,substr($string,$i,1),$foreground);
$x += rand(10,15);
$y += rand(-4,4);
}
}
//strikethrough
// strikethrough
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $foreground);
//rotate only if function is defined (many PHP installations have this function missing)
// rotate only if function is defined (many PHP installations have this function missing)
if (function_exists('imagerotate')) {
$im2 = imagerotate($im,rand(-20,45),$background);
imagedestroy($im);
......@@ -290,7 +298,8 @@ function _captcha_image() {
}
$middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
//random shapes
// random shapes
for ($x=0; $x<50;$x++) {
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment