3
3
namespace Mews \Captcha ;
4
4
5
5
/**
6
- * Laravel 5 Captcha package
6
+ * Laravel 5 & 6 Captcha package
7
7
*
8
8
* @copyright Copyright (c) 2015 MeWebStudio
9
9
* @version 2.x
18
18
use Illuminate \Config \Repository ;
19
19
use Illuminate \Hashing \BcryptHasher as Hasher ;
20
20
use Illuminate \Filesystem \Filesystem ;
21
- use Illuminate \Support \ Facades \ Crypt ;
21
+ use Illuminate \Http \ File ;
22
22
use Illuminate \Support \Str ;
23
+ use Intervention \Image \Gd \Font ;
24
+ use Intervention \Image \Image ;
23
25
use Intervention \Image \ImageManager ;
24
26
use Illuminate \Session \Store as Session ;
25
27
use Illuminate \Support \HtmlString ;
@@ -66,7 +68,7 @@ class Captcha
66
68
protected $ canvas ;
67
69
68
70
/**
69
- * @var ImageManager->image
71
+ * @var Image
70
72
*/
71
73
protected $ image ;
72
74
@@ -194,8 +196,7 @@ public function __construct(
194
196
Session $ session ,
195
197
Hasher $ hasher ,
196
198
Str $ str
197
- )
198
- {
199
+ ) {
199
200
$ this ->files = $ files ;
200
201
$ this ->config = $ config ;
201
202
$ this ->imageManager = $ imageManager ;
@@ -223,16 +224,18 @@ protected function configure($config)
223
224
* Create captcha image
224
225
*
225
226
* @param string $config
226
- * @param boolean $api
227
- * @return ImageManager->response
227
+ * @param bool $api
228
+ * @return array|mixed
229
+ * @throws Exception
228
230
*/
229
- public function create ($ config = 'default ' , $ api = false )
231
+ public function create (string $ config = 'default ' , bool $ api = false )
230
232
{
231
233
$ this ->backgrounds = $ this ->files ->files (__DIR__ . '/../assets/backgrounds ' );
232
234
$ this ->fonts = $ this ->files ->files ($ this ->fontsDirectory );
233
235
234
236
if (version_compare (app ()->version (), '5.5.0 ' , '>= ' )) {
235
237
$ this ->fonts = array_map (function ($ file ) {
238
+ /* @var File $file */
236
239
return $ file ->getPathName ();
237
240
}, $ this ->fonts );
238
241
}
@@ -272,7 +275,7 @@ public function create($config = 'default', $api = false)
272
275
$ this ->image ->sharpen ($ this ->sharpen );
273
276
}
274
277
if ($ this ->invert ) {
275
- $ this ->image ->invert ($ this -> invert );
278
+ $ this ->image ->invert ();
276
279
}
277
280
if ($ this ->blur ) {
278
281
$ this ->image ->blur ($ this ->blur );
@@ -290,22 +293,22 @@ public function create($config = 'default', $api = false)
290
293
*
291
294
* @return string
292
295
*/
293
- protected function background ()
296
+ protected function background (): string
294
297
{
295
298
return $ this ->backgrounds [rand (0 , count ($ this ->backgrounds ) - 1 )];
296
299
}
297
300
298
301
/**
299
302
* Generate captcha text
300
303
*
301
- * @return string
304
+ * @return array
305
+ * @throws Exception
302
306
*/
303
- protected function generate ()
307
+ protected function generate (): array
304
308
{
305
309
$ characters = is_string ($ this ->characters ) ? str_split ($ this ->characters ) : $ this ->characters ;
306
310
307
311
$ bag = [];
308
- $ key = '' ;
309
312
310
313
if ($ this ->math ) {
311
314
$ x = random_int (10 , 30 );
@@ -336,8 +339,10 @@ protected function generate()
336
339
337
340
/**
338
341
* Writing captcha text
342
+ *
343
+ * @return void
339
344
*/
340
- protected function text ()
345
+ protected function text (): void
341
346
{
342
347
$ marginTop = $ this ->image ->height () / $ this ->length ;
343
348
@@ -350,6 +355,7 @@ protected function text()
350
355
$ marginLeft = $ this ->textLeftPadding + ($ key * ($ this ->image ->width () - $ this ->textLeftPadding ) / $ this ->length );
351
356
352
357
$ this ->image ->text ($ char , $ marginLeft , $ marginTop , function ($ font ) {
358
+ /* @var Font $font */
353
359
$ font ->file ($ this ->font ());
354
360
$ font ->size ($ this ->fontSize ());
355
361
$ font ->color ($ this ->fontColor ());
@@ -365,32 +371,32 @@ protected function text()
365
371
*
366
372
* @return string
367
373
*/
368
- protected function font ()
374
+ protected function font (): string
369
375
{
370
376
return $ this ->fonts [rand (0 , count ($ this ->fonts ) - 1 )];
371
377
}
372
378
373
379
/**
374
380
* Random font size
375
381
*
376
- * @return integer
382
+ * @return int
377
383
*/
378
- protected function fontSize ()
384
+ protected function fontSize (): int
379
385
{
380
386
return rand ($ this ->image ->height () - 10 , $ this ->image ->height ());
381
387
}
382
388
383
389
/**
384
390
* Random font color
385
391
*
386
- * @return array
392
+ * @return string
387
393
*/
388
- protected function fontColor ()
394
+ protected function fontColor (): string
389
395
{
390
396
if (!empty ($ this ->fontColors )) {
391
397
$ color = $ this ->fontColors [rand (0 , count ($ this ->fontColors ) - 1 )];
392
398
} else {
393
- $ color = [ rand ( 0 , 255 ), rand ( 0 , 255 ), rand ( 0 , 255 )] ;
399
+ $ color = ' # ' . str_pad ( dechex ( mt_rand ( 0 , 0xFFFFFF )), 6 , ' 0 ' , STR_PAD_LEFT ) ;
394
400
}
395
401
396
402
return $ color ;
@@ -401,15 +407,15 @@ protected function fontColor()
401
407
*
402
408
* @return int
403
409
*/
404
- protected function angle ()
410
+ protected function angle (): int
405
411
{
406
412
return rand ((-1 * $ this ->angle ), $ this ->angle );
407
413
}
408
414
409
415
/**
410
416
* Random image lines
411
417
*
412
- * @return \Intervention\ Image\Image
418
+ * @return Image|ImageManager
413
419
*/
414
420
protected function lines ()
415
421
{
@@ -420,6 +426,7 @@ protected function lines()
420
426
rand (0 , $ this ->image ->width ()),
421
427
rand (0 , $ this ->image ->height ()),
422
428
function ($ draw ) {
429
+ /* @var Font $draw */
423
430
$ draw ->color ($ this ->fontColor ());
424
431
}
425
432
);
@@ -431,10 +438,10 @@ function ($draw) {
431
438
/**
432
439
* Captcha check
433
440
*
434
- * @param $value
441
+ * @param string $value
435
442
* @return bool
436
443
*/
437
- public function check ($ value )
444
+ public function check (string $ value ): bool
438
445
{
439
446
if (!$ this ->session ->has ('captcha ' )) {
440
447
return false ;
@@ -448,7 +455,7 @@ public function check($value)
448
455
}
449
456
450
457
$ check = $ this ->hasher ->check ($ value , $ key );
451
- // if verify pass,remove session
458
+ // if verify pass,remove session
452
459
if ($ check ) {
453
460
$ this ->session ->remove ('captcha ' );
454
461
}
@@ -459,34 +466,35 @@ public function check($value)
459
466
/**
460
467
* Captcha check
461
468
*
462
- * @param $value
469
+ * @param string $value
470
+ * @param string $key
463
471
* @return bool
464
472
*/
465
- public function check_api ($ value , $ key )
473
+ public function check_api ($ value , $ key ): bool
466
474
{
467
475
return $ this ->hasher ->check ($ value , $ key );
468
476
}
469
477
470
478
/**
471
479
* Generate captcha image source
472
480
*
473
- * @param null $config
481
+ * @param string| null $config
474
482
* @return string
475
483
*/
476
- public function src ($ config = null )
484
+ public function src (? string $ config): string
477
485
{
478
486
return url ('captcha ' . ($ config ? '/ ' . $ config : '/default ' )) . '? ' . $ this ->str ->random (8 );
479
487
}
480
488
481
489
/**
482
490
* Generate captcha image html tag
483
491
*
484
- * @param null $config
485
- * @param array $attrs HTML attributes supplied to the image tag where key is the attribute
486
- * and the value is the attribute value
492
+ * @param string| null $config
493
+ * @param array|null $attrs
494
+ * $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value
487
495
* @return string
488
496
*/
489
- public function img ($ config = null , $ attrs = [])
497
+ public function img (? string $ config, ? array $ attrs): string
490
498
{
491
499
$ attrs_str = '' ;
492
500
foreach ($ attrs as $ attr => $ value ) {
0 commit comments