Captcha [Part 2]

Captcha again!! :) In this Captcha, i tried to using Ming Library as PHP extension. Ming Library is used to generate Flash movie on Fly. So I can animated my Captcha using Flash movie. Make sure you have installed this extension. For Windows user you can install easily with XAMPP (download here). It’s same like previous Captcha you need add least 3 PHP files.

First type this code and save it to anti_spam.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<font face="arial" size="2">
<form method='POST' action='process_anti_spam.php' enctype='multipart/form-data'>
<strong>Input security code:</strong><br />
<table cellpadding='3' cellspacing='1'>
<tr><td>
 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" id=objects width=100 height=50>
<param name=movie value=image_anti_spam.php>
<embed src=image_anti_spam.php width=100 height=50 type="application/x-shockwave-flash" pluginspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</object>
 
</td></tr>
<tr><td><input type='text' name='code'/>
<input name='submit' type='submit' value='CheckCode'></td></tr>
</table>
</form>

It’s different form previous Captcha. In this Captcha form you need to embed SWF file with and tags. In this tutorial, I animated my Captcha with blink effect. You’ll need to create an movie, movie clips, shape and text.

To create a movie you’ll need class SWFMovie()

1
2
3
4
//MAKE MOVIE
$movie = new SWFMovie();
$movie->setDimension(100, 50);
$movie->setBackground(255,255,255);

You can set dimension with setDimension() function. This function has two arguments, first x coordinate in pixel and y coordinate in pixel. Then you can set background with setBackground() function. This function has 4 arguments, first 3 arguments are RGB value, and the second one is apha chanel (optional).

Then you need to add shape on your movie. To create shape you’ll need class SWFShape().

1
2
3
//MAKE SHAPE
$shape = new SWFShape();
$shape->setLine(1,0,0,0);

You can set line for shape using setLine() function. This function have five argument, first width of border line in pixel, next three are RGB value, and the last is Alpha channel (optional). Then you must buffer image(JPEG) for background.

1
2
3
4
5
6
//buffer image for background
$handle = fopen("bg.jpg", "rb");
$data='';
while (!feof($handle)) {
$data .= fread($handle, 8192);}
fclose($handle);

To fill image on shape you can use SWFBitmap() class.

1
2
3
4
//fill shape with image
$bitmap=new SWFBitmap($data);
$fill = $shape->addFill($bitmap, SWFFILL_TILED_BITMAP);
$shape->setRightFill($fill);

Then you must draw rectangle. You can draw it using movePenTo() and drawLineTo() functions.

1
2
3
4
5
6
//draw shape
$shape->movePenTo(0, 0);
$shape->drawLineTo(100, 0);
$shape->drawLineTo(100, 50);
$shape->drawLineTo(0, 50);
$shape->drawLineTo(0, 0);

The drawLineTo() function draws a line from the current pen position to the (x, y) coordinate specified.
The pen position is where the tip of the pen is currently on the canvas. You can move the position of the pen by calling the movePenTo() member function. The movePenTo() member function takes two arguments: an x coordinate and a y coordinate.

Then add shape to movie.

1
$movie->add($shape);

To create a text you’ll need SWFText() class and SWFFont() class . SWFText() class need file font with FDB(Font Definition Block) format. You can convert TTF to FDB with ttf2fdb software (download here).

1
2
3
4
5
6
7
8
//MAKE FONT AND TEXT
$font=new SWFFont("impact.fdb");
$text=new SWFText(); 
$text->setFont($font);
$text->moveTo(4, 35);
$text->setColor(0, 0, 0); 
$text->setHeight(30);  
$text->addString($randtext);

You can set font using setFont() function. Set color using setColor() function. It’s have RGB arguments. Set height of font using setHeight() function. Add string using addString() function.
Then you must add text to movie clip.

To create a movie clip you’ll need class SWFSprite()

1
2
3
4
5
6
7
8
9
//add text to Sprite
$handleText=$sprite->add($text);
//go to next Frame
$sprite->nextFrame();
$sprite->nextFrame();
$sprite->nextFrame();
//remove text form sprite
$sprite->remove($handleText);
$sprite->nextFrame();

Like in Macromedia Flash software, you can create a movie clip with Ming Library. Time lining in Ming Library is quite difficult, because it’s doesn’t have GUI to show it frames. You can move to next frame using nextFrame() function. Blinking effect created by adding and removing Captcha text on sprite (using add() function and remove() function ). Add this sprite to movie.

1
$movie->add($sprite);

Put it all together and save it to image_anti_spam.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
session_start();
 
function strrand($length){
$str = '';
while(strlen($str)<$length){
	$random=rand(48,122);
if(($random>47 && $random<58)){
	$str.=chr($random);}}
return $str;}
 
$randtext =$_SESSION['string']=strrand(6);
//-----------------------------------------------
//using Ming Library
//MAKE MOVIE
$movie = new SWFMovie();
$movie->setDimension(100, 50);
$movie->setBackground(255,255,255);
 
//MAKE SHAPE
$shape = new SWFShape();
$shape->setLine(1,0,0,0);
 
//buffer image for background
$handle = fopen("bg.jpg", "rb");
$data='';
while (!feof($handle)) {
$data .= fread($handle, 8192);}
fclose($handle);
 
//fill shape with image
$bitmap=new SWFBitmap($data);
$fill = $shape->addFill($bitmap, SWFFILL_TILED_BITMAP);
$shape->setRightFill($fill);
 
//draw shape
$shape->movePenTo(0, 0);
$shape->drawLineTo(100, 0);
$shape->drawLineTo(100, 50);
$shape->drawLineTo(0, 50);
$shape->drawLineTo(0, 0);
 
 
//MAKE FONT AND TEXT
$font=new SWFFont("impact.fdb");
$text=new SWFText(); 
$text->setFont($font);
$text->moveTo(4, 35);
$text->setColor(0, 0, 0); 
$text->setHeight(30);  
$text->addString($randtext);
 
$movie->add($shape);
 
//MAKE BLINK EFFECT
//make sprite (movie clip)			
$sprite=new SWFSprite();
 
//add text to Sprite
$handleText=$sprite->add($text);
 
//go to next Frame
$sprite->nextFrame();
$sprite->nextFrame();
$sprite->nextFrame();
 
//remove text form sprite
$sprite->remove($handleText);
$sprite->nextFrame();
 
$movie->add($sprite);
header('Content-type: application/x-shockwave-flash');
$movie->output();
//-----------------------------------------------
?>

The last one type this code and save it to process_anti_spam.php.

1
2
3
4
5
6
7
8
9
<?
session_start();
echo "<font face=\"arial\" size=\"6\">";
if($_POST['code']!=$_SESSION['string']){
	echo "You are Machine!!^^";
}else{
	echo "Welcome Human!!^^</font>";
}
?>

After you create all files above. Try to run Captcha with anti_spam.php under localhost.

Preview Captcha 2

Read the codes and type it on text area. Finish? Submit it. If you get error message, it’s mean you failed to submit or your codes doesn’t match >.< or “You are Machine!!” haha.. but if you get welcome , congratulation you are human :p ( no just kidding about machine and human).

Nice right^^ Get PHP more powerful then..

Finish…:)



Related Post:

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Blogroll Link Update