From 8963f4010174ad9edf75fb60c7f44f4464680e29 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 18 Jul 2013 16:02:47 +0300 Subject: Add color customization to the CAPTCHA helper Ref: Issue #867 & PRs #1405, #2485 --- system/helpers/captcha_helper.php | 54 ++++++++++++++++-------- user_guide_src/source/changelog.rst | 6 ++- user_guide_src/source/helpers/captcha_helper.rst | 11 ++++- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index f3b9c6cc4..2d2ae7751 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -43,15 +43,31 @@ if ( ! function_exists('create_captcha')) /** * Create CAPTCHA * - * @param array array of data for the CAPTCHA - * @param string path to create the image in - * @param string URL to the CAPTCHA image folder - * @param string server path to font + * @param array $data data for the CAPTCHA + * @param string $img_path path to create the image in + * @param string $img_url URL to the CAPTCHA image folder + * @param string $font_path server path to font * @return string */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + $defaults = array( + 'word' => '', + 'img_path' => '', + 'img_url' => '', + 'img_width' => '150', + 'img_height' => '30', + 'font_path' => '', + 'expiration' => 7200, + 'word_length' => 8, + 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'colors' => array( + 'background' => array(255,255,255), + 'border' => array(153,102,102), + 'text' => array(204,153,153), + 'grid' => array(255,182,182) + ) + ); foreach ($defaults as $key => $val) { @@ -122,15 +138,19 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Assign colors - // ----------------------------------- - $bg_color = imagecolorallocate($im, 255, 255, 255); - $border_color = imagecolorallocate($im, 153, 102, 102); - $text_color = imagecolorallocate($im, 204, 153, 153); - $grid_color = imagecolorallocate($im, 255, 182, 182); - $shadow_color = imagecolorallocate($im, 255, 240, 240); + // ---------------------------------- + + is_array($colors) OR $colors = $defaults['colors']; + + foreach (array_keys($default['colors']) as $key) + { + // Check for a possible missing value + is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key]; + $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]); + } - // Create the rectangle - ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); + // Create the rectangle + ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']); // ----------------------------------- // Create the spiral pattern @@ -151,7 +171,7 @@ if ( ! function_exists('create_captcha')) $rad1 = $radius * (($i + 1) / $points); $x1 = ($rad1 * cos($theta)) + $x_axis; $y1 = ($rad1 * sin($theta)) + $y_axis; - imageline($im, $x, $y, $x1, $y1, $grid_color); + imageline($im, $x, $y, $x1, $y1, $colors['grid']); $theta -= $thetac; } @@ -178,19 +198,19 @@ if ( ! function_exists('create_captcha')) if ($use_font === FALSE) { $y = rand(0 , $img_height / 2); - imagestring($im, $font_size, $x, $y, $word[$i], $text_color); + imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']); $x += ($font_size * 2); } else { $y = rand($img_height / 2, $img_height - 3); - imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]); + imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]); $x += $font_size; } } // Create the border - imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color); + imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']); // ----------------------------------- // Generate the image diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 6f08dcb92..4cbbda70e 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -123,10 +123,14 @@ Release Date: Not Released - Deprecated function ``trim_slashes()`` - it's just an alias for PHP's native ``trim()`` (with a slash as its second argument). - Deprecated randomization type options **unique** and **encrypt** for funcion :php:func:`random_string()` (they are only aliases for **md5** and **sha1** respectively). + - :doc:`CAPTCHA Helper ` changes include: + + - Added *word_length* and *pool* options to allow customization of the generated word. + - Added *colors* configuration to allow customization for the *background*, *border*, *text* and *grid* colors. + - :doc:`Directory Helper ` :php:func:`directory_map()` will now append ``DIRECTORY_SEPARATOR`` to directory names in the returned array. - :doc:`Language Helper ` :php:func:`lang()` now accepts an optional list of additional HTML attributes. - Deprecated the :doc:`Email Helper ` as its ``valid_email()``, ``send_email()`` functions are now only aliases for PHP native functions ``filter_var()`` and ``mail()`` respectively. - - :doc:`CAPTCHA Helper ` :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. - Database diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index ca24e011f..27e11e6f8 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -64,7 +64,15 @@ Once loaded you can generate a captcha like this:: 'img_height' => 30, 'expiration' => 7200, 'word_length' => 8, - 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', + + // White background and border, black text and shadow, red grid + 'colors' => array( + 'background' => array(255, 255, 255), + 'border' => array(255, 255, 255), + 'text' => array(0, 0, 0), + 'grid' => array(255, 40, 40) + ) ); $cap = create_captcha($vals); @@ -82,6 +90,7 @@ Once loaded you can generate a captcha like this:: in the captcha folder before it will be deleted. The default is two hours. - **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +- If any of the **colors** values is missing, it will be replaced by the default. Adding a Database ----------------- -- cgit v1.2.3-24-g4f1b