captcha.php
4 months ago
font-1.ttf
12 years ago
font-2.ttf
12 years ago
font-3.ttf
12 years ago
font-4.ttf
12 years ago
captcha.php
172 lines
| 1 | <?php |
| 2 | /* |
| 3 | PHP Captcha by Codepeople.net |
| 4 | http://www.codepeople.net |
| 5 | */ |
| 6 | |
| 7 | if (!ini_get("zlib.output_compression")) ob_clean(); |
| 8 | |
| 9 | header("Cache-Control: no-store, no-cache, must-revalidate"); |
| 10 | header("Pragma: no-cache"); |
| 11 | |
| 12 | if (!isset($_GET["ps"])) $_GET["ps"] = ''; |
| 13 | if (!isset($_GET["bcolor"]) || $_GET["bcolor"] == '') $_GET["bcolor"] = "FFFFFF"; |
| 14 | if (!isset($_GET["border"]) || $_GET["border"] == '') $_GET["border"] = "FFFFFF"; |
| 15 | |
| 16 | //configuration |
| 17 | $imgX = min( ( isset($_GET["width"]) && is_numeric( $_GET["width"] ) )? intval($_GET["width"]) : "180" , 800); |
| 18 | $imgY = min( ( isset($_GET["height"]) && is_numeric( $_GET["height"] ) )? intval($_GET["height"]) : "60" , 600); |
| 19 | |
| 20 | $letter_count = min( ( isset($_GET["letter_count"]) && is_numeric( $_GET["letter_count"] ) )? intval($_GET["letter_count"]) : "5", 20); |
| 21 | $min_size = min( ( isset($_GET["min_size"]) && is_numeric( $_GET["min_size"] ) )? intval($_GET["min_size"]) : "35", 200); |
| 22 | $max_size = min( ( isset($_GET["max_size"]) && is_numeric( $_GET["max_size"] ) )? intval($_GET["max_size"]) : "45", 200); |
| 23 | $noise = min( ( isset($_GET["noise"]) && is_numeric( $_GET["noise"] ) )? intval($_GET["noise"]) : "200", 5000); |
| 24 | $noiselength = min( ( isset($_GET["noiselength"]) && is_numeric( $_GET["noiselength"] ) )? intval($_GET["noiselength"]) : "5", 50); |
| 25 | $bcolor = cpcff_decodeColor($_GET["bcolor"]); |
| 26 | $border = cpcff_decodeColor($_GET["border"]); |
| 27 | |
| 28 | $noisecolor = 0xcdcdcd; |
| 29 | $random_noise_color= true; |
| 30 | $tcolor = cpcff_decodeColor("666666"); |
| 31 | $random_text_color= true; |
| 32 | |
| 33 | function cpcff_decodeColor($hexcolor) |
| 34 | { |
| 35 | $color = (hexdec($hexcolor)); |
| 36 | $c["b"] = $color % 256; |
| 37 | $color = $color / 256; |
| 38 | $c["g"] = floor($color) % 256; |
| 39 | $color = $color / 256; |
| 40 | $c["r"] = floor($color) % 256; |
| 41 | return $c; |
| 42 | } |
| 43 | |
| 44 | function cpcff_similarColors($c1, $c2) |
| 45 | { |
| 46 | return sqrt( pow($c1["r"]-$c2["r"],2) + pow($c1["g"]-$c2["g"],2) + pow($c1["b"]-$c2["b"],2)) < 125; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | function bccf_captcha_sanitize_key( $key ) { |
| 51 | $key = strtolower( $key ); |
| 52 | $key = preg_replace( '/[^a-z0-9_\-]/', '', $key ); |
| 53 | |
| 54 | return $key; |
| 55 | } |
| 56 | |
| 57 | if (function_exists('session_start')) @session_start(); |
| 58 | |
| 59 | function cpcff_make_seed() { |
| 60 | list($usec, $sec) = explode(' ', microtime()); |
| 61 | return round((float) $sec + ((float) $usec * 100000)); |
| 62 | } |
| 63 | mt_srand(cpcff_make_seed()); |
| 64 | $randval = mt_rand(); |
| 65 | |
| 66 | $str = ""; |
| 67 | $length = 0; |
| 68 | for ($i = 0; $i < $letter_count; $i++) { |
| 69 | $str .= chr(mt_rand(97, 122))." "; |
| 70 | } |
| 71 | $_SESSION['rand_code'.bccf_captcha_sanitize_key($_GET["ps"])] = str_replace(" ", "", $str); |
| 72 | |
| 73 | setCookie('rand_code'.bccf_captcha_sanitize_key($_GET["ps"]), md5(str_replace(" ", "", $str)), time()+36000,"/"); |
| 74 | |
| 75 | $image = imagecreatetruecolor($imgX, $imgY); |
| 76 | $backgr_col = imagecolorallocate($image, $bcolor["r"],$bcolor["g"],$bcolor["b"]); |
| 77 | $border_col = imagecolorallocate($image, $border["r"],$border["g"],$border["b"]); |
| 78 | |
| 79 | if ($random_text_color) |
| 80 | { |
| 81 | do |
| 82 | { |
| 83 | $selcolor = mt_rand(0,256*256*256); |
| 84 | } while ( cpcff_similarColors(cpcff_decodeColor($selcolor), $bcolor) ); |
| 85 | $tcolor = cpcff_decodeColor($selcolor); |
| 86 | } |
| 87 | |
| 88 | $text_col = imagecolorallocate($image, $tcolor["r"],$tcolor["g"],$tcolor["b"]); |
| 89 | |
| 90 | |
| 91 | |
| 92 | switch (@$_GET["font"]) { |
| 93 | case "font-2.ttf": |
| 94 | $selected_font = "font-2.ttf"; |
| 95 | break; |
| 96 | case "font-3.ttf": |
| 97 | $selected_font = "font-3.ttf"; |
| 98 | break; |
| 99 | case "font-4.ttf": |
| 100 | $selected_font = "font-4.ttf"; |
| 101 | break; |
| 102 | default: |
| 103 | $selected_font = "font-1.ttf"; |
| 104 | } |
| 105 | |
| 106 | $font = dirname( __FILE__ ) . "/". $selected_font; |
| 107 | |
| 108 | |
| 109 | // 1. Create Image and Colors |
| 110 | $image = imagecreatetruecolor($imgX, $imgY); |
| 111 | imagealphablending($image, true); |
| 112 | imagesavealpha($image, true); |
| 113 | |
| 114 | $bgColor = imagecolorallocate($image, $bcolor['r'], $bcolor['g'], $bcolor['b']); |
| 115 | $borderColor = imagecolorallocate($image, $border['r'], $border['g'], $border['b']); |
| 116 | imagefill($image, 0, 0, $bgColor); |
| 117 | |
| 118 | // 1. Setup Margins |
| 119 | $leftPadding = 15; // Explicit safe zone for the first character |
| 120 | $rightPadding = 15; // Safe zone for the last character |
| 121 | $availableWidth = $imgX - ($leftPadding + $rightPadding); |
| 122 | |
| 123 | $charArray = str_split($str); |
| 124 | $totalChars = count($charArray); |
| 125 | $cellWidth = $availableWidth / $totalChars; |
| 126 | |
| 127 | // 2. Decorative Background Noise |
| 128 | for ($i = 0; $i < 6; $i++) { |
| 129 | $shapeAlpha = imagecolorallocatealpha($image, rand(220, 245), rand(220, 245), rand(220, 245), 90); |
| 130 | imagefilledellipse($image, rand(0, $imgX), rand(0, $imgY), rand(20, $imgX), rand(20, $imgY), $shapeAlpha); |
| 131 | } |
| 132 | |
| 133 | // 3. Render Characters with Safe Positioning |
| 134 | foreach ($charArray as $i => $char) { |
| 135 | $fontSize = rand($min_size, $max_size); |
| 136 | $angle = rand(-10, 10); |
| 137 | |
| 138 | $bbox = imagettfbbox($fontSize, $angle, $font, $char); |
| 139 | |
| 140 | // Calculate dimensions from bounding box |
| 141 | $charWidth = abs($bbox[4] - $bbox[0]); |
| 142 | $charHeight = abs($bbox[5] - $bbox[1]); |
| 143 | |
| 144 | // X = Base padding + (index * cell width) + centering within that cell |
| 145 | $x = $leftPadding + ($i * $cellWidth) + ($cellWidth - $charWidth) / 2; |
| 146 | |
| 147 | // Y = Centered vertically with a slight random jitter for modern feel |
| 148 | $y = ($imgY / 2) + ($charHeight / 2) - rand(-2, 2); |
| 149 | |
| 150 | $textColor = imagecolorallocatealpha($image, rand(40, 70), rand(40, 70), rand(80, 110), 0); |
| 151 | |
| 152 | imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $font, $char); |
| 153 | } |
| 154 | |
| 155 | // 4. Fine Grain Noise |
| 156 | for ($i = 0; $i < $noise; $i++) { |
| 157 | $noiseColor = imagecolorallocatealpha($image, 80, 80, 80, rand(90, 110)); |
| 158 | $x1 = rand(0, $imgX); |
| 159 | $y1 = rand(0, $imgY); |
| 160 | // Using noiselength to define the sprawl of the noise dots/lines |
| 161 | imageline($image, $x1, $y1, $x1 + rand(1, $noiselength), $y1 + rand(1, $noiselength), $noiseColor); |
| 162 | } |
| 163 | |
| 164 | // 5. Border and Output |
| 165 | imagerectangle($image, 0, 0, $imgX - 1, $imgY - 1, $borderColor); |
| 166 | |
| 167 | header('Content-Type: image/png'); |
| 168 | imagepng($image); |
| 169 | imagedestroy($image); |
| 170 | |
| 171 | exit; |
| 172 | ?> |