1
+ <?php
2
+
3
+
4
+ namespace CrowdSecBouncer \Fixes \Gregwar \Captcha ;
5
+
6
+ use Gregwar \Captcha \CaptchaBuilder as GregwarCaptchaBuilder ;
7
+
8
+ /**
9
+ * Override to fix "implicit conversion error on PHP 8.1"
10
+ * @see https://github.com/crowdsecurity/php-cs-bouncer/issues/62 and
11
+ * @see https://github.com/Gregwar/Captcha/pull/101/files
12
+ *
13
+ */
14
+ class CaptchaBuilder extends GregwarCaptchaBuilder {
15
+
16
+
17
+ /**
18
+ * Writes the phrase on the image
19
+ */
20
+ protected function writePhrase ($ image , $ phrase , $ font , $ width , $ height )
21
+ {
22
+ $ length = mb_strlen ($ phrase );
23
+ if ($ length === 0 ) {
24
+ return \imagecolorallocate ($ image , 0 , 0 , 0 );
25
+ }
26
+
27
+ // Gets the text size and start position
28
+ $ size = (int ) round ($ width / $ length ) - $ this ->rand (0 , 3 ) - 1 ;
29
+ $ box = \imagettfbbox ($ size , 0 , $ font , $ phrase );
30
+ $ textWidth = $ box [2 ] - $ box [0 ];
31
+ $ textHeight = $ box [1 ] - $ box [7 ];
32
+ $ x = (int ) round (($ width - $ textWidth ) / 2 );
33
+ $ y = (int ) round (($ height - $ textHeight ) / 2 ) + $ size ;
34
+
35
+ if (!$ this ->textColor ) {
36
+ $ textColor = array ($ this ->rand (0 , 150 ), $ this ->rand (0 , 150 ), $ this ->rand (0 , 150 ));
37
+ } else {
38
+ $ textColor = $ this ->textColor ;
39
+ }
40
+ $ col = \imagecolorallocate ($ image , $ textColor [0 ], $ textColor [1 ], $ textColor [2 ]);
41
+
42
+ // Write the letters one by one, with random angle
43
+ for ($ i =0 ; $ i <$ length ; $ i ++) {
44
+ $ symbol = mb_substr ($ phrase , $ i , 1 );
45
+ $ box = \imagettfbbox ($ size , 0 , $ font , $ symbol );
46
+ $ w = $ box [2 ] - $ box [0 ];
47
+ $ angle = $ this ->rand (-$ this ->maxAngle , $ this ->maxAngle );
48
+ $ offset = $ this ->rand (-$ this ->maxOffset , $ this ->maxOffset );
49
+ \imagettftext ($ image , $ size , $ angle , $ x , $ y + $ offset , $ col , $ font , $ symbol );
50
+ $ x += $ w ;
51
+ }
52
+
53
+ return $ col ;
54
+ }
55
+
56
+
57
+ /**
58
+ * Returns a random number or the next number in the
59
+ * fingerprint
60
+ */
61
+ protected function rand ($ min , $ max )
62
+ {
63
+ if (!is_array ($ this ->fingerprint )) {
64
+ $ this ->fingerprint = array ();
65
+ }
66
+
67
+ if ($ this ->useFingerprint ) {
68
+ $ value = current ($ this ->fingerprint );
69
+ next ($ this ->fingerprint );
70
+ } else {
71
+ $ value = mt_rand ((int ) $ min , (int ) $ max );
72
+ $ this ->fingerprint [] = $ value ;
73
+ }
74
+
75
+ return $ value ;
76
+ }
77
+
78
+ }
0 commit comments