slpctrl Posted September 5, 2008 Share Posted September 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122897-solved-couple-php-questions/ Share on other sites More sharing options...
slpctrl Posted September 5, 2008 Author Share Posted September 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/122897-solved-couple-php-questions/#findComment-634740 Share on other sites More sharing options...
slpctrl Posted September 5, 2008 Author Share Posted September 5, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/122897-solved-couple-php-questions/#findComment-634753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.