├── .gitignore ├── backgrounds ├── kinda-jean.png ├── white-wave.png ├── cloth-alike.png ├── grey-sandbag.png ├── stitched-wool.png ├── white-carbon.png ├── polyester-lite.png └── 45-degree-fabric.png ├── fonts └── times_new_yorker.ttf ├── composer.json ├── README.md ├── LICENSE.md ├── index.php └── simple-php-captcha.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | simple-php-captcha.sublime-project 3 | simple-php-captcha.sublime-workspace -------------------------------------------------------------------------------- /backgrounds/kinda-jean.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/kinda-jean.png -------------------------------------------------------------------------------- /backgrounds/white-wave.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/white-wave.png -------------------------------------------------------------------------------- /fonts/times_new_yorker.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/fonts/times_new_yorker.ttf -------------------------------------------------------------------------------- /backgrounds/cloth-alike.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/cloth-alike.png -------------------------------------------------------------------------------- /backgrounds/grey-sandbag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/grey-sandbag.png -------------------------------------------------------------------------------- /backgrounds/stitched-wool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/stitched-wool.png -------------------------------------------------------------------------------- /backgrounds/white-carbon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/white-carbon.png -------------------------------------------------------------------------------- /backgrounds/polyester-lite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/polyester-lite.png -------------------------------------------------------------------------------- /backgrounds/45-degree-fabric.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yasirmturk/simple-php-captcha/HEAD/backgrounds/45-degree-fabric.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abeautifulsite/simple-php-captcha", 3 | "description": "A simple PHP CAPTCHA script", 4 | "version": "1.0.1", 5 | "homepage": "http://www.abeautifulsite.net/", 6 | "license": "MIT", 7 | "autoload": { 8 | "files": ["simple-php-captcha.php"] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A simple PHP CAPTCHA script 2 | 3 | _Written by Cory LaViska for A Beautiful Site, LLC. (http://abeautifulsite.net/)_ 4 | 5 | _Licensed under the MIT license: http://opensource.org/licenses/MIT_ 6 | 7 | ## Demo and Usage 8 | 9 | http://labs.abeautifulsite.net/simple-php-captcha/ 10 | 11 | ## Attribution 12 | 13 | - Special thanks to Subtle Patterns for the patterns used for default backgrounds: http://subtlepatterns.com/ 14 | - Special thanks to dafont.com for providing Times New Yorker: http://www.dafont.com/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 A Beautiful Site, LLC 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | Example » A simple PHP CAPTCHA script 13 | 25 | 26 | 27 |

28 | CAPTCHA Example 29 |

30 | 31 |

Usage

32 | 33 |

34 | The following code will prepare a CAPTCHA image and keep the code in a session 35 | variable for later use: 36 |

37 | 38 |
 39 | <?php
 40 | session_start();
 41 | include("simple-php-captcha.php");
 42 | $_SESSION['captcha'] = simple_php_captcha();
 43 | ?>
 44 | 
45 | 46 |

47 | After the call to simple_php_captcha() above, 48 | $_SESSION['captcha'] will be something like this: 49 |

50 | 51 |
 52 | 
 55 | 
56 | 57 |

58 | To display the CAPTCHA image, create an HTML <img> using 59 | $_SESSION['captcha']['image_src'] as the src attribute: 60 |

61 | 62 |

63 | '; 65 | 66 | ?> 67 |

68 | 69 |

70 | To verify the CAPTCHA value on the next page load (or in an AJAX request), test 71 | against $_SESSION['captcha']['code']. You can use 72 | strtolower() or strtoupper() to perform a 73 | case-insensitive match. 74 |

75 | 76 |

Configuration

77 |

78 | Configuration is easy and all values are optional. To specify one or more options, 79 | do this: 80 |

