Captcha Class
This is an captcha class which displays some random characters, in different colors.
The user has to enter all characters in blue. If there are no blue characters, the user must enter a dash (-).
Download script as zip
Tags:
GD
PHP
The user has to enter all characters in blue. If there are no blue characters, the user must enter a dash (-).
Download script as zip
Source
- captcha.class.php
- captcha.php
- <?php
- /**
- * Lucky's Captcha v3
- *
- * This captcha shows some random numbers on an image, in different colors,
- * the user has to enter the number of digits in a specific
- *
- * Copyright 2007 by Lucas van Dijk (info@return1.net)
- * For more info see http://www.return1.net
- *
- * @package Captcha
- * @category Captcha
- * @license http://www.opensource.org/licenses/gpl-license.php
- * @author Lucas van Dijk
- */
- class Captcha
- {
- protected $image;
- protected $image_size;
- protected $colors;
- protected $code;
- public function __construct($size = 120)
- {
- {
- throw new Exception('The session has not yet been started, please use the function session_start() before calling this class.');
- }
- // Create a new GD resource
- $this -> image = imagecreatetruecolor($size, $size);
- $this -> image_size = $size;
- // Fill the image with a white background
- $color = $this -> allocate_color('FFFFFF');
- imagefill($this -> image, 0, 0, $color);
- }
- private function __toString()
- {
- return $this -> code;
- }
- protected function allocate_color($color)
- {
- {
- }
- {
- return $this -> colors[$color];
- }
- else
- {
- $parts = str_split($color, 2);
- eval('$this -> colors[$color] = imagecolorallocate($this -> image, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
- return $this -> colors[$color];
- }
- }
- public function generate()
- {
- // Generate some lines, to divide the image in some smaller pieces
- $color = $this -> allocate_color('000000');
- for($i = 0; $i < $number_of_columns; $i++)
- {
- // Vertical line
- imageline($this -> image, 0, $i * $square_width, imagesx($this -> image), $i * $square_width, $color);
- // Horizontal Line
- imageline($this -> image, $i * $square_width, 0, $i * $square_width, imagesy($this -> image) - 1, $color);
- }
- imageline($this -> image, 0, imagesy($this -> image) - 1, imagesx($this -> image) - 1, imagesy($this -> image) - 1, $color);
- imageline($this -> image, imagesx($this -> image) - 1, 0, imagesx($this -> image) - 1, imagesy($this -> image) - 1, $color);
- // Add the numbers to the squares
- // Colors we're going to use
- $colors[] = 'FF0000'; // red
- $colors[] = '00FF00'; // green
- $colors[] = '0000FF'; // blue
- $colors[] = 'FF9900'; // Orange
- $row = 0;
- $column = 0;
- while($row < $number_of_columns)
- {
- // Calculate X and Y
- $x = (($square_width / 2) - (imagefontwidth(2) / 2)) + ($column * $square_width);
- $y = (($square_width / 2) - (imagefontheight(2) / 2)) + ($row * $square_width);
- // Select a color we're going to use
- $color = $this -> allocate_color($colors[$rand_color]);
- if($colors[$rand_color] == '0000FF')
- {
- $this -> code .= $char;
- }
- imagestring($this -> image, 2, $x, $y, $char, $color);
- $column++;
- if($column == $number_of_columns)
- {
- $row++;
- $column = 0;
- }
- }
- }
- public function show()
- {
- imagepng($this -> image);
- }
- public function get_code()
- {
- return $this -> code;
- }
- }
- <?php
- include 'includes/captcha.class.php';
- $captcha = new Captcha();
- $captcha -> generate();
- $captcha -> show();

