Jump to content

image captcha


Destramic

Recommended Posts

hey guys im trying to make an image captcha script for the fisrt time im having a problem getting it to work...its returning

 

Fatal error: Call to undefined function imagecreate() in C:\www\functions\captcha _image_verification.php on line 41

 

does anyone know why...im sure im using the right syntex

 

<?php
function image_captcha_verification()
{
$font_size         = "14";
$border_colour     = "239, 239, 239";
	$background_colour = "255, 255, 255, 127";
	$text_colour       = "191, 120, 120";
	$font              = "verdana.ttf";
$width             = "100";
$height            = "100";

$words = array ("police", "time", "broadway", "is", "throughout", "hateful", "action", "duvet", "tailcoat", "the",
                "danger", "minster", "army", "dent", "share", "beard", "lazy", "year", "must", "resigned", "angry",
                "relapse", "trump", "union", "adventure", "honey", "inject", "special", "end", "crossing", "slow",
                "grunge", "prize", "of", "dreamer", "issues", "tomcat", "riverbed", "founder", "working", "mask",
                "spinned", "reported", "meets", "john", "by", "vogue", "said", "jumpy", "democratic", "kingdom",
                "combs", "moments", "peaks", "rise", "innocent", "chalked", "from", "relations", "removing", 
                "platform", "unwillingness", "secretary", "computer", "arranger", "mentor", "was", "quote", "warn",
                "rally", "squadron", "profiles", "immature", "in", "drives", "train", "intrude", "state", "defense",
                "past", "plunges", "cry", "cameras", "promises", "outskirt", "subtitles", "hidden", "logic", "advertisement",
                "exact", "dimond", "risk", "director", "back", "sinatra", "store", "stopgap", "equal", "housing",
                "airport", "him", "security", "one", "dodges", "development", "entertainer", "ignition", "lovebird", "counts",
                "world", "protocol", "than", "cry", "shannon", "shear", "drain", "blues", "morning", "fronting", "executive",
                    "person", "reached", "taxplayer", "stranger", "schedules", "preston", "historic", "featured", "port", "tricked",
                "announced", "can", "time", "loadable", "wallets", "wounded", "core", "empire", "towers", "wished", "confused",
                "who", "hurdles", "toggles", "martin", "defender", "plane", "decked", "but", "harp", "ribs", "helpers", "least",
                "column", "cement", "room", "tension", "clipper", "shudder", "dangles", "storm", "farms", "zero", "affairs",
                "maniacs", "proposals", "two", "surrounded", "bone", "difficult", "dusters", "sailed", "bonding", "speak",
                "however", "sands", "scribble", "maiden", "mouths", "arms", "coopered", "learned", "recieved", "rocks", "tripod",
                "skull", "dwarf", "number", "arrested", "squish", "singer", "buttered", "found", "new", "million", "token", "combo",
                "completed", "national", "markets", "because", "lower", "trainers", "chief", "angle", "seabed", "trickled", "firsy",
                "undated", "ludlow", "stones", "joker", "monster", "reach", "outlook", "aware", "among", "comfort", "living", "contact",
                "french", "thailand", "open", "yelps", "delayed", "smart", "metropolitan", "meter", "snow", "castle", "tools", "irrigration",
                "new", "gas", "sidestepping", "drys", "cartoons", "declared", "numskull", "brass", "sped", "washington", "landing", 
                "ordeal", "cops", "quiet", "strike", "destruct", "wrote", "mamma", "tunes", "balanced", "officers", "books", "flame",
                    "am", "fishing", "not", "inspires", "beehives", "hypnotic", "flops", "sandhurst", "invoked", "bullion", "christmas", "muttered",
                "moats", "baggy", "ginger", "obligated", "hangover", "building", "midland", "mocks", "heavy", "don", "quotable", "clarkson",
                "vary", "enriches", "romania", "sumo", "ushering", "votes", "cool", "resumed", "sprayers", "convents", "unchanging", "prove",
                "renshaw", "holy", "recited", "kingpins", "valiant", "yesterday", "withdrawel", "itself", "slog", "marry", "newborns");

$image      = imagecreate($width, $height);
$background = imagecolorallocatealpha($image, $background_colour);
$text       = imagecolorallocate($image, $text_colour);

imagefill($image, 0, 0, $background);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $border_colour);

$string       = rand($words);
$angl         = "5";
$x_coordinate = "24";
$y_coordinate = "24";

