📜 ⬆️ ⬇️

MagickWand Captcha

Hello.
As I have already said , by coincidence, I had to get acquainted with the interface to ImageMagick - MagickWand for PHP . As it turned out, the library is quite powerful and interesting.
Well, as an experiment, as well as for the future, I made a function to generate captcha using this library. Its advantage is that we do not have to write a picture on a disk and therefore delete it. Everything happens, so to speak, on the fly.
Here's what I got


First, the form and verification:
 <?php 
if ($_POST['submitbutton'] === 'ok') { // , . ,
if (md5($_POST['check']) != $_POST['str']) { // $_POST['check'] - , . $_POST['str'] -
echo 'FALSE'; //
}
else {
echo 'TRUE'; //
}
}
function get_random_string($leight) //
{
$ret="";
for ($i=0; $i<$leight; $i++)
{
if ( mt_rand(1,2) == 1 )
$char_code=mt_rand(48,57);
else
$char_code=mt_rand(97,122);
$ret.=chr($char_code);
}
return $ret;
}
session_start(); // ,
$rand_string = get_random_string(5); // 5
$_SESSION['capt'] = $rand_string; //
$capt = '<img src="capcha.php" />'; // ,
$str = md5($rand_string); // -
echo $capt; //
?>
<!-- --> <form action="test.php" method="post"> <input type="text" name="check" /> <input type="hidden" name="str" value="<?php echo $str; ?>" /> <button name="submitbutton" type="submit" value="ok"></button>
<?php
if ($_POST['submitbutton'] === 'ok') { // , . ,
if (md5($_POST['check']) != $_POST['str']) { // $_POST['check'] - , . $_POST['str'] -
echo 'FALSE'; //
}
else {
echo 'TRUE'; //
}
}
function get_random_string($leight) //
{
$ret="";
for ($i=0; $i<$leight; $i++)
{
if ( mt_rand(1,2) == 1 )
$char_code=mt_rand(48,57);
else
$char_code=mt_rand(97,122);
$ret.=chr($char_code);
}
return $ret;
}
session_start(); // ,
$rand_string = get_random_string(5); // 5
$_SESSION['capt'] = $rand_string; //
$capt = '<img src="capcha.php" />'; // ,
$str = md5($rand_string); // -
echo $capt; //
?>
<!-- --> <form action="test.php" method="post"> <input type="text" name="check" /> <input type="hidden" name="str" value="<?php echo $str; ?>" /> <button name="submitbutton" type="submit" value="ok"></button>
 <?php 
if ($_POST['submitbutton'] === 'ok') { // , . ,
if (md5($_POST['check']) != $_POST['str']) { // $_POST['check'] - , . $_POST['str'] -
echo 'FALSE'; //
}
else {
echo 'TRUE'; //
}
}
function get_random_string($leight) //
{
$ret="";
for ($i=0; $i<$leight; $i++)
{
if ( mt_rand(1,2) == 1 )
$char_code=mt_rand(48,57);
else
$char_code=mt_rand(97,122);
$ret.=chr($char_code);
}
return $ret;
}
session_start(); // ,
$rand_string = get_random_string(5); // 5
$_SESSION['capt'] = $rand_string; //
$capt = '<img src="capcha.php" />'; // ,
$str = md5($rand_string); // -
echo $capt; //
?>
<!-- --> <form action="test.php" method="post"> <input type="text" name="check" /> <input type="hidden" name="str" value="<?php echo $str; ?>" /> <button name="submitbutton" type="submit" value="ok"></button>
<?php
if ($_POST['submitbutton'] === 'ok') { // , . ,
if (md5($_POST['check']) != $_POST['str']) { // $_POST['check'] - , . $_POST['str'] -
echo 'FALSE'; //
}
else {
echo 'TRUE'; //
}
}
function get_random_string($leight) //
{
$ret="";
for ($i=0; $i<$leight; $i++)
{
if ( mt_rand(1,2) == 1 )
$char_code=mt_rand(48,57);
else
$char_code=mt_rand(97,122);
$ret.=chr($char_code);
}
return $ret;
}
session_start(); // ,
$rand_string = get_random_string(5); // 5
$_SESSION['capt'] = $rand_string; //
$capt = '<img src="capcha.php" />'; // ,
$str = md5($rand_string); // -
echo $capt; //
?>
<!-- --> <form action="test.php" method="post"> <input type="text" name="check" /> <input type="hidden" name="str" value="<?php echo $str; ?>" /> <button name="submitbutton" type="submit" value="ok"></button>

Now directly the picture generator
<?php
session_start(); //
$secret = $_SESSION['capt']; // , -
$color = '#d67f25'; //
$background = NewMagickWand(); // Magicwand -
MagickNewImage($background,200,75,$color); //
MagickSetImageFormat($background, 'png'); // - png
MagickAddNoiseImage($background,MW_ImpulseNoise); //

$drawing=NewDrawingWand(); // -
DrawSetFont($drawing,"ACADEROM.TTF"); //
DrawSetFontSize($drawing,48); //
DrawSetGravity($drawing,MW_SouthWestGravity); // .

for ($i = 1; $i <= 5; $i++) { 1 . - 5

$r_st = substr($secret,($i-1),1); //
$angle_mod = rand(0,45); //
$angle_polar = rand(0,1); // , ,
if ($angle_polar == 1) { //
$angle = $angle_mod;
}
else { // -
$angle = '-'.$angle_mod;
}
$letter_color_1 = dechex(rand(0,255)); // .
$letter_color_2 = dechex(rand(0,255));
$letter_color_3 = dechex(rand(0,255));
$letter_color = '#'.$letter_color_1.$letter_color_2.$letter_color_3; //

$fontcolor_light=NewPixelWand(); // MagickWand .
PixelSetColor($fontcolor_light,"#000000"); // -
DrawSetFillColor($drawing,$fontcolor_light); //
MagickAnnotateImage($background,$drawing,($i*30),0,$angle,$r_st); //

$fontcolor_light=NewPixelWand(); // ,
PixelSetColor($fontcolor_light,"$letter_color"); //
DrawSetFillColor($drawing,$fontcolor_light); //
MagickAnnotateImage($background,$drawing,($i*30),1,$angle,$r_st); //
}
header('Content-type: image/jpeg');
MagickEchoImageBlob($background); //
?>

Thus, during the execution of the first script, we generate a random line of characters, which in the global $ _SESSION array we give to the image generator, which already parses this line and draws letters / numbers with a random angle of inclination and random color.

And now I have a question for specialists!
How stable is this captcha? Is it possible to get around it somehow? I think that it is impossible, but, as usual, I doubt ...

')

Source: https://habr.com/ru/post/66403/


All Articles