Jump to content

[SOLVED] Couple PHP questions


slpctrl

Recommended Posts

Alright, I'm coding a CAPTCHA, and I have a few questions. Here is my code (working thus far):

 

<?php
session_start();
$str1 = md5(microtime() * mktime());
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,54,29,$line);

imagestring($captcha, 5, 20, 10, $str, $black);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

 

Now with the imagestring functions, how can I set the text in the middle? Like, with this line:

 

imagestring($captcha, 5, 20, 10, $str, $black);

 

My image is 200x70, and I've always had trouble with the dimensions used in the GD library. Also, how can I change the font size? And font it's self? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/122897-solved-couple-php-questions/
Share on other sites

Alright, so I'm trying this:

 

<?php
session_start();
$str1 = md5(microtime() * mktime());
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,54,29,$line);

imagestring($captcha, rand(1, 5), rand(0, 200), rand(0,70), $str, $black);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

 

More importantly, this line:

 

imagestring($captcha, rand(1, 5), rand(0, 200), rand(0,70), $str, $black);

 

That right there, my width for the image is 200 and the height is 70, but for some reason it's going off the image. Anyone know what I did wrong?

Never mind. I realized that if it were to say, at worse case scenario start the text at the maximum random number I've got, it would start the text at the end of the image and continue off the page. Here is my working code to generate the captcha, not check it:

 

<?php
session_start();
$str1 = md5(microtime() * mktime());
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$black = imagecolorallocate($captcha,0,0,0);
$line = imagecolorallocate($captcha,rand(0,250),rand(0,239),rand(0,240));
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$line);
imagestring($captcha,rand(1,5),rand(0,100),rand(0,50),$str,$black);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.