Skip to content

Instantly share code, notes, and snippets.

@codingant007
Created December 21, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingant007/44dbcb4524d73d81c753439f0c919594 to your computer and use it in GitHub Desktop.
Save codingant007/44dbcb4524d73d81c753439f0c919594 to your computer and use it in GitHub Desktop.
watercap generator script
<?php
// Webmail CAPTCHA Image ganarator
// Generates labelled CAPTCHA images at random
// Can be used to generate a dataset
// README:
// - make sure php5 & php5-gd are installed (can be done via apt)
// - Create a directory to house your dataset
// - put this script in the that directory
// - open a shell and cd into that directory
// - Execute `php gen_cap.php 100` to generate 100 images.
// - Images will ge generated in the same directory
// - Number can be modified as per requirement
// length of CAPTCHA code in number of characters
//
$captcha_length = 5;
class WaterCap {
var $font = '';
function WaterCap ($code, $width='250', $height='60') {
$this->font = './Vera.ttf';
/* seed random number gen to produce the same noise pattern time after time */
mt_srand(crc32($code));
/* init image */
$font_size = $height * 0.85;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
$d = -1;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
imagettftext(
$image, $font_size, 0, $x + $d, $y + $d, $noise_color, $this->font , $code
) or die('Error in imagettftext function');
imagettftext(
$image, $font_size, 0, $x + 2 * $d + 1, $y + 2 * $d + 1, $noise_color, $this->font , $code
) or die('Error in imagettftext function');
imagettftext(
$image, $font_size, 0, $x + 2 * $d, $y + 2 * $d, $background_color, $this->font , $code
) or die('Error in imagettftext function');
/* mix in background dots */
for( $i=0; $i<($width*$height)/10; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $background_color);
}
/* mix in text and noise dots */
for( $i=0; $i<($width*$height)/25; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $text_color);
}
/* rotate a bit to add fuzziness */
$image = imagerotate($image, 1, 16777215);
/* output */
// header("Content-Type: image/png");
imagepng($image, $code.".png");
imagedestroy($image);
}
}
function generateCode($number_characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
// lower case letters such as g, p, q, y get cut off
//$possible = '23456789bcdfghjkmnpqrstvwxyz';
$possible = '23456789ABCDEFGHKMNPRSTVWXYZ';
// sq_mt_randomize();
$code = '';
$i = 0;
while ($i < $number_characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
$count = 0;
while($count < (int)$argv[1]) {
$captcha_code = generateCode($captcha_length);
$captcha = new WaterCap($captcha_code);
$count = $count + 1;
if($count%1000 == 0 && $count > 0) echo "Finished generating ".$count." CAPTCHAs";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment