I tried to make my own basic little captcha out of boredom. It seems to be working except that every now and then it doesn't seem to output anything. when it happens in firefox with the webdeveloper console open I get the message "The image "http://....verimage.php" cannot be displayed because it contains errors." I'm not sure what the errors are or why its not working but this is what I have... I was hoping someone could spot the error.
<?php
// START THE SESSION
session_start();
// RESOURCES
$base_images = array('base1.png','base2.png','base3.png','base4.png','base5.png');
$fonts = array('ARBLANCA.ttf','ARBERKLEY.ttf','ARDECODE.ttf','CHILLER.TTF','GIGI.TTF','HARNGTON.TTF','INFROMAN.TTF','MTCORSVA.TTF');
$colors = array(
'red' => array('r'=>200,'g'=>0, 'b'=>0),
'blue' => array('r'=>0, 'g'=>0, 'b'=>200),
'black' => array('r'=>0, 'g'=>0, 'b'=>0),
'grey' => array('r'=>50, 'g'=>50, 'b'=>50)
);
// BUILD THE TEXT
$letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$size = rand(4,7);
$code = '';
for( $i=0; $i<$size; $i++ ) {
$code .= $letters[rand(0,52)];
}
// SELECT BASE IMAGE
$base = $base_images[array_rand($base_images)];
$im = imagecreatefrompng($base);
// PUT THE CODE IN THE IMAGE
for( $i=0; $i<$size; $i++ ) {
$f = $fonts[array_rand($fonts)]; // FONT TO USE
$c = $colors[array_rand($colors)];
$c = imagecolorallocate($im,$c['r'],$c['g'],$c['b']); // COLOR FOR FONT
$s = rand(20,23); // FONT SIZE
$a = rand(-10,10); // FONT ANGLE
$x = isset($r) ? 1+$r : 3; // LEFT - X POSITION OF FONT
$y = rand(30,35); // BASELINE - Y POSITION OF FONT
$r = imagettftext($im, $s, $a, $x, $y, $c, $f, $code[$i]); // PUT TEXT ON IMAGE
if( $r === FALSE ) die('Error putting text to image'); // DIE IF TEXT WRITE FAILS
$lr = $r[2]; // BOTTOM RIGHT MOST POSITION OF TEXT
$tr = $r[4]; // TOP RIGHT MOST POSITION OF TEXT
$r = $lr < $tr ? $lr : $tr; // RIGHT MOST POSITION OF TEXT
}
// STORE THE CODE IN SESSIONS
$_SESSION['veriCode'] = md5(strtolower($code));
// OUTPUT THE IMAGE AND FREE THE RESOURCE
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);