Index: image_captcha.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/image_captcha/image_captcha.admin.inc,v retrieving revision 1.29 diff -u -b -u -p -r1.29 image_captcha.admin.inc --- image_captcha.admin.inc 11 Dec 2009 18:01:44 -0000 1.29 +++ image_captcha.admin.inc 13 Dec 2009 18:44:42 -0000 @@ -4,46 +4,25 @@ /** * @file * Functions for administration/settings interface. + * */ -/** - * function to get a list of available fonts - */ -function _image_captcha_available_fonts() { - // List of folders to search through for TrueType fonts. - $fontsdirectories = array( - drupal_get_path('module', 'image_captcha') .'/fonts', - file_directory_path(), - 'sites/all/libraries/fonts', - conf_path() .'/libraries/fonts', - ); - // Search through the folders and build a list of available fonts. - $available_fonts = array(); - foreach ($fontsdirectories as $fontsdirectory) { - foreach (file_scan_directory($fontsdirectory, '\.[tT][tT][fF]$') as $filename => $font) { - $available_fonts[$filename] = "{$font->basename} ($filename)"; - } - } - - // Append the PHP built-in font at the end. - $available_fonts['BUILTIN'] = t('Built-in font'); - - return $available_fonts; -} /** - * Configuration form for image_captcha + * Configuration form for image_captcha. */ function image_captcha_settings_form() { + // Add CSS for theming of admin form. + drupal_add_css(drupal_get_path('module', 'image_captcha') .'/image_captcha.css'); // Use javascript for some added usability on admin form. drupal_add_js(drupal_get_path('module', 'image_captcha') .'/image_captcha.js'); $form = array(); // First some error checking. - $image_captcha_setup_errno = _image_captcha_check_setup(); - if ($image_captcha_setup_errno & IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG) { + $setup_status = _image_captcha_check_setup(FALSE); + if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG) { drupal_set_message(t( 'The Image CAPTCHA module can not generate images because your PHP setup does not support it (no GD library with JPEG support).', array('!gdlib' => 'http://php.net/manual/en/book.image.php') @@ -81,66 +60,8 @@ function image_captcha_settings_form() { '#description' => t('The code length influences the size of the image. Note that larger values make the image generation more CPU intensive.'), ); - // font related stuff - $form['image_captcha_font_settings'] = array( - '#type' => 'fieldset', - '#title' => t('Font settings'), - ); - $available_fonts = _image_captcha_available_fonts(); - list($default_font, $errno) = _image_captcha_get_font(); - $form['image_captcha_font_settings']['image_captcha_font'] = array( - '#type' => 'select', - '#title' => t('Font'), - '#default_value' => $default_font, - '#description' => t('Select the font to use for the text in the image CAPTCHA. Apart from the provided defaults, you can also use your own TrueType fonts (filename extension .ttf) by putting them in the Drupal "files" directory (directory %filesdir), %fonts_library_general or %fonts_library_specific.', - array('%filesdir' => file_directory_path(), - '%fonts_library_general' => 'sites/all/libraries/fonts', - '%fonts_library_specific' => conf_path() .'/libraries/fonts', - ) - ), - '#options' => $available_fonts, - ); - - // add a prerender procedure for checking that a font should be set. - $form['#pre_render'] = array('image_captcha_settings_form_pre_render'); - // font size - if ($default_font != 'BUILTIN') { - $form['image_captcha_font_settings']['image_captcha_font_size'] = array( - '#type' => 'select', - '#title' => t('Font size'), - '#options' => array( - 9 => '9 pt - ' . t('tiny'), - 12 => '12 pt - ' . t('small'), - 18 => '18 pt', - 24 => '24 pt - ' . t('normal'), - 30 => '30 pt', - 36 => '36 pt - ' . t('large'), - 48 => '48 pt', - 64 => '64 pt - ' . t('extra large'), - ), - '#default_value' => (int) variable_get('image_captcha_font_size', 30), - '#description' => t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'), - ); - } - // Disable some options when there is no TTF support - if ($image_captcha_setup_errno & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) { - $form['image_captcha_font_settings']['image_captcha_font']['#disabled'] = TRUE; - $form['image_captcha_font_settings']['image_captcha_font']['#default_value'] = 'BUILTIN'; - } - - // character spacing - $form['image_captcha_font_settings']['image_captcha_character_spacing'] = array( - '#type' => 'select', - '#title' => t('Character spacing'), - '#description' => t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'), - '#default_value' => variable_get('image_captcha_character_spacing', '1.2'), - '#options' => array( - '0.75' => t('tight'), - '1' => t('normal'), - '1.2' => t('wide'), - '1.5' => t('extra wide'), - ), - ); + // Font related stuff. + $form['image_captcha_font_settings'] = _image_captcha_font_settings_form(); // Color and file format settings. $form['image_captcha_color_settings'] = array( @@ -248,30 +169,185 @@ function image_captcha_settings_form() { ), '#default_value' => (int) variable_get('image_captcha_noise_level', 5), ); + + // Add a validation handler. $form['#validate'] = array('image_captcha_settings_form_validate'); - return system_settings_form($form); + + // Make it a settings form. + $form = system_settings_form($form); + // But also do some custom submission handling. + $form['#submit'][] = 'image_captcha_settings_form_submit'; + + return $form; } + /** - * Pre render function for image_captcha_settings_form for providing - * extra info about TTF fonts. We add these warnings in the pre-render - * phase to avoid that these message whould show up twice after - * submitting the form because it is build twice (first for submitting - * the form and second for displaying the updated form). + * Form elements for the font specific setting. + * + * This is refactored to a separate function to avoid poluting the + * general form function image_captcha_settings_form with some + * specific logic. + * + * @return $form, the font settings specific form elements. */ -function image_captcha_settings_form_pre_render($form) { - $errno = _image_captcha_check_setup(); +function _image_captcha_font_settings_form() { + // Put it all in a fieldset. + $form = array( + '#type' => 'fieldset', + '#title' => t('Font settings'), + ); + + // First check if there is TrueType support. + $setup_status = _image_captcha_check_setup(FALSE); + if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) { + // Show a warning that there is no TrueType support + $form['no_ttf_support'] = array( + '#type' => 'item', + '#title' => t('No TrueType support'), + '#value' => t('The Image CAPTCHA module can not use TrueType fonts because your PHP setup does not support it. You can only use a PHP built-in bitmap font of fixed size.'), + ); + + } + else { + + // Build a list of all available fonts. + $available_fonts = array(); + + // List of folders to search through for TrueType fonts. + $fonts = _image_captcha_get_available_fonts_from_directories(); + // Cache the list of previewable fonts. All the previews are done + // in separate requests, and we don't want to rescan the filesystem + // every time, so we cache the result. + variable_set('image_captcha_fonts_preview_map_cache', $fonts); + // Put these fonts with preview image in the list + foreach ($fonts as $token => $font) { + $img_src = check_url(url('admin/user/captcha/image_captcha/font_preview/'. $token)); + $title = t('Font preview of @font (@file)', array('@font' => $font->name, '@file' => $font->filename)); + $available_fonts[$font->filename] = ''. $title .''; + } + + // We only show the name of fonts from the files directory + // and do not provide a preview for security reasons: + // files in files directory can be uploaded by normal or even anonymous + // users and should not be trusted. + $fonts = _image_captcha_get_available_fonts_from_directories(array(file_directory_path())); + foreach ($fonts as $font) { + $available_fonts[$font->name] = $font->name ." ($font->filename)"; + } + + // Append the PHP built-in font at the end. + $img_src = check_url(url('admin/user/captcha/image_captcha/font_preview/BUILTIN')); + $title = t('Preview of built-in font'); + $available_fonts['BUILTIN'] = t('!font_preview (PHP built-in font)', + array('!font_preview' => ''. $title .'') + ); - if ($errno & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) { - drupal_set_message( - t('The Image CAPTCHA module can not use TrueType fonts because your PHP setup does not support it. You can only use a low quality built-in bitmap font.'), - 'warning' + $default_fonts = _image_captcha_get_enabled_fonts(); + $form['image_captcha_fonts'] = array( + '#type' => 'checkboxes', + '#title' => t('Fonts'), + '#default_value' => $default_fonts, + '#description' => t('Select the fonts to use for the text in the image CAPTCHA. Apart from the provided defaults, you can also use your own TrueType fonts (filename extension .ttf) by putting them in %fonts_library_general or %fonts_library_specific. You can also upload them to your site, e.g. with the "Upload" module, (into the "files" directory %filesdir), but those fonts will not get a preview because of security reasons.', + array( + '%fonts_library_general' => 'sites/all/libraries/fonts', + '%fonts_library_specific' => conf_path() .'/libraries/fonts', + '%filesdir' => file_directory_path(), + ) + ), + '#options' => $available_fonts, + '#attributes' => array('class' => 'image_captcha_admin_fonts_selection'), + '#process' => array('expand_checkboxes', 'image_captcha_columnify_font_selection'), ); + + + // Font size. + $form['image_captcha_font_size'] = array( + '#type' => 'select', + '#title' => t('Font size'), + '#options' => array( + 9 => '9 pt - ' . t('tiny'), + 12 => '12 pt - ' . t('small'), + 18 => '18 pt', + 24 => '24 pt - ' . t('normal'), + 30 => '30 pt', + 36 => '36 pt - ' . t('large'), + 48 => '48 pt', + 64 => '64 pt - ' . t('extra large'), + ), + '#default_value' => (int) variable_get('image_captcha_font_size', 30), + '#description' => t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'), + ); + } + + // Character spacing (available for both the TrueType fonts and the builtin font. + $form['image_captcha_font_settings']['image_captcha_character_spacing'] = array( + '#type' => 'select', + '#title' => t('Character spacing'), + '#description' => t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'), + '#default_value' => variable_get('image_captcha_character_spacing', '1.2'), + '#options' => array( + '0.75' => t('tight'), + '1' => t('normal'), + '1.2' => t('wide'), + '1.5' => t('extra wide'), + ), + ); + return $form; } /** + * Helper function to get fonts from the given directories. + * + * @param $directories (optional) an array of directories + * to recursively search through, if not given, the default + * directories will be used. + * + * @return an array of fonts file objects (with fields 'name', + * 'basename' and 'filename'), keyed on the md5 hash of the font + * path (to have an easy token that can be used in an url + * without en/decoding issues). + */ +function _image_captcha_get_available_fonts_from_directories($directories=NULL) { + // If no fonts directories are given: use the default. + if ($directories === NULL) { + $directories = array( + drupal_get_path('module', 'image_captcha') .'/fonts', + 'sites/all/libraries/fonts', + conf_path() .'/libraries/fonts', + ); + } + // Collect the font information. + $fonts = array(); + foreach ($directories as $directory) { + foreach (file_scan_directory($directory, '\.[tT][tT][fF]$') as $filename => $font) { + $fonts[md5($filename)] = $font; + } + } + + return $fonts; +} + + +/** + * Additional processing (after Drupal core's expand_checkboxes) + * to put the font previews in a multi-column layout. + */ +function image_captcha_columnify_font_selection($element) { + // Get the fonts that get a preview. + $fonts = variable_get('image_captcha_fonts_preview_map_cache', array()); + // And add some markup so we can put them in column layout. + foreach ($fonts as $font) { + $element[$font->filename]['#prefix'] = '
'; + $element[$font->filename]['#suffix'] = '
'; + } + + return $element; + +} +/** * Validation function for image_captcha configuration form */ function image_captcha_settings_form_validate($form, &$form_state) { @@ -279,19 +355,24 @@ function image_captcha_settings_form_val if (preg_match('/\s/', $form_state['values']['image_captcha_image_allowed_chars'])) { form_set_error('image_captcha_image_allowed_chars', t('The list of characters to use should not contain spaces.')); } - // Check font. - $font = $form_state['values']['image_captcha_font']; - if ($font == '0') { - form_set_error('image_captcha_font', t('You need to select a font')); + + if (!isset($form['image_captcha_font_settings']['no_ttf_support'])) { + // Check the selected fonts. + // Filter the image_captcha fonts array to pick out the selected ones. + $fonts = array_filter($form_state['values']['image_captcha_fonts']); + if (count($fonts) < 1) { + form_set_error('image_captcha_fonts', t('You need to select at least one font.')); } - elseif ($font == 'BUILTIN') { + if ($form_state['values']['image_captcha_fonts']['BUILTIN']) { // With the built in font, only latin2 characters should be used. if (preg_match('/[^a-zA-Z0-9]/', $form_state['values']['image_captcha_image_allowed_chars'])) { form_set_error('image_captcha_image_allowed_chars', t('The built-in font only supports Latin2 characters. Only use "a" to "z" and numbers.')); } } - elseif ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) { - form_set_error('image_captcha_font', t('Font does not exist or is not readable.')); + list($readable_fonts, $problem_fonts) = _image_captcha_check_fonts($fonts); + if (count($problem_fonts) > 0) { + form_set_error('image_captcha_fonts', t('The following fonts are not readable: %fonts.', array('%fonts' => implode(', ', $problem_fonts)))); + } } // check color settings @@ -302,3 +383,70 @@ function image_captcha_settings_form_val form_set_error('image_captcha_foreground_color', t('Text color is not a valid hexadecimal color value.')); } } + +/** + * Submit function for image_captcha configuration form. + */ +function image_captcha_settings_form_submit($form, &$form_state) { + if (!isset($form['image_captcha_font_settings']['no_ttf_support'])) { + // Filter the image_captcha fonts array to pick out the selected ones. + $fonts = array_filter($form_state['values']['image_captcha_fonts']); + variable_set('image_captcha_fonts', $fonts); + } +} + +/** + * Menu handler for font preview request. + * + */ +function image_captcha_font_preview($font_token) { + + // Get the font from the given font token. + if ($font_token == 'BUILTIN') { + $font = 'BUILTIN'; + } + else { + // Get the mapping of font tokens to font file objects. + $fonts = variable_get('image_captcha_fonts_preview_map_cache', array()); + if (!isset($fonts[$font_token])) { + echo('bad token'); + exit(); + } + // Get the font path. + $font = $fonts[$font_token]->filename; + // Some sanity checks if the given font is valid. + if (!is_file($font) || !is_readable($font)) { + echo('bad font'); + exit(); + } + } + + // Settings of the font preview. + $width = 120; + $text = 'AaBbCc123'; + $font_size = 14; + $height = 2 * $font_size; + + // Allocate image resource. + $image = imagecreatetruecolor($width, $height); + if (!$image) { + exit(); + } + // White background and black foreground. + $background_color = imagecolorallocate($image, 255, 255, 255); + $color = imagecolorallocate($image, 0, 0, 0); + imagefilledrectangle($image, 0, 0, $width, $height, $background_color); + + // Draw preview text + if ($font == 'BUILTIN') { + imagestring($image, 5, 1, .5*$height-10, $text, $color); + } + else { + imagettftext($image, $font_size, 0, 1, 1.5*$font_size, $color, realpath($font), $text); + } + + drupal_set_header("Content-type: image/png"); + imagepng($image); + imagedestroy($image); + exit(); +} Index: image_captcha.css =================================================================== RCS file: image_captcha.css diff -N image_captcha.css --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ image_captcha.css 13 Dec 2009 18:44:42 -0000 @@ -0,0 +1,31 @@ + + +/** + * Styling of the font selection list (with previews) + * on the Image CAPTCHA settings page. + */ + +/** + * Float the fonts with preview (with a fixed width) + * to create a multi-column layout. + */ +.image_captcha_admin_font_preview { + float: left; + width: 160px; +} + +/** + * Fonts without a preview should not follow the column layout. + */ +.image_captcha_admin_fonts_selection .form-item { + clear: both; +} + +/** + * Center the font previews vertically to the text. + */ +.image_captcha_admin_fonts_selection img { + vertical-align: middle; +} + + Index: image_captcha.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/image_captcha/image_captcha.install,v retrieving revision 1.8 diff -u -b -u -p -r1.8 image_captcha.install --- image_captcha.install 21 Mar 2009 22:33:59 -0000 1.8 +++ image_captcha.install 13 Dec 2009 18:44:42 -0000 @@ -26,3 +26,20 @@ function image_captcha_uninstall() { db_query("DELETE FROM {variable} WHERE name LIKE 'image_captcha_%'"); cache_clear_all('variables', 'cache'); } + +/** + * Implementation of hook_update_N(). + * + * Translate single font variable to multiple font variable. + */ +function image_captcha_update_6203() { + // Old single font variable. + $font = variable_get('image_captcha_font', NULL); + // If there was a valid value, + // save it as an array to the new multiple fonts variable. + if ($font != NULL) { + variable_set('image_captcha_fonts', array($font)); + } + return array(); +} + Index: image_captcha.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/image_captcha/image_captcha.module,v retrieving revision 1.32 diff -u -b -u -p -r1.32 image_captcha.module --- image_captcha.module 8 Dec 2009 22:25:19 -0000 1.32 +++ image_captcha.module 13 Dec 2009 18:44:42 -0000 @@ -10,6 +10,7 @@ define('IMAGE_CAPTCHA_ALLOWED_CHARACTERS', 'aAbBCdEeFfGHhijKLMmNPQRrSTtWXYZ23456789'); +// Setup status flags. define('IMAGE_CAPTCHA_SETUP_OK', 0); define('IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG', 1); define('IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT', 2); @@ -45,6 +46,14 @@ function image_captcha_menu() { 'access arguments' => array('administer CAPTCHA settings'), 'type' => MENU_LOCAL_TASK, ); + // Menu path for generating font example. + $items['admin/user/captcha/image_captcha/font_preview'] = array( + 'title' => 'Font example', + 'file' => 'image_captcha.admin.inc', + 'page callback' => 'image_captcha_font_preview', + 'access arguments' => array('administer CAPTCHA settings'), + 'type' => MENU_CALLBACK, + ); // callback for generating an image $items['image_captcha'] = array( 'file' => 'image_captcha.user.inc', @@ -56,16 +65,41 @@ function image_captcha_menu() { } /** - * Helper function for getting the font setting. - * @return array($font, $errno) + * Helper function for getting the fonts to use in the image CAPTCHA. + * + * @return a list of font paths. + */ +function _image_captcha_get_enabled_fonts() { + if (IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT &_image_captcha_check_setup(FALSE)) { + return array('BUILTIN'); + } + else { + $default = array( + drupal_get_path('module', 'image_captcha') .'/fonts/Tesox/tesox.ttf', + drupal_get_path('module', 'image_captcha') .'/fonts/Tuffy/Tuffy.ttf', + ); + return variable_get('image_captcha_fonts', $default); + } +} + +/** + * Helper function for checking if the specified fonts are available. + * + * @param $fonts paths of fonts to check. + * @return list($readable_fonts, $problem_fonts) */ -function _image_captcha_get_font() { - $font = variable_get('image_captcha_font', drupal_get_path('module', 'image_captcha') .'/fonts/Tesox/tesox.ttf'); - $errno = 0; +function _image_captcha_check_fonts($fonts) { + $readable_fonts = array(); + $problem_fonts = array(); + foreach ($fonts as $font) { if ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) { - $errno = IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM; + $problem_fonts[] = $font; + } + else { + $readable_fonts[] = $font; + } } - return array($font, $errno); + return array($readable_fonts, $problem_fonts); } /** @@ -105,23 +139,32 @@ function _image_captcha_utf8_split($str) /** * Helper function for checking if the requirements for the Image CAPTCHA are met. * - * @return error code such as IMAGE_CAPTCHA_SETUP_OK, IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG, - * IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT, IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM. - */ -function _image_captcha_check_setup() { + * @param $check_fonts whether or not the enabled fonts should be checked. + * + * @return status code: bitwise 'OR' of status flags like + * IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG, IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT, + * IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM. + * When no problems, the status is IMAGE_CAPTCHA_SETUP_OK. + */ +function _image_captcha_check_setup($check_fonts=TRUE) { + // Start clean. + $status = IMAGE_CAPTCHA_SETUP_OK; // Check GD library and is TTF support. if (!function_exists('imagejpeg')) { - return IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG; + $status = $status | IMAGE_CAPTCHA_ERROR_NO_GDLIB_WITH_JPEG; } if (!function_exists('imagettftext')) { - return IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT; + $status = $status | IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT; + } + if ($check_fonts) { + // Check availability of enabled fonts. + $fonts = _image_captcha_get_enabled_fonts(); + list($readable_fonts, $problem_fonts) = _image_captcha_check_fonts($fonts); + if (count($problem_fonts) != 0) { + $status = $status | IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM; } - // Check font settings. - list($font, $errno) = _image_captcha_get_font(); - if ($errno && IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM) { - return IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM; } - return IMAGE_CAPTCHA_SETUP_OK; + return $status; } /** Index: image_captcha.user.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/captcha/image_captcha/image_captcha.user.inc,v retrieving revision 1.21 diff -u -b -u -p -r1.21 image_captcha.user.inc --- image_captcha.user.inc 22 Sep 2009 22:51:00 -0000 1.21 +++ image_captcha.user.inc 13 Dec 2009 18:44:42 -0000 @@ -62,17 +62,13 @@ function _image_captcha_hex_to_rgb($hex) return $rgb; } - /** * Base function for generating a image CAPTCHA. */ function _image_captcha_generate_image($code) { // Get font. - list($font, $errno) = _image_captcha_get_font(); - if ($errno != 0) { - watchdog('CAPTCHA', 'Could not find or read the configured font ("%font") for the image CAPTCHA.', array('%font' => $font), WATCHDOG_ERROR); - exit(); - } + $fonts = _image_captcha_get_enabled_fonts(); + // get other settings $font_size = (int) variable_get('image_captcha_font_size', 30); $character_spacing = (float) variable_get('image_captcha_character_spacing', '1.2'); @@ -98,7 +94,7 @@ function _image_captcha_generate_image($ imagefilledrectangle($image, 0, 0, $width, $height, $background_color); // draw text - $result = _image_captcha_image_generator_print_string($image, $width, $height, $font, $font_size, $code); + $result = _image_captcha_image_generator_print_string($image, $width, $height, $fonts, $font_size, $code); if (!$result) { return FALSE; } @@ -231,18 +227,11 @@ function _image_captcha_image_generator_ /** * Helper function for drawing text on the image. */ -function _image_captcha_image_generator_print_string(&$image, $width, $height, $font, $font_size, $text) { +function _image_captcha_image_generator_print_string(&$image, $width, $height, $fonts, $font_size, $text) { // get characters $characters = _image_captcha_utf8_split($text); $character_quantity = count($characters); - // get character width for builtin font - if ($font == 'BUILTIN') { - $character_width = imagefontwidth(5); - $character_height = imagefontheight(5); - $bbox = array(0, $character_height, $character_width, $character_height, $character_width, 0, 0, 0); - } - // get colors $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff')); $foreground_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_foreground_color', '#000000')); @@ -270,10 +259,20 @@ function _image_captcha_image_generator_ $center_x = ($c + 0.5) * $ccage_width; $center_y = 0.5 * $height; + + // Pick a random font from the list. + $font = array_rand($fonts); + + // get character dimensions for ttf fonts if ($font != 'BUILTIN') { $bbox = imagettfbbox($font_size, 0, realpath($font), $character); } + else { + $character_width = imagefontwidth(5); + $character_height = imagefontheight(5); + $bbox = array(0, $character_height, $character_width, $character_height, $character_width, 0, 0, 0); + } // determine print position: at what coordinate should the character be // printed so that the bounding box would be nicely centered in the cage?