imagettftext($image, $font_size, $angle, $x_coordinate, $y_coordinate, $text_colour, $font, $string);

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}

image_captcha_verification();
?>

Link to comment
Share on other sites

thanks alex that was the problem thanks...now the other problem im having is that the background is black on the image and i cant see no text

 

idealy i need the image to be transparant if possible

 

<?php
function image_captcha_verification()
{
$font_size         = "14";
$border_colour     = "239, 239, 239";
	$background_colour = "255, 255, 255, 127";
	$text_colour       = "191, 120, 120";
	$font              = "verdana.ttf";
$width             = "350";
$height            = "80";

$words = array ("police", "time", "broadway", "is", "throughout", "hateful", "action", "duvet", "tailcoat", "the",
                "danger", "minster", "army", "dent", "share", "beard", "lazy", "year", "must", "resigned", "angry",
                "relapse", "trump", "union", "adventure", "honey", "inject", "special", "end", "crossing", "slow",
                "grunge", "prize", "of", "dreamer", "issues", "tomcat", "riverbed", "founder", "working", "mask",
                "spinned", "reported", "meets", "john", "by", "vogue", "said", "jumpy", "democratic", "kingdom",
                "combs", "moments", "peaks", "rise", "innocent", "chalked", "from", "relations", "removing", 
                "platform", "unwillingness", "secretary", "computer", "arranger", "mentor", "was", "quote", "warn",
                "rally", "squadron", "profiles", "immature", "in", "drives", "train", "intrude", "state", "defense",
                "past", "plunges", "cry", "cameras", "promises", "outskirt", "subtitles", "hidden", "logic", "advertisement",
                "exact", "dimond", "risk", "director", "back", "sinatra", "store", "stopgap", "equal", "housing",
                "airport", "him", "security", "one", "dodges", "development", "entertainer", "ignition", "lovebird", "counts",
                "world", "protocol", "than", "cry", "shannon", "shear", "drain", "blues", "morning", "fronting", "executive",
                    "person", "reached", "taxplayer", "stranger", "schedules", "preston", "historic", "featured", "port", "tricked",
                "announced", "can", "time", "loadable", "wallets", "wounded", "core", "empire", "towers", "wished", "confused",
                "who", "hurdles", "toggles", "martin", "defender", "plane", "decked", "but", "harp", "ribs", "helpers", "least",
                "column", "cement", "room", "tension", "clipper", "shudder", "dangles", "storm", "farms", "zero", "affairs",
                "maniacs", "proposals", "two", "surrounded", "bone", "difficult", "dusters", "sailed", "bonding", "speak",
                "however", "sands", "scribble", "maiden", "mouths", "arms", "coopered", "learned", "recieved", "rocks", "tripod",
                "skull", "dwarf", "number", "arrested", "squish", "singer", "buttered", "found", "new", "million", "token", "combo",
                "completed", "national", "markets", "because", "lower", "trainers", "chief", "angle", "seabed", "trickled", "firsy",
                "undated", "ludlow", "stones", "joker", "monster", "reach", "outlook", "aware", "among", "comfort", "living", "contact",
                "french", "thailand", "open", "yelps", "delayed", "smart", "metropolitan", "meter", "snow", "castle", "tools", "irrigration",
                "new", "gas", "sidestepping", "drys", "cartoons", "declared", "numskull", "brass", "sped", "washington", "landing", 
                "ordeal", "cops", "quiet", "strike", "destruct", "wrote", "mamma", "tunes", "balanced", "officers", "books", "flame",
                    "am", "fishing", "not", "inspires", "beehives", "hypnotic", "flops", "sandhurst", "invoked", "bullion", "christmas", "muttered",
                "moats", "baggy", "ginger", "obligated", "hangover", "building", "midland", "mocks", "heavy", "don", "quotable", "clarkson",
                "vary", "enriches", "romania", "sumo", "ushering", "votes", "cool", "resumed", "sprayers", "convents", "unchanging", "prove",
                "renshaw", "holy", "recited", "kingpins", "valiant", "yesterday", "withdrawel", "itself", "slog", "marry", "newborns");

$image      = imagecreate($width, $height);
$background = imagecolorallocatealpha($image, 255, 255, 255, 127);
$text       = imagecolorallocate($image, 20, 40, 100);

imagefill($image, 0, 0, $background);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $border_colour);

