📜 ⬆️ ⬇️

Matane captcha for PHP - it's easy!


In this topic, I want to tell you about creating a so-called. Matan-Captcha using only PHP and GD. To draw the formulas, we will use PhpMathPublisher - a freely available library that uses only the GD extension.

A bit of theory


As a task, we will use not a limit, as on Lurkmore, but a simple definite integral of a polynomial function. If desired, it can be counted even in the mind. Here is an example of a solution for a picture from the beginning of the article:

Actually, the development


PhpMathPublisher should be in the phpmathpublisher folder.
Initialize:
include("phpmathpublisher/mathpublisher.php"); //    $upper=rand(0, 10); //    $lower=rand(-10, 0); //    $num=4; //     

Now you need to create a string with the formula, you can simultaneously calculate the answer:
 $text="int{".$lower."}{".$upper."}{"; for($i=0;$i<$num;$i++){ $k=rand(1, 5); //   x $p=rand(1, 5); //   x $sign=rand(0, 2)==0?0:1; // ,    2   $tt=$k>1?$k:""; //      $tt.="x"; if($p>1)$tt.="^".$p; if($i==0){ if($sign==1)$text.="({-}".$tt.")"; else $text.=$tt; }else $text.=($sign==1?"-":"+").$tt; if($sign==1)$k=-$k; $answer+=($k*pow($upper, $p+1)/($p+1))-($k*pow($lower, $p+1)/($p+1)); //    . } $text.="dx}"; 

As a result, we get a line of this type:
int {-4} {4} {3x ^ 5-4x ^ 5 + 3x ^ 2 + x ^ 2dx}
Now it remains to draw all this in the picture - with the help of PhpMathPublisher it will take only a few lines:
 $formula=new expression_math(tableau_expression(trim($text))); $formula->dessine(24); // 24 -   $w=imagesx($formula->image)+20; //   10    ,            $h=imagesy($formula->image)+20; $fi=imagecreatetruecolor($w, $h); imagefill($fi, 0, 0, 0xFFFFFF); imagecopy($fi, $formula->image, 10, 10, 0, 0, $w-20, $h-20); 

Next, it’s nice to apply nonlinear distortion - for this I used the MultiWave algorithm. Under the link the full code and an example of use.
And then we have nothing left but to display the resulting image in the browser:
 header("Content-type: image/png"); imagepng($im); 

Instead of conclusion


A few more captchas generated by this script:



And a page with an example of use .
I hope, my server will not lay down under a habraeffekt

')

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


All Articles