/    Sign up×
Community /Pin to ProfileBookmark

Help with GD image creation

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);
?>
[/code]

also, I do all my scripts in raw and debug in my head and with alerts, can someone recommend a good IDE?

Thanks,
CM

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinauthorJan 24.2006 — Ok, I got this working fine, but only when it was in a page of it's own.

I guess because it creates an image not a document, and won't allow anything else in the document.

My problem is, I can now display it is an iframe on the registration page, but how do I get the variable $code for verification?

I tried seeting a session variable once $code had been created, and then tried to get this variable, but I couldn't, and without a php debugger I'm not even sure if the session variable is being set.

thanks
Copy linkTweet thisAlerts:
@CrazyMerlinauthorJan 24.2006 — Ok, I'm getting there slowly.

I now know the session variable is being set, and I can get it.

The problem is, the page that gets the session variable is completed before the page that writes it, so the value is always the previous value.

I could use a javascript call to setTimeout() to get the value, but is there a 'nicer' way to do it?

thanks
Copy linkTweet thisAlerts:
@bokehJan 24.2006 — Your code is giving me a headache. If I echo $code at the end it returns a different code to the one displayed in the image. How are you going to read the image code if this is the case? It's no good randomising things to such a degree that the script can't access the code it is creating.
Copy linkTweet thisAlerts:
@CrazyMerlinauthorJan 24.2006 — when I echo $code, from with thscript, juast after it's created it returns the correct value, the same as the image, but if I then assign it to a session variable, when I check the session variable it is different. After a few page refreshes you'll notice the the session variable has the value of the last displayed image, not the current one.
Copy linkTweet thisAlerts:
@bokehJan 24.2006 — This works for me:[code=php]<?php

session_start();

if(isset($_GET['i'])){

// image creation section
crazy_image();

}elseif(isset($_POST['captcha'])){

// validation section
echo(captcha_validate())?'That was correct.<br>':'That was not correct.<br>';
echo '<a href="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'">Try another?</a>';
exit;

}else{

// form section
echo
'<img src="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?i='.uniqid().'" alt=""><br>'."n".
'<form action="http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'" method="POST">'."n".
'<input type="text" name="captcha" ><input type="submit" value="test it"></form>';

}


function crazy_image()
{
//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
header('Content-Type: image/png');
imagepng($img);
$_SESSION['captcha'] = str_replace(' ','',$code);
}

function captcha_validate()
{
return($_POST['captcha'] == $_SESSION['captcha']);
}

?>[/code]
Copy linkTweet thisAlerts:
@CrazyMerlinauthorJan 24.2006 — Thank you bokeh, I was pulling my hair out then.
Copy linkTweet thisAlerts:
@bokehJan 24.2006 — Thank you bokeh, I was pulling my hair out then.[/QUOTE]You're welcome! Personally I find your image a bit hard to read. And the randomization a bit over complex. [URL=http://bokehman.com/captcha_verification]Here's one I made with audio backup[/URL]. And the following one is at the other end of the complexity scale. [code=php]<?php

function captcha_image()
{
$_SESSION['captcha'] = substr(uniqid(), 0, 8);
$image = imagecreate(80, 20);
$background_colour = imagecolorallocate($image, 255,255,255);
$text_shadow = imagecolorallocate($image, 127,127,127);
$text_colour = imagecolorallocate($image, 0,0,0);
imagestring($image, 5, 1, 1, $_SESSION['captcha'], $text_shadow);
imagestring($image, 5, 0, 0, $_SESSION['captcha'], $text_colour);
header ('Content-type: image/png');
imagepng($image, null, 100);
imagedestroy($image);
exit;
}

?>[/code]
×

Success!

Help @CrazyMerlin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...