$string        = "test 123!";
$angl          = "5";
$x_coordinates = "24";
$y_coordinates = "24";

imagettftext($image, $font_size, $angle, $x_coordinates, $y_coordinates, $text_colour, $font, $string);

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}

image_captcha_verification();
?>

Link to comment
Share on other sites

ive tried to set the transparentcey but its not working...also i think im having a problem with text and border colour

 

<?php
function image_captcha_verification($width = NULL, $height = NULL, $words = NULL)
{
$font_size      = "18";
$border_colour  = "239, 239, 239";
	$font           = "verdana.ttf";
$width          = "400";
$height         = "80";

$words = array ("police", "time", "broadway", "is", "throughout", "hateful", "action", "duvet", "tailcoat", "the",
                "danger", "minster", "army", "dent", "share", "beard", "lazy", "year", "must", "resigned", "angry",
                "relapse", "trump", "union", "adventure", "honey", "inject", "special", "end", "crossing", "slow",
                "grunge", "prize", "of", "dreamer", "issues", "tomcat", "riverbed", "founder", "working", "mask",
                "spinned", "reported", "meets", "john", "by", "vogue", "said", "jumpy", "democratic", "kingdom",
                "combs", "moments", "peaks", "rise", "innocent", "chalked", "from", "relations", "removing", 
                "platform", "unwillingness", "secretary", "computer", "arranger", "mentor", "was", "quote", "warn",
                "rally", "squadron", "profiles", "immature", "in", "drives", "train", "intrude", "state", "defense",
                "past", "plunges", "cry", "cameras", "promises", "outskirt", "subtitles", "hidden", "logic", "advertisement",
                "exact", "dimond", "risk", "director", "back", "sinatra", "store", "stopgap", "equal", "housing",
                "airport", "him", "security", "one", "dodges", "development", "entertainer", "ignition", "lovebird", "counts",
                "world", "protocol", "than", "cry", "shannon", "shear", "drain", "blues", "morning", "fronting", "executive",
                    "person", "reached", "taxplayer", "stranger", "schedules", "preston", "historic", "featured", "port", "tricked",
                "announced", "can", "time", "loadable", "wallets", "wounded", "core", "empire", "towers", "wished", "confused",
                "who", "hurdles", "toggles", "martin", "defender", "plane", "decked", "but", "harp", "ribs", "helpers", "least",
                "column", "cement", "room", "tension", "clipper", "shudder", "dangles", "storm", "farms", "zero", "affairs",
                "maniacs", "proposals", "two", "surrounded", "bone", "difficult", "dusters", "sailed", "bonding", "speak",
                "however", "sands", "scribble", "maiden", "mouths", "arms", "coopered", "learned", "recieved", "rocks", "tripod",
                "skull", "dwarf", "number", "arrested", "squish", "singer", "buttered", "found", "new", "million", "token", "combo",
                "completed", "national", "markets", "because", "lower", "trainers", "chief", "angle", "seabed", "trickled", "firsy",
                "undated", "ludlow", "stones", "joker", "monster", "reach", "outlook", "aware", "among", "comfort", "living", "contact",
                "french", "thailand", "open", "yelps", "delayed", "smart", "metropolitan", "meter", "snow", "castle", "tools", "irrigration",
                "new", "gas", "sidestepping", "drys", "cartoons", "declared", "numskull", "brass", "sped", "washington", "landing", 
                "ordeal", "cops", "quiet", "strike", "destruct", "wrote", "mamma", "tunes", "balanced", "officers", "books", "flame",
                    "am", "fishing", "not", "inspires", "beehives", "hypnotic", "flops", "sandhurst", "invoked", "bullion", "christmas", "muttered",
                "moats", "baggy", "ginger", "obligated", "hangover", "building", "midland", "mocks", "heavy", "don", "quotable", "clarkson",
                "vary", "enriches", "romania", "sumo", "ushering", "votes", "cool", "resumed", "sprayers", "convents", "unchanging", "prove",
                "renshaw", "holy", "recited", "kingpins", "valiant", "yesterday", "withdrawel", "itself", "slog", "marry", "newborns");

$image      = imagecreatetruecolor($width, $height);
$background = imagecolorallocatealpha($image, 255, 0, 0, 0);
$text       = imagecolorallocate($image, 20, 40, 100);

imagefill($image, 0, 0, $background);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $border_colour);
imagecolortransparent($image, $background);

