10
10
use SimpleCaptcha \Helpers \Str ;
11
11
use SimpleCaptcha \Helpers \Mime ;
12
12
13
+ use \thiagoalessio \TesseractOCR \TesseractOCR ;
14
+
13
15
use GdImage ;
14
16
use resource ;
15
17
use Exception ;
@@ -299,25 +301,25 @@ private function drawLine(?int $color = null): void
299
301
*/
300
302
private function applyNoise (): void
301
303
{
302
- for ($ i = 0 ; $ i < Str::length ($ this ->phrase ) * $ this ->noiseFactor ; $ i ++) {
304
+ for ($ i = 0 ; $ i < Str::length ($ this ->phrase ) * $ this ->noiseFactor ; $ i ++) {
303
305
# Determine random letter ..
304
- $ character = static ::randomCharacter ();
306
+ $ character = static ::randomCharacter ();
305
307
$ font = $ this ->randomFont ();
306
308
307
309
# .. of random size & color, ..
308
- $ fontSize = mt_rand (5 , 10 );
310
+ $ fontSize = mt_rand (5 , 10 );
309
311
$ textColor = imagecolorallocate ($ this ->image , mt_rand (0 , 128 ), mt_rand (0 , 128 ), mt_rand (0 , 128 ));
310
312
311
313
# .. random position ..
312
314
$ x = mt_rand (0 , $ this ->width );
313
- $ y = mt_rand (0 , $ this ->height );
315
+ $ y = mt_rand (0 , $ this ->height );
314
316
315
317
# .. random angle ..
316
- $ angle = mt_rand (-45 , 45 );
318
+ $ angle = mt_rand (-45 , 45 );
317
319
318
320
# .. and apply it
319
- imagettftext ($ this ->image , $ fontSize , $ angle , $ x , $ y , $ textColor , $ font , $ character );
320
- }
321
+ imagettftext ($ this ->image , $ fontSize , $ angle , $ x , $ y , $ textColor , $ font , $ character );
322
+ }
321
323
}
322
324
323
325
@@ -545,7 +547,6 @@ private function distort(): void
545
547
$ Vn2 = $ Vn + 4 * sin ($ Vn / 30 );
546
548
$ nX = $ X + ($ Vx * $ Vn2 / $ Vn );
547
549
$ nY = $ Y + ($ Vy * $ Vn2 / $ Vn );
548
-
549
550
} else {
550
551
$ nX = $ X ;
551
552
$ nY = $ Y ;
@@ -562,7 +563,6 @@ private function distort(): void
562
563
$ this ->pixel2int (floor ($ nX ), ceil ($ nY )),
563
564
$ this ->pixel2int (ceil ($ nX ), ceil ($ nY ))
564
565
);
565
-
566
566
} else {
567
567
$ p = $ this ->pixel2int ($ this ->round ($ nX ), $ this ->round ($ nY ));
568
568
}
@@ -695,12 +695,12 @@ public function build(int $width = 150, int $height = 40): self
695
695
* See https://priteshgupta.com/2011/09/advanced-image-functions-using-php
696
696
* See https://github.com/raoulduke/phpocrad
697
697
*
698
- * @param string $file Output file
698
+ * @param string $output Output file
699
699
* @param int $amount
700
700
* @param int $threshold
701
701
* @return void
702
702
*/
703
- private function img2ocr (? string $ output = null , int $ amount = 80 , int $ threshold = 3 ): void
703
+ private function img2ocr (string $ output , int $ amount = 80 , int $ threshold = 3 ): void
704
704
{
705
705
$ image = $ this ->image ;
706
706
@@ -752,9 +752,7 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
752
752
}
753
753
}
754
754
}
755
- }
756
-
757
- else {
755
+ } else {
758
756
for ($ x = 0 ; $ x < $ this ->width ; $ x ++) { # each row
759
757
for ($ y = 0 ; $ y < $ this ->height ; $ y ++) { # each pixel
760
758
$ rgbOrig = imagecolorat ($ image , $ x , $ y );
@@ -770,14 +768,23 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
770
768
$ bBlur = ($ rgbBlur & 0xFF );
771
769
772
770
$ rNew = ($ amount * ($ rOrig - $ rBlur )) + $ rOrig ;
773
- if ($ rNew > 255 ) { $ rNew = 255 ; }
774
- elseif ($ rNew < 0 ) { $ rNew = 0 ; }
771
+ if ($ rNew > 255 ) {
772
+ $ rNew = 255 ;
773
+ } elseif ($ rNew < 0 ) {
774
+ $ rNew = 0 ;
775
+ }
775
776
$ gNew = ($ amount * ($ gOrig - $ gBlur )) + $ gOrig ;
776
- if ($ gNew > 255 ) { $ gNew = 255 ; }
777
- elseif ($ gNew < 0 ) { $ gNew = 0 ; }
777
+ if ($ gNew > 255 ) {
778
+ $ gNew = 255 ;
779
+ } elseif ($ gNew < 0 ) {
780
+ $ gNew = 0 ;
781
+ }
778
782
$ bNew = ($ amount * ($ bOrig - $ bBlur )) + $ bOrig ;
779
- if ($ bNew > 255 ) { $ bNew = 255 ; }
780
- elseif ($ bNew < 0 ) { $ bNew = 0 ; }
783
+ if ($ bNew > 255 ) {
784
+ $ bNew = 255 ;
785
+ } elseif ($ bNew < 0 ) {
786
+ $ bNew = 0 ;
787
+ }
781
788
$ rgbNew = ($ rNew << 16 ) + ($ gNew << 8 ) + $ bNew ;
782
789
783
790
imagesetpixel ($ image , $ x , $ y , $ rgbNew );
@@ -802,15 +809,11 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol
802
809
$ green = $ this ->round (0.59 * $ colors ['green ' ]);
803
810
$ blue = $ this ->round (0.11 * $ colors ['blue ' ]);
804
811
805
- # Create single-byte string from them
812
+ # Create single-byte string from them
806
813
$ pgm .= chr ($ red + $ green + $ blue );
807
814
}
808
815
}
809
816
810
- if (empty ($ output )) {
811
- $ output = sprintf ('%s/%s.pgm ' , F::dirname ($ file ), F::name ($ file ));
812
- }
813
-
814
817
F::write ($ output , $ pgm );
815
818
}
816
819
@@ -858,9 +861,9 @@ public function isOCRReadable(string $tmpDir = '.tmp'): bool
858
861
# Iterate over available modes ..
859
862
foreach ($ modes as $ mode ) {
860
863
# .. using (suggested) external library (if available), otherwise ..
861
- if ($ mode == 'tesseract ' && class_exists (' \thiagoalessio\ TesseractOCR\TesseractOCR ' )) {
864
+ if ($ mode == 'tesseract ' && class_exists (TesseractOCR::class )) {
862
865
# Execute `tesseract-ocr-for-php` & store its output
863
- $ tesseract = new \ thiagoalessio \ TesseractOCR \ TesseractOCR ($ pgmFile );
866
+ $ tesseract = new TesseractOCR ($ pgmFile );
864
867
$ outputs [] = $ tesseract ->allowlist (range (0 , 9 ), range ('a ' , 'z ' ), range ('A ' , 'Z ' ))->dpi (2200 )->run ();
865
868
}
866
869
@@ -956,13 +959,9 @@ protected function gd2img(int $quality = 90, ?string $filename = null, string $t
956
959
957
960
if ($ type == 'gif ' ) {
958
961
imagegif ($ this ->image , $ filename );
959
- }
960
-
961
- elseif ($ type == 'jpg ' ) {
962
+ } elseif ($ type == 'jpg ' ) {
962
963
imagejpeg ($ this ->image , $ filename , $ quality );
963
- }
964
-
965
- elseif ($ type == 'png ' ) {
964
+ } elseif ($ type == 'png ' ) {
966
965
# Normalize quality
967
966
if ($ quality > 9 ) {
968
967
$ quality = -1 ;
@@ -1041,7 +1040,7 @@ public function inline(int $quality = 90, string $type = 'jpg'): string
1041
1040
1042
1041
/**
1043
1042
* Rounds float to integer
1044
- *
1043
+ *
1045
1044
* @param float $number
1046
1045
* @return int
1047
1046
*/
@@ -1053,7 +1052,7 @@ private function round(float $number): int
1053
1052
1054
1053
/**
1055
1054
* Creates random float between two digits
1056
- *
1055
+ *
1057
1056
* @param float|int $min
1058
1057
* @param float|int $max
1059
1058
* @return float
0 commit comments