Source for file securimage.php
Documentation is available at securimage.php
* Project: Securimage: A PHP class for creating and managing form CAPTCHA images<br />
* File: securimage.php<br />
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.<br /><br />
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.<br /><br />
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br /><br />
* Any modifications to the library should be indicated clearly in the source code
* to inform users that the changes are not a part of the original software.<br /><br />
* If you found this script useful, please take a quick moment to rate it.<br />
* http://www.hotscripts.com/rate/49400.html Thanks.
* @link http://www.phpcaptcha.org Securimage PHP CAPTCHA
* @link http://www.phpcaptcha.org/latest.zip Download Latest Version
* @link http://www.phpcaptcha.org/Securimage_Docs/ Online Documentation
* @copyright 2007 Drew Phillips
* @author drew010 <drew@drew-phillips.com>
* @version 1.0.2 (January 6, 2008)
- Audible CAPTCHA Code wav files
- Create codes from a word list instead of random strings
- Added the ability to use a selected character set, rather than a-z0-9 only.
- Added the multi-color text option to use different colors for each letter.
- Switched to automatic session handling instead of using files for code storage
- Added GD Font support if ttf support is not available. Can use internal GD fonts or load new ones.
- Added the ability to set line thickness
- Added option for drawing arced lines over letters
- Added ability to choose image type for output
* Output images in JPEG format
* Output images in PNG format
* Output images in GIF format
* Must have GD >= 2.0.28!
* Securimage CAPTCHA Class.
* The desired width of the CAPTCHA image.
* The desired width of the CAPTCHA image.
* The image format for output.<br />
* Valid options: SI_IMAGE_PNG, SI_IMAGE_JPG, SI_IMAGE_GIF
* The length of the code to generate.
* The character set for individual characters in the image.<br />
* Letters are converted to uppercase.<br />
* The font must support the letters or there may be problematic substitutions.
var $charset = 'ABCDEFGHKLMNPRSTUVWYZ23456789';
//var $charset = '0123456789';
* Create codes using this word list
* @var string The path to the word list to use for creating CAPTCHA codes
* True to use a word list file instead of a random code
* Whether to use a GD font instead of a TTF font.<br />
* TTF offers more support and options, but use this if your PHP doesn't support TTF.<br />
* The GD font to use.<br />
* Internal gd fonts can be loaded by their number.<br />
* Alternatively, a file path can be given and the font will be loaded from file.
* The approximate size of the font in pixels.<br />
* This does not control the size of the font because that is determined by the GD font itself.<br />
* This is used to aid the calculations of positioning used by this class.<br />
// Note: These font options below do not apply if you set $use_gd_font to true with the exception of $text_color
* The path to the TTF font file to load.
* Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2)<br />
* The minimum angle in degrees, with 0 degrees being left-to-right reading text.<br />
* Higher values represent a counter-clockwise rotation.<br />
* For example, a value of 90 would result in bottom-to-top reading text.
* The minimum angle in degrees, with 0 degrees being left-to-right reading text.<br />
* Higher values represent a counter-clockwise rotation.<br />
* For example, a value of 90 would result in bottom-to-top reading text.
* The X-Position on the image where letter drawing will begin.<br />
* This value is in pixels from the left side of the image.
* Letters can be spaced apart at random distances.<br />
* This is the minimum distance between two letters.<br />
* This should be <i>at least</i> as wide as a font character.<br />
* Small values can cause letters to be drawn over eachother.<br />
* Letters can be spaced apart at random distances.<br />
* This is the maximum distance between two letters.<br />
* This should be <i>at least</i> as wide as a font character.<br />
* Small values can cause letters to be drawn over eachother.<br />
* The background color for the image.<br />
* This should be specified in HTML hex format.<br />
* Make sure to include the preceding # sign!
* The text color to use for drawing characters.<br />
* This value is ignored if $use_multi_text is set to true.<br />
* Make sure this contrasts well with the background color.<br />
* Specify the color in HTML hex format with preceding # sign
* @see Securimage::$use_multi_text
* Set to true to use multiple colors for each character.
* @see Securimage::$multi_text_color
* String of HTML hex colors to use.<br />
* Separate each possible color with commas.<br />
* Be sure to precede each value with the # sign.
* Set to true to make the characters appear transparent.
* @see Securimage::$text_transparency_percentage
* The percentage of transparency, 0 to 100.<br />
* A value of 0 is completely opaque, 100 is completely transparent (invisble)
* @see Securimage::$use_transparent_text
* Draw vertical and horizontal lines on the image.
* @see Securimage::$line_color
* @see Securimage::$line_distance
* @see Securimage::$line_thickness
* @see Securimage::$draw_lines_over_text
* The color of the lines drawn on the image.<br />
* Use HTML hex format with preceding # sign.
* @see Securimage::$draw_lines
* How far apart to space the lines from eachother in pixels.
* @see Securimage::$draw_lines
* How thick to draw the lines in pixels.<br />
* 1-3 is ideal depending on distance
* @see Securimage::$draw_lines
* @see Securimage::$line_distance
* Set to true to draw angled lines on the image in addition to the horizontal and vertical lines.
* @see Securimage::$draw_lines
* Draw the lines over the text.<br />
* If fales lines will be drawn before putting the text on the image.<br />
* This can make the image hard for humans to read depending on the line thickness and distance.
* For added security, it is a good idea to draw arced lines over the letters to make it harder for bots to segment the letters.<br />
* Two arced lines will be drawn over the text on each side of the image.<br />
* This is currently expirimental and may be off in certain configurations.
* The colors or color of the arced lines.<br />
* Use HTML hex notation with preceding # sign, and separate each value with a comma.<br />
* This should be similar to your font color for single color images.
* Full path to the WAV files to use to make the audio files, include trailing /.<br />
* Name Files [A-Z0-9].wav
//There should be no need to edit below unless you really know what you are doing.
* The background image resource
* The code generated by the script
* The code that was entered by the user
* Whether or not the correct code was entered
* Class constructor.<br />
* Because the class uses sessions, this will attempt to start a session if there is no previous one.<br />
* If you do not start a session before calling the class, the constructor must be called before any
* output is sent to the browser.
* $securimage = new Securimage();
if ( session_id() == '' ) { // no session has been started yet, which is needed for validation
* Generate a code and output the image to the browser.
* include 'securimage.php';
* $securimage = new Securimage();
* $securimage->show('bg.jpg');
* @param string $background_image The path to an image to use as the background for the CAPTCHA
function show($background_image = "")
if($background_image != "" && is_readable($background_image)) {
$this->bgimg = $background_image;
* Validate the code entered by the user.
* $code = $_POST['code'];
* if ($securimage->check($code) == false) {
* die("Sorry, the code entered did not match.");
* @param string $code The code the user entered
* @return boolean true if the code was correct, false if not
$this->code_entered = $code;
return $this->correct_code;
* Generate and output the image
} else { //no transparency
if($this->bgimg != "") { $this->setBackground(); }
* Set the background of the CAPTCHA image
if($dat == false) { return; }
* Draw arced lines over the text
if ( rand(0,100) % 2 == 0 ) {
$end = $start + rand(75, 110);
imagearc($this->im, $xpos, $ypos, $width, $height, $start, $end, $linecolor);
if ( rand(1,75) % 2 == 0 ) {
$end = $start + rand(75, 100);
imagearc($this->im, $this->image_width * .75, $ypos, $width, $height, $start, $end, $linecolor);
* Draw lines on the image
* Draw the CAPTCHA code over the image
} else { //gd font identifier
|