$random_key    = array_rand($words, 2);
$string        = $words[$random_key[0]] . " " . $words[$random_key[1]];
$array         = preg_split("//", $string, -1);
$array_length  = count($array) - 1;

for($i = 1; $i < $array_length; $i++)
{		
	$letter        = $array[$i];

	if ($letter == " ")
	{
	//	ucfirst
		//lcfirst
		//strtolower
		//strtoupper
		//ucwords
	}

	$letter_spacer = 25 * $i;
	$angle         = rand(1, 25);
	$x_coordinates = 50 + $letter_spacer;
 	$y_coordinates = ($height / 2);

	imagettftext($image, $font_size, $angle, $x_coordinates, $y_coordinates, $text_colour, $font, $letter);
}

session_start();
$_SESSION['captcha_code'] = md5($string);

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}

image_captcha_verification();
?>

Link to comment
Share on other sites

<?php
function image_captcha_verification($width = NULL, $height = NULL, $words = NULL)
{
$font_size      = "18";
$border_colour  = "239, 239, 239";
	$font           = "verdana.ttf";
$width          = "400";
$height         = "80";

$words = array ("police", "time", "broadway", "is", "throughout", "hateful", "action", "duvet", "tailcoat", "the",
                "danger", "minster", "army", "dent", "share", "beard", "lazy", "year", "must", "resigned", "angry",
                "relapse", "trump", "union", "adventure", "honey", "inject", "special", "end", "crossing", "slow",
                "grunge", "prize", "of", "dreamer", "issues", "tomcat", "riverbed", "founder", "working", "mask",
                "spinned", "reported", "meets", "john", "by", "vogue", "said", "jumpy", "democratic", "kingdom",
                "combs", "moments", "peaks", "rise", "innocent", "chalked", "from", "relations", "removing", 
                "platform", "unwillingness", "secretary", "computer", "arranger", "mentor", "was", "quote", "warn",
                "rally", "squadron", "profiles", "immature", "in", "drives", "train", "intrude", "state", "defense",
                "past", "plunges", "cry", "cameras", "promises", "outskirt", "subtitles", "hidden", "logic", "advertisement",
                "exact", "dimond", "risk", "director", "back", "sinatra", "store", "stopgap", "equal", "housing",
                "airport", "him", "security", "one", "dodges", "development", "entertainer", "ignition", "lovebird", "counts",
                "world", "protocol", "than", "cry", "shannon", "shear", "drain", "blues", "morning", "fronting", "executive",
                    "person", "reached", "taxplayer", "stranger", "schedules", "preston", "historic", "featured", "port", "tricked",
                "announced", "can", "time", "loadable", "wallets", "wounded", "core", "empire", "towers", "wished", "confused",
                "who", "hurdles", "toggles", "martin", "defender", "plane", "decked", "but", "harp", "ribs", "helpers", "least",
                "column", "cement", "room", "tension", "clipper", "shudder", "dangles", "storm", "farms", "zero", "affairs",
                "maniacs", "proposals", "two", "surrounded", "bone", "difficult", "dusters", "sailed", "bonding", "speak",
                "however", "sands", "scribble", "maiden", "mouths", "arms", "coopered", "learned", "recieved", "rocks", "tripod",
                "skull", "dwarf", "number", "arrested", "squish", "singer", "buttered", "found", "new", "million", "token", "combo",
                "completed", "national", "markets", "because", "lower", "trainers", "chief", "angle", "seabed", "trickled", "firsy",
                "undated", "ludlow", "stones", "joker", "monster", "reach", "outlook", "aware", "among", "comfort", "living", "contact",
                "french", "thailand", "open", "yelps", "delayed", "smart", "metropolitan", "meter", "snow", "castle", "tools", "irrigration",
                "new", "gas", "sidestepping", "drys", "cartoons", "declared", "numskull", "brass", "sped", "washington", "landing", 
                "ordeal", "cops", "quiet", "strike", "destruct", "wrote", "mamma", "tunes", "balanced", "officers", "books", "flame",
                    "am", "fishing", "not", "inspires", "beehives", "hypnotic", "flops", "sandhurst", "invoked", "bullion", "christmas", "muttered",
                "moats", "baggy", "ginger", "obligated", "hangover", "building", "midland", "mocks", "heavy", "don", "quotable", "clarkson",
                "vary", "enriches", "romania", "sumo", "ushering", "votes", "cool", "resumed", "sprayers", "convents", "unchanging", "prove",
                "renshaw", "holy", "recited", "kingpins", "valiant", "yesterday", "withdrawel", "itself", "slog", "marry", "newborns");

$image      = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($image, 255, 0, 0);
$text       = imagecolorallocate($image, 20, 40, 100);

imagefill($image, 0, 0, $background);
//imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $border_colour);
imagecolortransparent($image, $background);

$random_key    = array_rand($words, 2);
$string        = $words[$random_key[0]] . " " . $words[$random_key[1]];
$array         = preg_split("//", $string, -1);
$array_length  = count($array) - 1;

for($i = 1; $i < $array_length; $i++)
{		
	$letter        = $array[$i];

	if ($letter == " ")
	{
	//	ucfirst
		//lcfirst
		//strtolower
		//strtoupper
		//ucwords
	}

	$letter_spacer = 25 * $i;
	$angle         = rand(1, 25);
	$x_coordinates = 50 + $letter_spacer;
 	$y_coordinates = ($height / 2);

	imagettftext($image, $font_size, $angle, $x_coordinates, $y_coordinates, $text_colour, $font, $letter);
}

session_start();
$_SESSION['captcha_code'] = md5($string);

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}

