Jump to content

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.