Working with the GD Graphics Library to create a CAPTURE image

I’m trying to create a CAPTCHA image as a form field using the GD Graphics library in PHP. The PHP functions used are:

imagecreatetruecolor();
imagecolorallocate();
imagefilledrectangle();
imageline();
imagesetpixel();
imagestring();
imagepng();
imagedestroy();

I’m getting the image, pixels, and lines, as well as the background color, but not the text. The code is below

mark

[php]session_start();

define(‘CAPTCHA_NUMCHRS’, 6);
define(‘CAPTCHA_WIDTH’, 100);
define(‘CAPTCHA_HEIGHT’, 25);

$passPhrase = ‘’;
for ($i = 0; $i < CAPTCHA_NUMCHRS; $i++)
{
$passPhrase .= chr(rand(97, 122));
}
$_SESSION[‘passPhrase’] = sha1($passPhrase);

$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

$bgColor = imagecolorallocate($img, 255, 255, 255);
$textColor = imagecolorallocate($img, 0, 0, 0);
$graphicColor = imagecolorallocate($img, 64, 64, 64);

imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bgColor);

for ($i = 0; $i < 5; $i++)
{
imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphicColor);
}
for ($i = 0; $i < 50; $i++)
{
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphicColor);
}
imagestring($img, 3, 75, 75, $passPhrase, $textColor);

header(“Content-type: image/png”);
imagepng($img);
imagedestroy($img);[/php]

You can start here with something that works…

[php]header(“Content-type: image/png”);

$string = “This is my test string.”;

$font = 2;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);

$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);

imagestring ($image,$font,0,0,$string,$black);

imagepng ($image);
imagedestroy($image);[/php]

The second and third for loops are what’s now producing an error saying image has errors. The $passPhrase is set ok however.

[php]<?php
header(“Content-type: image/png”);

$passPhrase = ‘’;
for ($i = 0; $i < 6; $i++)
{
$passPhrase .= chr(rand(97, 122));
}
$_SESSION[‘passPhrase’] = sha1($passPhrase);

$font = 2;
$width = imagefontwidth($font) * strlen($passPhrase);
$height = imagefontheight($font);

$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
$gray = imagecolorallocate($image, 64, 64, 64);
imagefill($image,0,0,$white);

for ($i = 0; $i < 5; $i++)
{
imageline($image, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $black);
}

for ($i = 0; $i < 50; $i++)
{
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $black);
}

imagestring ($image,$font,0,0,$passPhrase,$black);

imagepng ($image);
imagedestroy($image);

/*session_start();

define(‘CAPTCHA_NUMCHRS’, 6);
define(‘CAPTCHA_WIDTH’, 100);
define(‘CAPTCHA_HEIGHT’, 25);

$passPhrase = ‘’;
for ($i = 0; $i < CAPTCHA_NUMCHRS; $i++)
{
$passPhrase .= chr(rand(97, 122));
}
$_SESSION[‘passPhrase’] = sha1($passPhrase);

$img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

$bgColor = imagecolorallocate($img, 255, 255, 255);
$textColor = imagecolorallocate($img, 0, 0, 0);
$graphicColor = imagecolorallocate($img, 64, 64, 64);

imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bgColor);

for ($i = 0; $i < 5; $i++)
{
imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphicColor);
}
for ($i = 0; $i < 50; $i++)
{
imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphicColor);
}
imagestring($img, 3, 75, 75, $passPhrase, $textColor);

header(“Content-type: image/png”);
imagepng($img);
imagedestroy($img);*/
?>[/php]

[php]<?php
header(“Content-Type: image/png”);

// start image canvas
$image = @imagecreate(85, 30) or die(“Could not create image!”);

// allocate colors
imagecolorallocate($image,200,200,200);
$color_black = imagecolorallocate($image,0,0,0);

// generate random string and add it to the image
$random_string = rand();
$random_string = sha1($random_string);
$random_string = substr($random_string, 0, 7);

//imagestring($image, $font_size, $x_pos, $y_pos, $random_string, $color_black);
imagestring($image, 6, 10, 5, $random_string, $color_black);

// output image and free up memory
imagepng($image);
imagedestroy($image);
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service