Commit f53eebc7 authored by soxofaan's avatar soxofaan

#188599: added color settings to image CAPTCHA

parent 67fdaece
...@@ -87,6 +87,43 @@ function image_captcha_settings_form() { ...@@ -87,6 +87,43 @@ function image_captcha_settings_form() {
), ),
); );
// color settings
$form['image_captcha_color_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Color settings'),
'#description' => t('Configuration of the background and text colors in the image CAPTCHA.'),
);
$form['image_captcha_color_settings']['image_captcha_background_color'] = array(
'#type' => 'textfield',
'#title' => t('Background color'),
'#description' => t('Enter the hexadecimal code for the background color (e.g. #FFF or #FFCE90).'),
'#default_value' => variable_get('image_captcha_background_color', '#ffffff'),
'#maxlength' => 7,
'#size' => 8,
);
$form['image_captcha_color_settings']['image_captcha_foreground_color'] = array(
'#type' => 'textfield',
'#title' => t('Text color'),
'#description' => t('Enter the hexadecimal code for the text color (e.g. #000 or #004283).'),
'#default_value' => variable_get('image_captcha_foreground_color', '#000000'),
'#maxlength' => 7,
'#size' => 8,
);
$form['image_captcha_color_settings']['image_captcha_foreground_color_randomness'] = array(
'#type' => 'select',
'#title' => t('Additional variation of text color'),
'#options' => array(
0 => t('none'),
50 => t('small'),
100 => t('moderate'),
150 => t('high'),
200 => t('very high'),
),
'#default_value' => (int) variable_get('image_captcha_foreground_color_randomness', 100),
'#description' => t('The different characters will have randomized colors in the specified range around the text color.'),
);
// distortion and noise settings // distortion and noise settings
$form['image_captcha_distortion_and_noise'] = array( $form['image_captcha_distortion_and_noise'] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
...@@ -178,4 +215,12 @@ function image_captcha_settings_form_validate($form, &$form_state) { ...@@ -178,4 +215,12 @@ function image_captcha_settings_form_validate($form, &$form_state) {
elseif ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) { elseif ($font != 'BUILTIN' && (!is_file($font) || !is_readable($font))) {
form_set_error('image_captcha_font', t('Font does not exist or is not readable.')); form_set_error('image_captcha_font', t('Font does not exist or is not readable.'));
} }
// check color settings
if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_background_color'])){
form_set_error('image_captcha_background_color', t('Background color is not a valid hexadecimal color value.'));
}
if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state['values']['image_captcha_foreground_color'])){
form_set_error('image_captcha_foreground_color', t('Text color is not a valid hexadecimal color value.'));
}
} }
...@@ -43,6 +43,24 @@ function image_captcha_image($seed=NULL) { ...@@ -43,6 +43,24 @@ function image_captcha_image($seed=NULL) {
} }
} }
/**
* small helper function for parsing a hexadecimal color to a RGB tuple
*/
function _image_captcha_hex_to_rgb($hex) {
// handle #RGB format
if (strlen($hex) == 4) {
$hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
}
$c = hexdec($hex);
$rgb = array();
for ($i = 16; $i >= 0; $i -= 8) {
$rgb[] = ($c >> $i) & 0xFF;
}
return $rgb;
}
/** /**
* base function for generating a image CAPTCHA * base function for generating a image CAPTCHA
*/ */
...@@ -67,8 +85,9 @@ function _image_captcha_generate_image($code) { ...@@ -67,8 +85,9 @@ function _image_captcha_generate_image($code) {
return FALSE; return FALSE;
} }
// background // get background color and paint
$background_color = imagecolorallocate($image, 255, 255, 255); $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
$background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
imagefilledrectangle($image, 0, 0, $width, $height, $background_color); imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
// draw text // draw text
...@@ -205,7 +224,12 @@ function _image_captcha_image_generator_add_dots(&$image, $width, $height, $colo ...@@ -205,7 +224,12 @@ function _image_captcha_image_generator_add_dots(&$image, $width, $height, $colo
} }
} }
function _image_captcha_image_generator_print_string(&$image, $width, $height, $font, $font_size, $text, $light_colors=FALSE) { /**
* Helper function for drawing text on the image
*
* @param $background_mode if the text is for the background of the double vision mode
*/
function _image_captcha_image_generator_print_string(&$image, $width, $height, $font, $font_size, $text, $background_mode=FALSE) {
// get characters // get characters
$characters = _image_captcha_utf8_split($text); $characters = _image_captcha_utf8_split($text);
$character_quantity = count($characters); $character_quantity = count($characters);
...@@ -227,6 +251,33 @@ function _image_captcha_image_generator_print_string(&$image, $width, $height, $ ...@@ -227,6 +251,33 @@ function _image_captcha_image_generator_print_string(&$image, $width, $height, $
// character jittering // character jittering
$jittering_x = .3 * $font_size; $jittering_x = .3 * $font_size;
$jittering_y = .3 * $font_size; $jittering_y = .3 * $font_size;
// 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'));
// correct foreground for background mode (double vision)
if ($background_mode) {
for ($i=0; $i<3; $i++) {
$foreground_rgb[$i] = 0.75 * $background_rgb[$i] + 0.25 * $foreground_rgb[$i];
}
}
$background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
$foreground_color = imagecolorallocate($image, $foreground_rgb[0], $foreground_rgb[1], $foreground_rgb[2]);
// precalculate the value ranges for color randomness
$foreground_randomness = (int)(variable_get('image_captcha_foreground_color_randomness', 100));
if ($foreground_randomness) {
if ($background_mode) {
$foreground_randomness *= .25;
}
$foreground_color_range = array();
for ($i=0; $i<3; $i++) {
$foreground_color_range[$i] = array(max(0, $foreground_rgb[$i] - $foreground_randomness), min(255, $foreground_rgb[$i] + $foreground_randomness));
}
}
// set default text color
$color = $foreground_color;
// start cursor // start cursor
$x = $spacing; $x = $spacing;
foreach ($characters as $character) { foreach ($characters as $character) {
...@@ -238,13 +289,16 @@ function _image_captcha_image_generator_print_string(&$image, $width, $height, $ ...@@ -238,13 +289,16 @@ function _image_captcha_image_generator_print_string(&$image, $width, $height, $
} }
// calculate y position // calculate y position
$y = .5 * ($height - $character_height); $y = .5 * ($height - $character_height);
// generate random color
if ($light_colors) { // set text color in case of randomness
$color = imagecolorallocate($image, mt_rand(128, 255), mt_rand(128, 255), mt_rand(128, 255)); if ($foreground_randomness) {
} $color = imagecolorallocate($image,
else { mt_rand($foreground_color_range[0][0], $foreground_color_range[0][1]),
$color = imagecolorallocate($image, mt_rand(0, 127), mt_rand(0, 127), mt_rand(0, 127)); mt_rand($foreground_color_range[1][0], $foreground_color_range[1][1]),
mt_rand($foreground_color_range[2][0], $foreground_color_range[2][1])
);
} }
// add jitter to position // add jitter to position
$pos_x = $x + mt_rand(-$jittering_x, $jittering_x); $pos_x = $x + mt_rand(-$jittering_x, $jittering_x);
$pos_y = $y + mt_rand(-$jittering_y, $jittering_y); $pos_y = $y + mt_rand(-$jittering_y, $jittering_y);
......
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