kevin_newbie Posted November 9, 2009 Share Posted November 9, 2009 Hello, I have a captcha going with a custom font. The only thing I am having trouble with creating space between each letter. what area should I work on to achieve this? <?php session_start(); class CaptchaSecurityImages { var $font = 'monofont.ttf'; function generateCode($characters) { $possible = '23456789bcdfghjkmnpqrstvwxyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; } function CaptchaSecurityImages($width='100',$height='30',$characters='6') { $code = $this->generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.65; $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours */ $background_color = imagecolorallocate($image, 255, 255, 255); $text_color = imagecolorallocate($image, 0, 59, 84); $noise_color = imagecolorallocate($image, 100, 120, 180); /* create textbox and add text */ $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function'); $x = ($width - $textbox[4])/2; $y = ($height - $textbox[5])/2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function'); /* output captcha image to browser */ header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['securityCode'] = $code; } } $width = isset($_GET['width']) ? $_GET['width'] : '100'; $height = isset($_GET['height']) ? $_GET['height'] : '30'; $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6'; $captcha = new CaptchaSecurityImages($width,$height,$characters); ?> Quote Link to comment Share on other sites More sharing options...
kevin_newbie Posted November 10, 2009 Author Share Posted November 10, 2009 Maybe I wasn't clear on it but my string is doing "a4fd5" I want spaces in between those letters so that it is like "a 4 f d 5". Hopes that helps. Quote Link to comment Share on other sites More sharing options...
.josh Posted November 10, 2009 Share Posted November 10, 2009 you would basically loop through your script (up to the imagettftext part) doing 1 letter at a time but with an added offset amount to $x in imagettftext Quote Link to comment Share on other sites More sharing options...
kevin_newbie Posted November 10, 2009 Author Share Posted November 10, 2009 OH nice thanks Crayon, I did this just before the imagettftext $newcode = ''; $codearray = str_split($code); for($i = 0;$i < count($codearray);$i++) { $newcode .= $codearray[$i].' '; } Quote Link to comment Share on other sites More sharing options...
.josh Posted November 10, 2009 Share Posted November 10, 2009 yeah, suppose you could do that if you want it to be spaced by a whole blank space. The way I mentioned would allow you to control the spacing by the pixel. Quote Link to comment Share on other sites More sharing options...
kevin_newbie Posted November 10, 2009 Author Share Posted November 10, 2009 Not sure what you mean by the pixel? Quote Link to comment Share on other sites More sharing options...
.josh Posted November 10, 2009 Share Posted November 10, 2009 The spacing between each letter. A blank space is X pixels wide, depending on the font you are using. The way you did it, it will be a set X pixel space (whatever the width of the space is for your font you are using. With the way I suggested, you can specify how many pixels worth of space are inbetween each letter. You can even use like rand(1,10) or something to make the amount of space between each letter vary. Quote Link to comment Share on other sites More sharing options...
kevin_newbie Posted November 10, 2009 Author Share Posted November 10, 2009 OH okay that makes sense thanks Quote Link to comment 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.