81 | 82 |
 83 | <?php
 84 | 
 85 | $_SESSION['captcha'] = simple_php_captcha( array(
 86 |     'min_length' => 5,
 87 |     'max_length' => 5,
 88 |     'backgrounds' => array(image.png', ...),
 89 |     'fonts' => array('font.ttf', ...),
 90 |     'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
 91 |     'min_font_size' => 28,
 92 |     'max_font_size' => 28,
 93 |     'color' => '#666',
 94 |     'angle_min' => 0,
 95 |     'angle_max' => 10,
 96 |     'shadow' => true,
 97 |     'shadow_color' => '#fff',
 98 |     'shadow_offset_x' => -1,
 99 |     'shadow_offset_y' => 1
100 | ));
101 | 
102 | >
103 | 
104 | 105 |

Notes

106 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /simple-php-captcha.php: -------------------------------------------------------------------------------- 1 | '', 22 | 'min_length' => 5, 23 | 'max_length' => 5, 24 | 'backgrounds' => array( 25 | $bg_path . '45-degree-fabric.png', 26 | $bg_path . 'cloth-alike.png', 27 | $bg_path . 'grey-sandbag.png', 28 | $bg_path . 'kinda-jean.png', 29 | $bg_path . 'polyester-lite.png', 30 | $bg_path . 'stitched-wool.png', 31 | $bg_path . 'white-carbon.png', 32 | $bg_path . 'white-wave.png' 33 | ), 34 | 'fonts' => array( 35 | $font_path . 'times_new_yorker.ttf' 36 | ), 37 | 'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789', 38 | 'min_font_size' => 28, 39 | 'max_font_size' => 28, 40 | 'color' => '#666', 41 | 'angle_min' => 0, 42 | 'angle_max' => 10, 43 | 'shadow' => true, 44 | 'shadow_color' => '#fff', 45 | 'shadow_offset_x' => -1, 46 | 'shadow_offset_y' => 1 47 | ); 48 | 49 | // Overwrite defaults with custom config values 50 | if( is_array($config) ) { 51 | foreach( $config as $key => $value ) $captcha_config[$key] = $value; 52 | } 53 | 54 | // Restrict certain values 55 | if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1; 56 | if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0; 57 | if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10; 58 | if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min']; 59 | if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10; 60 | if( $captcha_config['max_font_size'] < $captcha_config['min_font_size'] ) $captcha_config['max_font_size'] = $captcha_config['min_font_size']; 61 | 62 | // Generate CAPTCHA code if not set by user 63 | if( empty($captcha_config['code']) ) { 64 | $captcha_config['code'] = ''; 65 | $length = mt_rand($captcha_config['min_length'], $captcha_config['max_length']); 66 | while( strlen($captcha_config['code']) < $length ) { 67 | $captcha_config['code'] .= substr($captcha_config['characters'], mt_rand() % (strlen($captcha_config['characters'])), 1); 68 | } 69 | } 70 | 71 | // Generate HTML for image src 72 | if ( strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) ) { 73 | $image_src = substr(__FILE__, strlen( realpath($_SERVER['DOCUMENT_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime()); 74 | $image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/'); 75 | } else { 76 | $_SERVER['WEB_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']); 77 | $image_src = substr(__FILE__, strlen( realpath($_SERVER['WEB_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime()); 78 | $image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/'); 79 | } 80 | 81 | $_SESSION['_CAPTCHA']['config'] = serialize($captcha_config); 82 | 83 | return array( 84 | 'code' => $captcha_config['code'], 85 | 'image_src' => $image_src 86 | ); 87 | 88 | } 89 | 90 | 91 | if( !function_exists('hex2rgb') ) { 92 | function hex2rgb($hex_str, $return_string = false, $separator = ',') { 93 | $hex_str = preg_replace("/[^0-9A-Fa-f]/", '', $hex_str); // Gets a proper hex string 94 | $rgb_array = array(); 95 | if( strlen($hex_str) == 6 ) { 96 | $color_val = hexdec($hex_str); 97 | $rgb_array['r'] = 0xFF & ($color_val >> 0x10); 98 | $rgb_array['g'] = 0xFF & ($color_val >> 0x8); 99 | $rgb_array['b'] = 0xFF & $color_val; 100 | } elseif( strlen($hex_str) == 3 ) { 101 | $rgb_array['r'] = hexdec(str_repeat(substr($hex_str, 0, 1), 2)); 102 | $rgb_array['g'] = hexdec(str_repeat(substr($hex_str, 1, 1), 2)); 103 | $rgb_array['b'] = hexdec(str_repeat(substr($hex_str, 2, 1), 2)); 104 | } else { 105 | return false; 106 | } 107 | return $return_string ? implode($separator, $rgb_array) : $rgb_array; 108 | } 109 | } 110 | 111 | // Draw the image 112 | if( isset($_GET['_CAPTCHA']) ) { 113 | 114 | session_start(); 115 | 116 | $captcha_config = unserialize($_SESSION['_CAPTCHA']['config']); 117 | if( !$captcha_config ) exit(); 118 | 119 | if( isset($_GET['_RENDER']) ) { 120 | $_SESSION['_CAPTCHA'] = simple_php_captcha(); 121 | } else { 122 | unset($_SESSION['_CAPTCHA']); 123 | } 124 | 125 | // Pick random background, get info, and start captcha 126 | $background = $captcha_config['backgrounds'][mt_rand(0, count($captcha_config['backgrounds']) -1)]; 127 | list($bg_width, $bg_height, $bg_type, $bg_attr) = getimagesize($background); 128 | 129 | $captcha = imagecreatefrompng($background); 130 | 131 | $color = hex2rgb($captcha_config['color']); 132 | $color = imagecolorallocate($captcha, $color['r'], $color['g'], $color['b']); 133 | 134 | // Determine text angle 135 | $angle = mt_rand( $captcha_config['angle_min'], $captcha_config['angle_max'] ) * (mt_rand(0, 1) == 1 ? -1 : 1); 136 | 137 | // Select font randomly 138 | $font = $captcha_config['fonts'][mt_rand(0, count($captcha_config['fonts']) - 1)]; 139 | 140 | // Verify font file exists 141 | if( !file_exists($font) ) throw new Exception('Font file not found: ' . $font); 142 | 143 | //Set the font size. 144 | $font_size = mt_rand($captcha_config['min_font_size'], $captcha_config['max_font_size']); 145 | $text_box_size = imagettfbbox($font_size, $angle, $font, $captcha_config['code']); 146 | 147 | // Determine text position 148 | $box_width = abs($text_box_size[6] - $text_box_size[2]); 149 | $box_height = abs($text_box_size[5] - $text_box_size[1]); 150 | $text_pos_x_min = 0; 151 | $text_pos_x_max = ($bg_width) - ($box_width); 152 | $text_pos_x = mt_rand($text_pos_x_min, $text_pos_x_max); 153 | $text_pos_y_min = $box_height; 154 | $text_pos_y_max = ($bg_height) - ($box_height / 2); 155 | if ($text_pos_y_min > $text_pos_y_max) { 156 | $temp_text_pos_y = $text_pos_y_min; 157 | $text_pos_y_min = $text_pos_y_max; 158 | $text_pos_y_max = $temp_text_pos_y; 159 | } 160 | $text_pos_y = mt_rand($text_pos_y_min, $text_pos_y_max); 161 | 162 | // Draw shadow 163 | if( $captcha_config['shadow'] ){ 164 | $shadow_color = hex2rgb($captcha_config['shadow_color']); 165 | $shadow_color = imagecolorallocate($captcha, $shadow_color['r'], $shadow_color['g'], $shadow_color['b']); 166 | imagettftext($captcha, $font_size, $angle, $text_pos_x + $captcha_config['shadow_offset_x'], $text_pos_y + $captcha_config['shadow_offset_y'], $shadow_color, $font, $captcha_config['code']); 167 | } 168 | 169 | // Draw text 170 | imagettftext($captcha, $font_size, $angle, $text_pos_x, $text_pos_y, $color, $font, $captcha_config['code']); 171 | 172 | // Output image 173 | header("Content-type: image/png"); 174 | imagepng($captcha); 175 | 176 | } 177 | --------------------------------------------------------------------------------