Hey,
I wrote a script that outputs a filled rectangle with border, and inside is a random 8 char code that is machine unreadable, for a registration page.
I’m sure you know the type I’m talking about.
Instead of getting the png that I should be getting, I am getting a lot of line of garbled text.
I have the GD2 extension on, and I have specified: header(‘Content-type: image/png’) at the start of the document, but it still won’t draw.
Can someone tell me why? I’m sure it’s not the code, but something I’m not doing header or extension wise.
Here is the script:
[code=php]
<?
//create canvas size variables
$size_x = 200;
$size_y = 75;
//create random 8 character code with 1 number in the middle
$arrCode = array(‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’);
$arrNums = array(‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’);
for($i=0; $i < 7; $i++){
$t = mt_rand(0,26);
$tcode .= $arrCode[$t];
};
//add a random number to the middle of the code
$code = substr($tcode,0,3) . $arrNums[mt_rand(0,9)] . substr($tcode,4);
//calculate space between characters
$space_per_char = $size_x / (strlen($code) + 1);
//create canvas
$img = imagecreatetruecolor($size_x, $size_y);
//allocate colors
$background = imagecolorallocate($img, 255,255,255);
$border = imagecolorallocate($img, 128,128,128);
$colors[] = imagecolorallocate($img, 128,64,192);
$colors[] = imagecolorallocate($img, 192,64,128);
$colors[] = imagecolorallocate($img, 108,192,64);
//fill background
imagefilledrectangle($img, 1, 1, $size_x – 2, $size_y – 2, $background);
imagerectangle($img, 0, 0, $size_x – 1, $size_y – 1, $border);
//draw text
for($i = 0; $i < strlen($code); $i++){
$color = $colors[$i % count($colors)];
imagettftext($img, 28 + mt_rand(0,8), -20 + mt_rand(0,40), ($i + 0.3) * $space_per_char, 50 + mt_rand(0,10), $color, ‘arial.ttf’, $code{$i});
};
//add distortion
imageantialias($img, true);
for($i = 0; $i < 1000; $i++){
$x1 = mt_rand(5, $size_x – 5);
$y1 = mt_rand(5, $size_y – 5);
$x2 = $x1 – 4 + mt_rand(0,8);
$y2 = $y1 – 4 + mt_rand(0,8);
imageline($img, $x1, $y1, $x2, $y2, $colors[mt_rand(0, count($colors) – 1)]);
};
//output to page
imagepng($img);
?>
also, I do all my scripts in raw and debug in my head and with alerts, can someone recommend a good IDE?
Thanks,
CM