Captcha Generator using PHP
Submitted by jaredgwapo on Wednesday, January 13, 2016 - 10:50.
Language
In this tutorial we are going to learn how to make a captcha generator that you can use in login, sign up, comment and etc. Just download the source code below.
Thank you for viewing this article. If you have any comments, suggestions, questions just comment below or email me at [email protected].
DIRECTIONS
Create index.php
- <?php
- session_start();
- $_SESSION = array();
- include("simple-php-captcha.php");
- $_SESSION['captcha'] = simple_php_captcha();
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <style type="text/css">
- pre {
- border: solid 1px #bbb;
- padding: 10px;
- margin: 2em;
- }
- img {
- border: solid 1px #ccc;
- margin: 0 2em;
- }
- </style>
- </head>
- <body>
- <h1>
- CAPTCHA Example
- </h1>
- <p>
- <?php
- echo '<img src="' . $_SESSION['captcha']['image_src'] . '" alt="CAPTCHA code">';
- ?>
- </p>
- </body>
- </html>
Create simple-php-captcha.php
- <?php
- //
- // A simple PHP CAPTCHA script
- //
- // Copyright 2011 by Cory LaViska for A Beautiful Site, LLC
- //
- // See readme.md for usage, demo, and licensing info
- //
- // Check for GD library
- throw new Exception('Required GD library is missing');
- }
- // Default values
- 'code' => '',
- 'min_length' => 5,
- 'max_length' => 5,
- $bg_path . '45-degree-fabric.png',
- $bg_path . 'cloth-alike.png',
- $bg_path . 'grey-sandbag.png',
- $bg_path . 'kinda-jean.png',
- $bg_path . 'polyester-lite.png',
- $bg_path . 'stitched-wool.png',
- $bg_path . 'white-carbon.png',
- $bg_path . 'white-wave.png'
- ),
- $font_path . 'times_new_yorker.ttf'
- ),
- 'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789',
- 'min_font_size' => 28,
- 'max_font_size' => 28,
- 'color' => '#666',
- 'angle_min' => 0,
- 'angle_max' => 10,
- 'shadow' => true,
- 'shadow_color' => '#fff',
- 'shadow_offset_x' => -1,
- 'shadow_offset_y' => 1
- );
- // Overwrite defaults with custom config values
- foreach( $config as $key => $value ) $captcha_config[$key] = $value;
- }
- // Restrict certain values
- if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1;
- if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0;
- if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10;
- if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min'];
- if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10;
- if( $captcha_config['max_font_size'] < $captcha_config['min_font_size'] ) $captcha_config['max_font_size'] = $captcha_config['min_font_size'];
- // Generate CAPTCHA code if not set by user
- $captcha_config['code'] = '';
- }
- }
- // Generate HTML for image src
- } else {
- }
- 'code' => $captcha_config['code'],
- 'image_src' => $image_src
- );
- }
- function hex2rgb($hex_str, $return_string = false, $separator = ',') {
- $rgb_array['r'] = 0xFF & ($color_val >> 0x10);
- $rgb_array['g'] = 0xFF & ($color_val >> 0x8);
- $rgb_array['b'] = 0xFF & $color_val;
- } else {
- return false;
- }
- }
- }
- // Draw the image
- // Pick random background, get info, and start captcha
- $color = hex2rgb($captcha_config['color']);
- // Determine text angle
- // Select font randomly
- // Verify font file exists
- //Set the font size.
- // Determine text position
- $text_pos_x_min = 0;
- $text_pos_x_max = ($bg_width) - ($box_width);
- $text_pos_y_min = $box_height;
- $text_pos_y_max = ($bg_height) - ($box_height / 2);
- if ($text_pos_y_min > $text_pos_y_max) {
- $temp_text_pos_y = $text_pos_y_min;
- $text_pos_y_min = $text_pos_y_max;
- $text_pos_y_max = $temp_text_pos_y;
- }
- // Draw shadow
- if( $captcha_config['shadow'] ){
- $shadow_color = hex2rgb($captcha_config['shadow_color']);
- $shadow_color = imagecolorallocate($captcha, $shadow_color['r'], $shadow_color['g'], $shadow_color['b']);
- 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']);
- }
- // Draw text
- imagettftext($captcha, $font_size, $angle, $text_pos_x, $text_pos_y, $color, $font, $captcha_config['code']);
- // Output image
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 562 views