--- textimage.module.cvs 2006-08-16 14:27:06.000000000 +0200 +++ textimage.module 2006-08-17 00:33:44.000000000 +0200 @@ -5,11 +5,9 @@ switch ($section) { case 'admin/help#textimage': - $output .= "

Implements an image recognition captcha.

"; + $output = '

'.t('Implements an image recognition captcha.').'

'; break; - case 'admin/modules#description': - case 'admin/modules/textimage': - case 'admin/textimage': + case 'admin/settings/modules#description': $output = t('Implements an image recognition captcha.'); break; } @@ -17,6 +15,7 @@ } function textimage_captchachallenge(&$form) { + $img_title = t('Captcha Image: you will need to recognize the text in it.'); $form['captcha_response'] = array ( '#type' => 'textfield', '#title' => t('Captcha Validation'), @@ -24,14 +23,15 @@ '#required' => TRUE, '#validate' => array('_captcha_validate' => array()), '#description' => t('Please type in the letters/numbers that are shown in the image above.'), - '#prefix' => '
Captcha Image: you will need to recognize the text in it.
', + '#prefix' => '
' . $img_title . '
', + '#weight' => 2 ); return $form; } function textimage_captchavalidate(&$captcha_word, &$correct) { - $captcha_word = drupal_strtolower($captcha_word); if ($captcha_word == $_SESSION['captcha']) { $correct = true; @@ -49,16 +49,21 @@ function textimage_menu($may_cache) { $items = array(); - $suffix = ''; - - if (arg(2)!=null) $suffix='/'.arg(2); - - $items[] = array( - 'path' => '_textimage/image'.$suffix, 'title' => t('textimage'), - 'callback' => '_textimage_image', - 'access' => user_access('access textimages'), - 'type' => MENU_CALLBACK - ); + if ($may_cache) { + $items[] = array('path' => 'admin/settings/textimage', 'callback' => 'textimage_admin_settings', + 'description' => t('Settings for captcha images.'), 'title' => 'textimage', + 'type' => MENU_NORMAL_ITEM, 'access' => user_access('access administration pages')); + } + else { + $suffix = ''; + if (arg(2)!=null) $suffix = '/'.arg(2); + $items[] = array( + 'path' => '_textimage/image'.$suffix, 'title' => t('textimage'), + 'callback' => '_textimage_image', + 'access' => user_access('access textimages'), + 'type' => MENU_CALLBACK + ); + } return $items; } @@ -67,147 +72,148 @@ return array('access textimages'); } -function textimage_settings() { - - $fonts_path = variable_get("textimage_fonts_path", ""); +function textimage_admin_settings() { + $fonts_path = variable_get("textimage_fonts_path", ""); $images_path = variable_get("textimage_images_path", ""); //check for GD - if (!function_exists(imagecreate)) + if (!function_exists(imagecreate)) { drupal_set_message(t('Image library not available. Textimage needs the GD library extension to be installed. Please install GD.')); - - //check for TTF support - elseif (!function_exists(imagettftext)) - drupal_set_message(t('Your image library does not seem to have TrueType font support. Textimage will work, but will use the default inbuilt font.'),'status'); - - //check for valid font path - elseif ($fonts_path!="" && !is_dir($fonts_path)) - drupal_set_message(t('The current font path is invalid. The default font will be used.')); - - //check for valid image path - if ($images_path!="" && !is_dir($images_path)) - drupal_set_message(t('The current images path is invalid. No images will be used.')); + } + //check for TTF support + elseif (!function_exists(imagettftext)) { + drupal_set_message(t('Your image library does not seem to have TrueType font support. Textimage will work, but will use the default inbuilt font.')); + } + //check for valid font path + elseif ($fonts_path!="" && !is_dir($fonts_path)) { + drupal_set_message(t('The current font path is invalid. The default font will be used.')); + } + + //check for valid image path + if ($images_path && !is_dir($images_path)) { + drupal_set_message(t('The current images path is invalid. No images will be used.')); + } //Fonts settings $form['fonts'] = array( - '#type' => 'fieldset', '#title' => t('Fonts settings'), - '#collapsible' => TRUE, '#collapsed' => FALSE - ); + '#type' => 'fieldset', '#title' => t('Fonts settings'), + '#collapsible' => TRUE, '#collapsed' => FALSE + ); $form['fonts']['textimage_use_only_upper'] = array( - '#type' => 'checkbox', - '#title' => t('Use only Uppercase'), - '#default_value' => variable_get('textimage_use_only_upper',0) - ); + '#type' => 'checkbox', + '#title' => t('Use only Uppercase'), + '#default_value' => variable_get('textimage_use_only_upper',0) + ); $form['fonts']['textimage_fonts_path'] = array( - '#type' => 'textfield', - '#title' => t('TrueType Fonts Path'), - '#default_value' => $fonts_path, - '#size' => 30, - '#maxlength' => 255, - '#description' => t('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. Relative paths will be resolved relative to the Drupal installation directory.'), - ); + '#type' => 'textfield', + '#title' => t('TrueType Fonts Path'), + '#default_value' => $fonts_path, + '#size' => 30, + '#maxlength' => 255, + '#description' => t('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. Relative paths will be resolved relative to the Drupal installation directory.'), + ); $form['fonts']['textimage_font_size'] = array( - '#type' => 'textfield', - '#title' => t('Font Size'), - '#default_value' => variable_get('textimage_font_size',24), - '#size' => 5, - '#maxlength' => 2, - '#description' => t('Font size of Captcha text (in pixels).'), - '#validate' => array("_textimage_number_validate" => array("textimage_font_size")), - ); + '#type' => 'textfield', + '#title' => t('Font Size'), + '#default_value' => variable_get('textimage_font_size',24), + '#size' => 5, + '#maxlength' => 2, + '#description' => t('Font size of Captcha text (in pixels).'), + '#validate' => array("_textimage_number_validate" => array("textimage_font_size")), + ); $form['fonts']['textimage_char_spacing_max'] = array( - '#type' => 'textfield', - '#title' => t('Character Spacing'), - '#default_value' => variable_get('textimage_char_spacing_max',10), - '#size' => 5, - '#maxlength' => 4, - '#description' => t('Sets the kerning between letters in Captcha. Higher numbers indicate more spacing.'), - '#validate' => array("_textimage_number_validate" => array("textimage_char_spacing_max")), - ); + '#type' => 'textfield', + '#title' => t('Character Spacing'), + '#default_value' => variable_get('textimage_char_spacing_max',10), + '#size' => 5, + '#maxlength' => 4, + '#description' => t('Sets the kerning between letters in Captcha. Higher numbers indicate more spacing.'), + '#validate' => array("_textimage_number_validate" => array("textimage_char_spacing_max")), + ); $form['fonts']['textimage_char_jiggle_amount'] = array( - '#type' => 'textfield', - '#title' => t('Character Jiggle'), - '#default_value' => variable_get('textimage_char_jiggle_amount',5), - '#size' => 5, - '#maxlength' => 2, - '#description' => t('Sets the amount of up and down movement in the Captcha letters. Higher numbers indicate more jiggling.'), - '#validate' => array("_textimage_number_validate" => array("textimage_char_jiggle_amount")), - ); + '#type' => 'textfield', + '#title' => t('Character Jiggle'), + '#default_value' => variable_get('textimage_char_jiggle_amount',5), + '#size' => 5, + '#maxlength' => 2, + '#description' => t('Sets the amount of up and down movement in the Captcha letters. Higher numbers indicate more jiggling.'), + '#validate' => array("_textimage_number_validate" => array("textimage_char_jiggle_amount")), + ); $form['fonts']['textimage_char_rotate_amount'] = array( - '#type' => 'textfield', - '#title' => t('Character Rotation'), - '#default_value' => variable_get('textimage_char_rotate_amount',5), - '#size' => 5, - '#maxlength' => 2, - '#description' => t('Sets the amount of rotation in the Captcha letters (in degrees, only works with non-default fonts).'), - '#validate' => array("_textimage_number_validate" => array("textimage_char_rotate_amount")), - ); + '#type' => 'textfield', + '#title' => t('Character Rotation'), + '#default_value' => variable_get('textimage_char_rotate_amount',5), + '#size' => 5, + '#maxlength' => 2, + '#description' => t('Sets the amount of rotation in the Captcha letters (in degrees, only works with non-default fonts).'), + '#validate' => array("_textimage_number_validate" => array("textimage_char_rotate_amount")), + ); $form['fonts']['textimage_char_size_amount'] = array( - '#type' => 'textfield', - '#title' => t('Character Size Adjustment'), - '#default_value' => variable_get('textimage_char_size_amount',2), - '#size' => 5, - '#maxlength' => 2, - '#description' => t('Sets the amount of variation in size between the different letters in the Captcha (in pixels).'), - '#validate' => array("_textimage_number_validate" => array("textimage_char_size_amount")), - ); + '#type' => 'textfield', + '#title' => t('Character Size Adjustment'), + '#default_value' => variable_get('textimage_char_size_amount',2), + '#size' => 5, + '#maxlength' => 2, + '#description' => t('Sets the amount of variation in size between the different letters in the Captcha (in pixels).'), + '#validate' => array("_textimage_number_validate" => array("textimage_char_size_amount")), + ); //Image settings $form['images'] = array( - '#type' => 'fieldset', '#title' => t('Image settings'), - '#collapsible' => TRUE, '#collapsed' => FALSE - ); + '#type' => 'fieldset', '#title' => t('Image settings'), + '#collapsible' => TRUE, '#collapsed' => FALSE + ); $form['images']['textimage_images_path'] = array( - '#type' => 'textfield', - '#title' => t('Background Images Path'), - '#default_value' => $images_path, - '#size' => 30, - '#maxlength' => 255, - '#description' => t('Location of the directory where the background images are stored. If you do not provide a directory, solid colors will be used. Relative paths will be resolved relative to the Drupal installation directory.'), - ); + '#type' => 'textfield', + '#title' => t('Background Images Path'), + '#default_value' => $images_path, + '#size' => 30, + '#maxlength' => 255, + '#description' => t('Location of the directory where the background images are stored. If you do not provide a directory, solid colors will be used. Relative paths will be resolved relative to the Drupal installation directory.'), + ); $form['images']['textimage_image_noise'] = array( - '#type' => 'textfield', - '#title' => t('Image Noise (pixels)'), - '#default_value' => variable_get('textimage_image_noise',4), - '#size' => 5, - '#maxlength' => 4, - '#description' => t('Sets the amount of noise (random pixels) in the Captcha image. Higher numbers indicate more noise.'), - '#validate' => array("_textimage_number_validate" => array("textimage_image_noise")), - ); + '#type' => 'textfield', + '#title' => t('Image Noise (pixels)'), + '#default_value' => variable_get('textimage_image_noise',4), + '#size' => 5, + '#maxlength' => 4, + '#description' => t('Sets the amount of noise (random pixels) in the Captcha image. Higher numbers indicate more noise.'), + '#validate' => array("_textimage_number_validate" => array("textimage_image_noise")), + ); $form['images']['textimage_image_lines'] = array( - '#type' => 'textfield', - '#title' => t('Image Noise (lines)'), - '#default_value' => variable_get('textimage_image_lines',4), - '#size' => 5, - '#maxlength' => 4, - '#description' => t('Sets the amount of noise (random lines) in the Captcha image. Higher numbers indicate more noise.'), - '#validate' => array("_textimage_number_validate" => array("textimage_image_lines")), - ); + '#type' => 'textfield', + '#title' => t('Image Noise (lines)'), + '#default_value' => variable_get('textimage_image_lines',4), + '#size' => 5, + '#maxlength' => 4, + '#description' => t('Sets the amount of noise (random lines) in the Captcha image. Higher numbers indicate more noise.'), + '#validate' => array("_textimage_number_validate" => array("textimage_image_lines")), + ); $form['images']['textimage_image_margin'] = array( - '#type' => 'textfield', - '#title' => t('Image Margin'), - '#default_value' => variable_get('textimage_image_margin',10), - '#size' => 5, - '#maxlength' => 4, - '#description' => t('Set a distance between the Captcha letters and the edges of the image.'), - '#validate' => array("_textimage_number_validate" => array("textimage_image_margin")), - ); + '#type' => 'textfield', + '#title' => t('Image Margin'), + '#default_value' => variable_get('textimage_image_margin',10), + '#size' => 5, + '#maxlength' => 4, + '#description' => t('Set a distance between the Captcha letters and the edges of the image.'), + '#validate' => array("_textimage_number_validate" => array("textimage_image_margin")), + ); $form['info'] = array( - '#type' => 'fieldset', '#title' => t('Image and font information'), - '#collapsible' => TRUE, '#collapsed' => FALSE - ); + '#type' => 'fieldset', '#title' => t('Image and font information'), + '#collapsible' => TRUE, '#collapsed' => FALSE + ); if (isset($fonts_path)) { $imagefontinfo .= t('Number of fonts found: ').count(_textimage_font_list()); } @@ -222,20 +228,22 @@ $imagefontinfo .= '
'; $form['info']['captcha_info'] = array ( - '#type' => 'item', - '#value' => $imagefontinfo, - ); + '#type' => 'item', + '#value' => $imagefontinfo, + ); - return $form; + return system_settings_form('textimage', $form); } function textimage_settings_form_validate ($form_id,$form) { //check for valid font path - if ($form['textimage_fonts_path'] !="" && !is_dir($form['textimage_fonts_path'])) + if ($form['textimage_fonts_path'] && !is_dir($form['textimage_fonts_path'])) { form_set_error('textimage_fonts_path', t('The entered font path is invalid')); + } //check for valid image path - if ($form['textimage_images_path'] !="" && !is_dir($form['textimage_images_path'])) + if ($form['textimage_images_path'] && !is_dir($form['textimage_images_path'])) { form_set_error('textimage_images_path', t('The entered image path is invalid')); + } } function _textimage_number_validate ($field,$fieldName) { @@ -283,10 +291,8 @@ // write text using a truetype font if (function_exists(imagettftext) && count($fonts) > 0) { - - // Initialize variables for the loop - $characterDetails = array(); // contains the final info about each character + $characterDetails = array(); // contains the final info about each character // Build a list of character settings for the captcha string for ($i=0;$i $charSize, - "angle" => $charAngle, - "x" => $x, - "y" => $y, - "color" => $foreground, - "font" => $font, - "char" => $char - ); + $characterDetails[] = array ( + "size" => $charSize, + "angle" => $charAngle, + "x" => $x, + "y" => $y, + "font" => $font, + "char" => $char + ); // Increment the image size - $imageWidth = $x + $charWidth; - $imageHeight = max($imageHeight,$y+$charJiggleAmount); + $imageWidth = $x + $charWidth; + $imageHeight = max($imageHeight,$y+$charJiggleAmount); } - // Create the image based off the string length and margin + // Create the image based off the string length and margin if (count($images) > 0) { // We're going to be using an image, and need a tranparent background to start with $im = _textimage_create_transparent_image($imageWidth+2*$imageMargin, $imageHeight+2*$imageMargin); - $noisecolor = imagecolorallocatealpha($im, 0, 0, 0, 127); } else { // Just make a plain-jane color brackground $im = imagecreatetruecolor($imageWidth+2*$imageMargin, $imageHeight+2*$imageMargin); $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); - $noisecolor = $background; imagefill($im, 0, 0, $background); } + // set the foreground and the noise color + $foreground = imagecolorallocate($im, rand(0, 80), rand(0, 80), rand(0, 80)); + $noisecolor = $foreground; - // Specify colors to be used in the image - $foreground = imagecolorallocate($im, rand(0, 80), rand(0, 80), rand(0, 80)); - - foreach($characterDetails as $char) { - // draw character - imagettftext($im,$char['size'],$char['angle'],$char['x']+$imageMargin,$char['y']+$imageMargin,$foreground,$char['font'],$char['char']); - } - + foreach ($characterDetails as $char) { + // draw character + imagettftext($im,$char['size'],$char['angle'],$char['x']+$imageMargin,$char['y']+$imageMargin,$foreground,$char['font'],$char['char']); + } } else { // write text using a built-in font - $x = 0; + $x = $imageMargin; $y = 0; - $imageWidth = 60 + drupal_strlen($string)*$charSpacingMax*.35; + $imageWidth = 90 + drupal_strlen($string)*$charSpacingMax*.35; $imageHeight = 30 + $charJiggleAmount; // Create the image if (count($images) > 0 && function_exists(imagecolorallocatealpha)) { // We're going to be using an image, and need a tranparent background to start with $im = _textimage_create_transparent_image($imageWidth, $imageHeight); - $noisecolor = imagecolorallocatealpha($im, 0, 0, 0, 127); } else { // Just make a plain-jane color brackground $im = imagecreatetruecolor($imageWidth, $imageHeight); $background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); - $noisecolor = $background; imagefill($im, 0, 0, $background); } - + + // set the foreground and the noise color + $foreground = imagecolorallocate($im, rand(0, 80), rand(0, 80), rand(0, 80)); + $noisecolor = $foreground; + // Add the text for ($i=0;$i +?> \ No newline at end of file