Jump to content

[SOLVED] Captcha Spacing?


kevin_newbie

Recommended Posts

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

?>

 

Link to comment
https://forums.phpfreaks.com/topic/180917-solved-captcha-spacing/
Share on other sites

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.

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.