Lucky's Captcha v4
Another captcha by me 
This captcha consists of three completely random generated drawings, and each of the drawing contains has a code behind it. The drawings are painted on the image on the right side. One of the three drawings is drawings is painted on the left side of the image. The visitor has to enter te code which matches the drawing on the right.
Download script as zip
Tags:
gd
php

This captcha consists of three completely random generated drawings, and each of the drawing contains has a code behind it. The drawings are painted on the image on the right side. One of the three drawings is drawings is painted on the left side of the image. The visitor has to enter te code which matches the drawing on the right.
Download script as zip
Source
- captcha.class.php
- captcha.php
- <?php
- /**
- * Lucky's Framework
- * A highly extendable MVC PHP framework
- *
- * Created by Lucas van Dijk (http://www.return1.net)
- * Copyright 2007 by Lucas van Dijk
- *
- * $Id$
- *
- * ---------------------------------------------------------------------
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /**
- * Represents an image.
- *
- * @package Images
- * @author Lucas van Dijk
- */
- class Image
- {
- /**
- * Holds the image GD resource
- *
- * @var resource
- */
- protected $image_resource;
- /**
- * Holds the width of the image
- *
- * @var int
- */
- protected $width;
- /**
- * Holds the height of the image
- *
- * @var int
- */
- protected $height;
- /**
- * Constructor, creates the image resource
- * @param mixed $param1 The filename to open, or the width of the image
- * @param int $param2 The height of the image, when creating a new image (not from a file)
- */
- public function __construct($param1, $param2 = null)
- {
- {
- // Just one parameter given,
- // Param1 should be a filename
- $this -> image_resource = $this -> create_image($param1);
- }
- else
- {
- // Param 2 is given
- // So we create a new image with the width and height
- }
- if(!$this -> image_resource)
- {
- throw new Exception('Could not create image', 100);
- }
- $this -> update_size();
- }
- /**
- * Creates an GD image resource based on image type
- * @param string $file The file to open
- * @return resource
- */
- protected function create_image($file)
- {
- $img = false;
- switch($image_type)
- {
- case 3:
- $img = imagecreatefrompng($file);
- break;
- case 2:
- $img = imagecreatefromjpeg($file);
- break;
- case 1:
- $img = imagecreatefromgif($file);
- break;
- case 6:
- $img = imagecreatefromwbmp($file);
- break;
- default:
- throw new Exception('File is not a valid Image', 101);
- }
- return $img;
- }
- /**
- * Allocates a color, based on a hex color
- * @param string $color The hex color to allocate
- * @param int The background color
- */
- protected function allocate_color($color)
- {
- {
- }
- $parts = str_split($color, 2);
- return eval('return imagecolorallocate($this -> image_resource, 0x'.$parts[0].', 0x'.$parts[1].', 0x'.$parts[2].');');
- }
- /**
- * Generates a random hex color
- * @param bool $return_array return an array with three elememts containing each a part of the color, default false
- * @return string|array The random hex color
- */
- protected function random_hex_color($return_array = false)
- {
- $rand_color = '#';
- for($i = 0; $i < 6; $i++)
- {
- }
- if($return_array)
- {
- return $rand_color_array;
- }
- return $rand_color;
- }
- /**
- * Generates a random RGB Color
- * @return array with key 0 as red, key 1 as green, and key 2 as blue
- */
- protected function random_rgb_color()
- {
- for ($c = 0; $c < 3; $c++)
- {
- }
- return $color;
- }
- /**
- * Updates the width and height of the image
- */
- protected function update_size()
- {
- $this -> width = imagesx($this -> image_resource);
- $this -> height = imagesy($this -> image_resource);
- }
- /**
- * Returns the image resource
- * @return resource
- */
- public function get_resource()
- {
- return $this -> image_resource;
- }
- /**
- * Copies an Image object into the current image
- * @param Image $image the image object to copy
- * @param int $x The X position
- * @param int $y The Y psoition
- */
- {
- imagecopy($this -> image_resource, $image -> get_resource(), $x, $y, 0, 0, $image -> get_width() - 1, $image -> get_height() - 1);
- }
- /**
- * Resizes the image
- * @param int $max_width max width of the image
- * @param int $max_height max height of the image
- * @param bool $keep_aspect_ratio True if you wnat to keep the aspect ratio
- */
- public function resize($max_width, $max_height, $keep_aspect_ratio = true)
- {
- if($keep_aspect_ratio)
- {
- // Find resize scale
- $new_width = $resize_scale * $this -> width;
- $new_height = $resize_scale * $this -> height;
- }
- else
- {
- $new_width = $max_width;
- $new_height = $max_height;
- }
- $new_img = imagecreatetruecolor($new_width, $new_height);
- // Resize it
- imagecopyresampled($new_img, $this -> image_resource, 0, 0, 0, 0, $new_width, $new_height, $this -> width, $this -> height);
- $this -> image_resource = $new_img;
- $this -> update_size();
- }
- /**
- * Outputs the image to the screen
- *
- * This method outputs the current image to the screen.
- * Note: This function does <b>not</b> send the right Content-Type
- * for the image, you'll have to do that by yourself
- * @param string $type The image type to output
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function show_image($type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, null, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource);
- break;
- default:
- imagepng($this -> image_resource);
- }
- }
- /**
- * Saves the image to a specific path
- * @param string $file The path where to save to
- * @param string $type The image type
- * @param int $quality The quality of the image (only needed for JPEG images)
- */
- public function save_to_file($file, $type = "png", $quality = 70)
- {
- switch($type)
- {
- case "png":
- imagepng($this -> image_resource, $file);
- break;
- case "jpg":
- case "jpeg":
- imagejpeg($this -> image_resource, $file, $quality);
- break;
- case "gif":
- imagegif($this -> image_resource, $file);
- break;
- default:
- imagepng($this -> image_resource, $file);
- }
- }
- /**
- * Returns the width of the image
- *
- * @return int
- */
- public function get_width()
- {
- return $this -> width;
- }
- /**
- * Returns the height of the image
- *
- * @return int
- */
- public function get_height()
- {
- return $this -> height;
- }
- public function __toString()
- {
- return imagepng($this -> image_resource);
- }
- }
- /**
- * Represents the captcha image
- * It generates a number of drawings,
- * paints it on the main image,
- * and sets the captcha code
- * @package Images
- * @author Lucas van Dijk
- */
- class Captcha extends Image
- {
- protected $font;
- protected $number;
- protected $code_length;
- protected $code;
- /**
- * Constructor, sets the captcha parameters, and inits the image
- * @param string $font The TTF font file used in the image, use ./font.ttf (with ./) ti use a font in the current directory
- * @param int $number_of_drawings The number of drawings you want to generate
- * @param int $code_length The length of the catcha codes
- */
- public function __construct($number_of_drawings = 2, $code_length = 5)
- {
- {
- throw new InvalidArgumentException('Invalid parameters');
- }
- $this -> code_length = $code_length;
- $this -> number = $number_of_drawings;
- $height = 50 + ($number_of_drawings * 35);
- $width = 200 + (imagefontwidth(4) * $code_length) + 50;
- parent::__construct($width, $height);
- $this -> create();
- imagecolortransparent($this -> image_resource, $this -> allocate_color('FFFFFF'));
- }
- /**
- * Generates a random string
- * @param int $max_length The length of the generated string
- * @param bool $hash Hash the string with md5?
- * @return string The generated string
- */
- public function generate_random_string($max_length = 8, $hash = false)
- {
- $rand_str = '';
- for($i = 0; $i < $max_length; $i++)
- {
- }
- }
- /**
- * Returns the 'main' captcha code
- * @return string
- */
- public function get_code()
- {
- return $this -> code;
- }
- /**
- * Creates the image
- *
- * Adds a border to the image, the seperation line, and paints the drawings
- * on the image
- */
- protected function create()
- {
- imagefilledrectangle($this -> image_resource, 0, 0, $this -> get_width() - 1, $this -> get_height() - 1, $this -> allocate_color('FFFFFF'));
- imagerectangle($this -> image_resource, 0, 0, $this -> get_width() - 1, $this -> get_height() - 1, $this -> allocate_color('000000'));
- imageline($this -> image_resource, 150, 0, 150, $this -> get_height(), $this -> allocate_color('000000'));
- for($i = 0; $i < $this -> number; $i++)
- {
- $this -> add_drawing(new CaptchaDrawing());
- }
- // Determine the 'main' drawing
- $this -> code = $this -> drawings[$main][1];
- $this -> paint_drawings($main);
- }
- /**
- * This function paints the drawings on the main image
- * @param int $main The index of the 'main' image, which will be painted on the left
- */
- protected function paint_drawings($main)
- {
- // paint the main drawing on the image
- // Paint all drawings on the right of the image, with the code
- $i = 0;
- $x = 175;
- foreach($this -> drawings as $drawing)
- {
- $drawing[0] -> resize(30, 30);
- imagestring($this -> image_resource, 4, $x + 40, 15 + (50 * $i), $drawing[1], $this -> allocate_color('000000'));
- $i++;
- }
- }
- /**
- * Adds a drawing to the $drawings member, and generates a code for the drawing
- * @param CaptchaDrawing $drawing The drawing to add
- */
- protected function add_drawing(CaptchaDrawing $drawing)
- {
- }
- }
- /**
- * A simple drawing on the captcha images
- *
- * The drawing is generated completely random
- * @package Images
- * @author Lucas van Dijk
- */
- class CaptchaDrawing extends Image
- {
- /**
- * Constructor, generates the image immediatly
- * @param int $width The width of the drawing
- * @param int $height The height of the drawing
- */
- public function __construct($width = 100, $height = 100)
- {
- parent::__construct($width, $height);
- $this -> generate_random_drawing();
- }
- /**
- * Generates a completely random drawing
- */
- protected function generate_random_drawing()
- {
- imagefilledrectangle($this -> image_resource, 0, 0, $this -> get_width() - 1, $this -> get_height() - 1, $this -> allocate_color('FFFFFF'));
- $lines_drawed = 0;
- $min_width = $this -> get_width() / 100 * 15;
- $max_width = $this -> get_width() / 100 * 75;
- $min_height = $this -> get_height() / 100 * 15;
- $max_height = $this -> get_height() / 100 * 75;
- $rectangle_drawed = false;
- $circle_drawed = false;
- while(($rectangle_drawed == false || $circle_drawed == false) && $lines_drawed < $max_lines)
- {
- // generate random width/height
- // Generate the X and Y positions
- $max_x = $this -> get_width() - $width;
- $max_y = $this -> get_height() - $height;
- switch($what_to_draw)
- {
- case 1:
- if($filled)
- {
- imagefilledellipse($this -> image_resource, $x, $y, $width, $height, $this -> allocate_color($this -> random_hex_color()));
- }
- else
- {
- imageellipse($this -> image_resource, $x, $y, $width, $height, $this -> allocate_color($this -> random_hex_color()));
- }
- $circle_drawed = true;
- break;
- case 2:
- if($filled)
- {
- imagefilledrectangle($this -> image_resource, $x, $y, $x + $width, $y + $height, $this -> allocate_color($this -> random_hex_color()));
- }
- else
- {
- imagerectangle($this -> image_resource, $x, $y, $x + $width, $y + $height, $this -> allocate_color($this -> random_hex_color()));
- }
- $rectangle_drawed = true;
- break;
- case 3:
- case 4:
- // Draw a random line
- if($lines_drawed <= $max_lines)
- {
- imageline($this -> image_resource, $x, $y, $x2, $y2, $this -> allocate_color($this -> random_hex_color()));
- $lines_drawed++;
- }
- break;
- }
- }
- }
- }
- <?php
- include_once 'captcha.class.php';
- $captcha = new Captcha(3);

