Jump to content

Captcha Creation Help


aeroswat

Recommended Posts

So I looked up some code for a very simple captcha so that I don't have to re-invent the wheel. Anywho this code creates images which are very basic 6 letter strings. What I want to do is rotate each of the characters though. It uses the function ImageString() and I know ImageChar() exists as well. From what I know neither of these supports rotation of a character. Does anyone know of a function I can use to draw the character in a rotated state? I can't simply rotate the whole image because I want random rotations for each character.

Link to comment
https://forums.phpfreaks.com/topic/198280-captcha-creation-help/
Share on other sites

I suspect you will need to take a two phased approach. imagestring() simply writes text to an image, so it will always write it at the correct orientation. Here is what I would do.

 

1. Create the master image. Let's say you want the captcha letters to be 20 x 20 pixels. Then this image would be 120 wide and 20 high.

2. Create a single image for each letter that is 20 x 20. Then randomly rotate the image. They should maintain their 20x20 dimensions.

3. Place each image for each letter into the master image.

I suspect you will need to take a two phased approach. imagestring() simply writes text to an image, so it will always write it at the correct orientation. Here is what I would do.

 

1. Create the master image. Let's say you want the captcha letters to be 20 x 20 pixels. Then this image would be 120 wide and 20 high.

2. Create a single image for each letter that is 20 x 20. Then randomly rotate the image. They should maintain their 20x20 dimensions.

3. Place each image for each letter into the master image.

 

Damn >< I was hoping I wouldn't have to do that. I am also looking at a function I just found called imagettftext() it looks like this can handle rotation of text. I'm going to check into it. If not I'll have to go with your suggestion. Thanks!

I have a sort of captcha image. It's actually a sum. eg: 1 + 1 = $_SESSION['answer']

On the form, you check if the answer you enter in, matches the session. Although I could change this and put the code through.

Depends on what your wanting, what colour lines and dots (noise) and what colour text.

I have a sort of captcha image. It's actually a sum. eg: 1 + 1 = $_SESSION['answer']

On the form, you check if the answer you enter in, matches the session. Although I could change this and put the code through.

Depends on what your wanting, what colour lines and dots (noise) and what colour text.

 

Thanks. I was looking for rotation actually ;) I got it working though and it looks great! I used imagettftext() in a for loop where i broke my string down to a character array. For my x position I just used a number generated from whatever position was in the for loop. The function call looks like this for anyone else wanting to do this

 

	for($i=0;$i<6;$i++) {
	$rotate = rand(-35,35);
	imagettftext($image,16,$rotate,(($i+1)*30),30,$white,$font,$security_code[$i]);
}

 

$image is an image object that was defined with the ImageCreate function

16 is the font size

$rotate is defined above as a random number between -35 and 35

($i+1) * 30 is my x position for each character

30 is my y position

$white is the color of course defined with ImageColorAllocate function

$font is a path to my ttf file

$security_code[$i] is each character in my character array

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.