image_captcha_verification();
?>

Link to comment
Share on other sites

see if this helps, maybe you can use by example... *NOTE* This is set up as a class....

 

<?php
   // start a PHP session - this class uses sessions to store the generated 
   // code. Comment out if you are calling already from your application
   session_start();
   
   // class defaults - change to effect globally
   
   define('CAPTCHA_SESSION_ID', 'php_captcha');
   define('CAPTCHA_WIDTH', 200); // max 500
   define('CAPTCHA_HEIGHT', 50); // max 200
   define('CAPTCHA_NUM_CHARS', 4);
   define('CAPTCHA_NUM_LINES', 70);
   define('CAPTCHA_CHAR_SHADOW', false);
   define('CAPTCHA_OWNER_TEXT', '');
   define('CAPTCHA_CHAR_SET', 'A-Z'); // defaults to A-Z
   define('CAPTCHA_CASE_INSENSITIVE', true);
   define('CAPTCHA_BACKGROUND_IMAGES', '');
   define('CAPTCHA_MIN_FONT_SIZE', 20);
   define('CAPTCHA_MAX_FONT_SIZE', 25);
   define('CAPTCHA_USE_COLOUR', false);
   define('CAPTCHA_FILE_TYPE', 'jpeg');
   define('CAPTCHA_FLITE_PATH', '/usr/bin/flite');
   define('CAPTCHA_AUDIO_PATH', '/tmp/'); // must be writeable by PHP process
   
   /************************ End Default Options **********************/
   
   // don't edit below this line (unless you want to change the class!)
   
   class PhpCaptcha {
      var $oImage;
      var $aFonts;
      var $iWidth;
      var $iHeight;
      var $iNumChars;
      var $iNumLines;
      var $iSpacing;
      var $bCharShadow;
      var $sOwnerText;
      var $aCharSet;
      var $bCaseInsensitive;
      var $vBackgroundImages;
      var $iMinFontSize;
      var $iMaxFontSize;
      var $bUseColour;
      var $sFileType;
      var $sCode = '';
      
      function PhpCaptcha(
         $aFonts, // array of TrueType fonts to use - specify full path
         $iWidth = CAPTCHA_WIDTH, // width of image
         $iHeight = CAPTCHA_HEIGHT // height of image
      ) {
         // get parameters
         $this->aFonts = $aFonts;
         $this->SetNumChars(CAPTCHA_NUM_CHARS);
         $this->SetNumLines(CAPTCHA_NUM_LINES);
         $this->DisplayShadow(CAPTCHA_CHAR_SHADOW);
         $this->SetOwnerText(CAPTCHA_OWNER_TEXT);
         $this->SetCharSet(CAPTCHA_CHAR_SET);
         $this->CaseInsensitive(CAPTCHA_CASE_INSENSITIVE);
         $this->SetBackgroundImages(CAPTCHA_BACKGROUND_IMAGES);
         $this->SetMinFontSize(CAPTCHA_MIN_FONT_SIZE);
         $this->SetMaxFontSize(CAPTCHA_MAX_FONT_SIZE);
         $this->UseColour(CAPTCHA_USE_COLOUR);
         $this->SetFileType(CAPTCHA_FILE_TYPE);   
         $this->SetWidth($iWidth);
         $this->SetHeight($iHeight);
      }
      
      function CalculateSpacing() {
         $this->iSpacing = (int)($this->iWidth / $this->iNumChars);
      }
      
      function SetWidth($iWidth) {
         $this->iWidth = $iWidth;
         if ($this->iWidth > 500) $this->iWidth = 500; // to prevent perfomance impact
         $this->CalculateSpacing();
      }
      
      function SetHeight($iHeight) {
         $this->iHeight = $iHeight;
         if ($this->iHeight > 200) $this->iHeight = 200; // to prevent performance impact
      }
      
      function SetNumChars($iNumChars) {
         $this->iNumChars = $iNumChars;
         $this->CalculateSpacing();
      }
      
      function SetNumLines($iNumLines) {
         $this->iNumLines = $iNumLines;
      }
      
      function DisplayShadow($bCharShadow) {
         $this->bCharShadow = $bCharShadow;
      }
      
      function SetOwnerText($sOwnerText) {
         $this->sOwnerText = $sOwnerText;
      }
      
      function SetCharSet($vCharSet) {
         // check for input type
         if (is_array($vCharSet)) {
            $this->aCharSet = $vCharSet;
         } else {
            if ($vCharSet != '') {
               // split items on commas
               $aCharSet = explode(',', $vCharSet);
            
               // initialise array
               $this->aCharSet = array();
            
               // loop through items 
               foreach ($aCharSet as $sCurrentItem) {
                  // a range should have 3 characters, otherwise is normal character
                  if (strlen($sCurrentItem) == 3) {
                     // split on range character
                     $aRange = explode('-', $sCurrentItem);
                  
                     // check for valid range
                     if (count($aRange) == 2 && $aRange[0] < $aRange[1]) {
                        // create array of characters from range
                        $aRange = range($aRange[0], $aRange[1]);
                     
                        // add to charset array
                        $this->aCharSet = array_merge($this->aCharSet, $aRange);
                     }
                  } else {
                     $this->aCharSet[] = $sCurrentItem;
                  }
               }
            }
         }
      }
      
      function CaseInsensitive($bCaseInsensitive) {
         $this->bCaseInsensitive = $bCaseInsensitive;
      }
      
      function SetBackgroundImages($vBackgroundImages) {
         $this->vBackgroundImages = $vBackgroundImages;
      }
      
      function SetMinFontSize($iMinFontSize) {
         $this->iMinFontSize = $iMinFontSize;
      }
      
      function SetMaxFontSize($iMaxFontSize) {
         $this->iMaxFontSize = $iMaxFontSize;
      }
      
      function UseColour($bUseColour) {
         $this->bUseColour = $bUseColour;
      }
      
      function SetFileType($sFileType) {
         // check for valid file type
         if (in_array($sFileType, array('gif', 'png', 'jpeg'))) {
            $this->sFileType = $sFileType;
         } else {
            $this->sFileType = 'jpeg';
         }
      }
      
      function DrawLines() {
         for ($i = 0; $i < $this->iNumLines; $i++) {
            // allocate colour
            if ($this->bUseColour) {
               $iLineColour = imagecolorallocate($this->oImage, rand(100, 250), rand(100, 250), rand(100, 250));
            } else {
               $iRandColour = rand(100, 250);
               $iLineColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);
            }
            
            // draw line
            imageline($this->oImage, rand(0, $this->iWidth), rand(0, $this->iHeight), rand(0, $this->iWidth), rand(0, $this->iHeight), $iLineColour);
         }
      }
      
      function DrawOwnerText() {
         // allocate owner text colour
         $iBlack = imagecolorallocate($this->oImage, 0, 0, 0);
         // get height of selected font
         $iOwnerTextHeight = imagefontheight(2);
         // calculate overall height
         $iLineHeight = $this->iHeight - $iOwnerTextHeight - 4;
         
         // draw line above text to separate from CAPTCHA
         imageline($this->oImage, 0, $iLineHeight, $this->iWidth, $iLineHeight, $iBlack);
         
         // write owner text
         imagestring($this->oImage, 2, 3, $this->iHeight - $iOwnerTextHeight - 3, $this->sOwnerText, $iBlack);
         
         // reduce available height for drawing CAPTCHA
         $this->iHeight = $this->iHeight - $iOwnerTextHeight - 5;
      }
      
      function GenerateCode() {
         // reset code
         $this->sCode = '';
         
         // loop through and generate the code letter by letter
         for ($i = 0; $i < $this->iNumChars; $i++) {
            if (count($this->aCharSet) > 0) {
               // select random character and add to code string
               $this->sCode .= $this->aCharSet[array_rand($this->aCharSet)];
            } else {
               // select random character and add to code string
               $this->sCode .= chr(rand(65, 90));
            }
         }
         
         // save code in session variable
         if ($this->bCaseInsensitive) {
            $_SESSION[CAPTCHA_SESSION_ID] = strtoupper($this->sCode);
         } else {
            $_SESSION[CAPTCHA_SESSION_ID] = $this->sCode;
         }
      }
      
      function DrawCharacters() {
         // loop through and write out selected number of characters
         for ($i = 0; $i < strlen($this->sCode); $i++) {
            // select random font
            $sCurrentFont = $this->aFonts[array_rand($this->aFonts)];
            
            // select random colour
            if ($this->bUseColour) {
               $iTextColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));
            
               if ($this->bCharShadow) {
                  // shadow colour
                  $iShadowColour = imagecolorallocate($this->oImage, rand(0, 100), rand(0, 100), rand(0, 100));
               }
            } else {
               $iRandColour = rand(0, 100);
               $iTextColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);
            
               if ($this->bCharShadow) {
                  // shadow colour
                  $iRandColour = rand(0, 100);
                  $iShadowColour = imagecolorallocate($this->oImage, $iRandColour, $iRandColour, $iRandColour);
               }
            }
            
            // select random font size
            $iFontSize = rand($this->iMinFontSize, $this->iMaxFontSize);
            
            // select random angle
            $iAngle = rand(-30, 30);
            
            // get dimensions of character in selected font and text size
            $aCharDetails = imageftbbox($iFontSize, $iAngle, $sCurrentFont, $this->sCode[$i], array());
            
            // calculate character starting coordinates
            $iX = $this->iSpacing / 4 + $i * $this->iSpacing;
            $iCharHeight = $aCharDetails[2] - $aCharDetails[5];
            $iY = $this->iHeight / 2 + $iCharHeight / 4; 
            
            // write text to image
            imagefttext($this->oImage, $iFontSize, $iAngle, $iX, $iY, $iTextColour, $sCurrentFont, $this->sCode[$i], array());
            
            if ($this->bCharShadow) {
               $iOffsetAngle = rand(-30, 30);
               
               $iRandOffsetX = rand(-5, 5);
               $iRandOffsetY = rand(-5, 5);
               
               imagefttext($this->oImage, $iFontSize, $iOffsetAngle, $iX + $iRandOffsetX, $iY + $iRandOffsetY, $iShadowColour, $sCurrentFont, $this->sCode[$i], array());
            }
         }
      }
      
      function WriteFile($sFilename) {
         if ($sFilename == '') {
            // tell browser that data is jpeg
            header("Content-type: image/$this->sFileType");
         }
         
         switch ($this->sFileType) {
            case 'gif':
               $sFilename != '' ? imagegif($this->oImage, $sFilename) : imagegif($this->oImage);
               break;
            case 'png':
               $sFilename != '' ? imagepng($this->oImage, $sFilename) : imagepng($this->oImage);
               break;
            default:
               $sFilename != '' ? imagejpeg($this->oImage, $sFilename) : imagejpeg($this->oImage);
         }
      }
      
      function Create($sFilename = '') {
         // check for required gd functions
         if (!function_exists('imagecreate') || !function_exists("image$this->sFileType") || ($this->vBackgroundImages != '' && !function_exists('imagecreatetruecolor'))) {
            return false;
         }
         
         // get background image if specified and copy to CAPTCHA
         if (is_array($this->vBackgroundImages) || $this->vBackgroundImages != '') {
            // create new image
            $this->oImage = imagecreatetruecolor($this->iWidth, $this->iHeight);
            
            // create background image
            if (is_array($this->vBackgroundImages)) {
               $iRandImage = array_rand($this->vBackgroundImages);
               $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages[$iRandImage]);
            } else {
               $oBackgroundImage = imagecreatefromjpeg($this->vBackgroundImages);
            }
            
            // copy background image
            imagecopy($this->oImage, $oBackgroundImage, 0, 0, 0, 0, $this->iWidth, $this->iHeight);
            
            // free memory used to create background image
            imagedestroy($oBackgroundImage);
         } else {
            // create new image
            $this->oImage = imagecreate($this->iWidth, $this->iHeight);
         }
         
         // allocate white background colour
         imagecolorallocate($this->oImage, 255, 255, 255);
         
         // check for owner text
         if ($this->sOwnerText != '') {
            $this->DrawOwnerText();
         }
         
         // check for background image before drawing lines
         if (!is_array($this->vBackgroundImages) && $this->vBackgroundImages == '') {
            $this->DrawLines();
         }
         
         $this->GenerateCode();
         $this->DrawCharacters();
         
         // write out image to file or browser
         $this->WriteFile($sFilename);
         
         // free memory used in creating image
         imagedestroy($this->oImage);
         
         return true;
      }
      
      // call this method statically
      function Validate($sUserCode, $bCaseInsensitive = true) {
         if ($bCaseInsensitive) {
            $sUserCode = strtoupper($sUserCode);
         }
         
         if (!empty($_SESSION[CAPTCHA_SESSION_ID]) && $sUserCode == $_SESSION[CAPTCHA_SESSION_ID]) {
            // clear to prevent re-use
            unset($_SESSION[CAPTCHA_SESSION_ID]);
            
            return true;
         }
         
         return false;
      }
   }
   
   // this class will only work correctly if a visual CAPTCHA has been created first using PhpCaptcha
   class AudioPhpCaptcha {
      var $sFlitePath;
      var $sAudioPath;
      var $sCode;
      
      function AudioPhpCaptcha(
         $sFlitePath = CAPTCHA_FLITE_PATH, // path to flite binary
         $sAudioPath = CAPTCHA_AUDIO_PATH // the location to temporarily store the generated audio CAPTCHA
      ) {
         $this->SetFlitePath($sFlitePath);
         $this->SetAudioPath($sAudioPath);
         
         // retrieve code if already set by previous instance of visual PhpCaptcha
         if (isset($_SESSION[CAPTCHA_SESSION_ID])) {
            $this->sCode = $_SESSION[CAPTCHA_SESSION_ID];
         }
      }
      
      function SetFlitePath($sFlitePath) {
         $this->sFlitePath = $sFlitePath;
      }
      
      function SetAudioPath($sAudioPath) {
         $this->sAudioPath = $sAudioPath;
      }
      
      function Mask($sText) {
         $iLength = strlen($sText);
         
         // loop through characters in code and format
         $sFormattedText = '';
         for ($i = 0; $i < $iLength; $i++) {
            // comma separate all but first and last characters
            if ($i > 0 && $i < $iLength - 1) {
               $sFormattedText .= ', ';
            } elseif ($i == $iLength - 1) { // precede last character with "and"
               $sFormattedText .= ' and ';
            }
            $sFormattedText .= $sText[$i];
         }
         
         $aPhrases = array(
            "The %1\$s characters are as follows: %2\$s",
            "%2\$s, are the %1\$s letters",
            "Here are the %1\$s characters: %2\$s",
            "%1\$s characters are: %2\$s",
            "%1\$s letters: %2\$s"
         );
         
         $iPhrase = array_rand($aPhrases);
         
         return sprintf($aPhrases[$iPhrase], $iLength, $sFormattedText);
      }
      
      function Create() {
         $sText = $this->Mask($this->sCode);
         $sFile = md5($this->sCode.time());
         
         // create file with flite
         shell_exec("$this->sFlitePath -t \"$sText\" -o $this->sAudioPath$sFile.wav");
         
         // set headers
         header('Content-type: audio/x-wav');
         header("Content-Disposition: attachment;filename=$sFile.wav");
         
         // output to browser
         echo file_get_contents("$this->sAudioPath$sFile.wav");
         
         // delete temporary file
         @unlink("$this->sAudioPath$sFile.wav");
      }
   }
   
   // example sub class
   class PhpCaptchaColour extends PhpCaptcha {
      function PhpCaptchaColour($aFonts, $iWidth = CAPTCHA_WIDTH, $iHeight = CAPTCHA_HEIGHT) {
         // call parent constructor
         parent::PhpCaptcha($aFonts, $iWidth, $iHeight);
         
         // set options
         $this->UseColour(true);
      }
   }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.