30
30
*/
31
31
class Captcha
32
32
{
33
-
34
33
/**
35
34
* @var Filesystem
36
35
*/
@@ -198,7 +197,7 @@ public function __construct(
198
197
$ this ->session = $ session ;
199
198
$ this ->hasher = $ hasher ;
200
199
$ this ->str = $ str ;
201
- $ this ->characters = config ('captcha.characters ' ,['1 ' ,'2 ' ,'3 ' ,'4 ' ,'6 ' ,'7 ' ,'8 ' ,'9 ' ]);
200
+ $ this ->characters = config ('captcha.characters ' , ['1 ' , '2 ' , '3 ' , '4 ' , '6 ' , '7 ' , '8 ' , '9 ' ]);
202
201
}
203
202
204
203
/**
@@ -207,10 +206,8 @@ public function __construct(
207
206
*/
208
207
protected function configure ($ config )
209
208
{
210
- if ($ this ->config ->has ('captcha. ' . $ config ))
211
- {
212
- foreach ($ this ->config ->get ('captcha. ' . $ config ) as $ key => $ val )
213
- {
209
+ if ($ this ->config ->has ('captcha. ' . $ config )) {
210
+ foreach ($ this ->config ->get ('captcha. ' . $ config ) as $ key => $ val ) {
214
211
$ this ->{$ key } = $ val ;
215
212
}
216
213
}
@@ -228,8 +225,8 @@ public function create($config = 'default', $api = false)
228
225
$ this ->backgrounds = $ this ->files ->files (__DIR__ . '/../assets/backgrounds ' );
229
226
$ this ->fonts = $ this ->files ->files (__DIR__ . '/../assets/fonts ' );
230
227
231
- if (app ()->version () >= 5.5 ){
232
- $ this ->fonts = array_map (function ($ file ) {
228
+ if (app ()->version () >= 5.5 ) {
229
+ $ this ->fonts = array_map (function ($ file ) {
233
230
return $ file ->getPathName ();
234
231
}, $ this ->fonts );
235
232
}
@@ -247,45 +244,38 @@ public function create($config = 'default', $api = false)
247
244
$ this ->bgColor
248
245
);
249
246
250
- if ($ this ->bgImage )
251
- {
247
+ if ($ this ->bgImage ) {
252
248
$ this ->image = $ this ->imageManager ->make ($ this ->background ())->resize (
253
249
$ this ->width ,
254
250
$ this ->height
255
251
);
256
252
$ this ->canvas ->insert ($ this ->image );
257
- }
258
- else
259
- {
253
+ } else {
260
254
$ this ->image = $ this ->canvas ;
261
255
}
262
256
263
- if ($ this ->contrast != 0 )
264
- {
257
+ if ($ this ->contrast != 0 ) {
265
258
$ this ->image ->contrast ($ this ->contrast );
266
259
}
267
260
268
261
$ this ->text ();
269
262
270
263
$ this ->lines ();
271
264
272
- if ($ this ->sharpen )
273
- {
265
+ if ($ this ->sharpen ) {
274
266
$ this ->image ->sharpen ($ this ->sharpen );
275
267
}
276
- if ($ this ->invert )
277
- {
268
+ if ($ this ->invert ) {
278
269
$ this ->image ->invert ($ this ->invert );
279
270
}
280
- if ($ this ->blur )
281
- {
271
+ if ($ this ->blur ) {
282
272
$ this ->image ->blur ($ this ->blur );
283
273
}
284
274
285
275
return $ api ? [
286
- 'sensitive ' => $ generator ['sensitive ' ],
287
- ' key ' => $ generator ['key ' ],
288
- ' img ' => $ this ->image ->encode ('data-url ' )->encoded
276
+ 'sensitive ' => $ generator ['sensitive ' ],
277
+ ' key ' => $ generator ['key ' ],
278
+ ' img ' => $ this ->image ->encode ('data-url ' )->encoded
289
279
] : $ this ->image ->response ('png ' , $ this ->quality );
290
280
}
291
281
@@ -319,7 +309,7 @@ protected function generate()
319
309
$ key .= '' ;
320
310
} else {
321
311
for ($ i = 0 ; $ i < $ this ->length ; $ i ++) {
322
- $ char = $ characters [rand (0 , count ($ characters ) - 1 )];
312
+ $ char = $ characters [rand (0 , count ($ characters ) - 1 )];
323
313
$ bag [] = $ this ->sensitive ? $ char : $ this ->str ->lower ($ char );
324
314
}
325
315
$ key = implode ('' , $ bag );
@@ -328,13 +318,13 @@ protected function generate()
328
318
$ hash = $ this ->hasher ->make ($ key );
329
319
$ this ->session ->put ('captcha ' , [
330
320
'sensitive ' => $ this ->sensitive ,
331
- 'key ' => $ hash
321
+ 'key ' => $ hash
332
322
]);
333
323
334
324
return [
335
- ' value ' => $ bag ,
336
- 'sensitive ' => $ this ->sensitive ,
337
- ' key ' => $ hash
325
+ ' value ' => $ bag ,
326
+ 'sensitive ' => $ this ->sensitive ,
327
+ ' key ' => $ hash
338
328
];
339
329
}
340
330
@@ -343,17 +333,17 @@ protected function generate()
343
333
*/
344
334
protected function text ()
345
335
{
346
- $ marginTop = $ this ->image ->height () / $ this ->length ;
336
+ $ marginTop = $ this ->image ->height () / $ this ->length ;
347
337
348
- $ text = $ this ->text ;
349
- if (is_string ($ text )) {
338
+ $ text = $ this ->text ;
339
+ if (is_string ($ text )) {
350
340
$ text = str_split ($ text );
351
341
}
352
342
353
- foreach ($ text as $ key => $ char ) {
354
- $ marginLeft = $ this ->textLeftPadding + ($ key * ($ this ->image ->width () - $ this ->textLeftPadding ) / $ this ->length );
343
+ foreach ($ text as $ key => $ char ) {
344
+ $ marginLeft = $ this ->textLeftPadding + ($ key * ($ this ->image ->width () - $ this ->textLeftPadding ) / $ this ->length );
355
345
356
- $ this ->image ->text ($ char , $ marginLeft , $ marginTop , function ($ font ) {
346
+ $ this ->image ->text ($ char , $ marginLeft , $ marginTop , function ($ font ) {
357
347
$ font ->file ($ this ->font ());
358
348
$ font ->size ($ this ->fontSize ());
359
349
$ font ->color ($ this ->fontColor ());
@@ -391,12 +381,9 @@ protected function fontSize()
391
381
*/
392
382
protected function fontColor ()
393
383
{
394
- if ( ! empty ($ this ->fontColors ))
395
- {
384
+ if (!empty ($ this ->fontColors )) {
396
385
$ color = $ this ->fontColors [rand (0 , count ($ this ->fontColors ) - 1 )];
397
- }
398
- else
399
- {
386
+ } else {
400
387
$ color = [rand (0 , 255 ), rand (0 , 255 ), rand (0 , 255 )];
401
388
}
402
389
@@ -420,8 +407,7 @@ protected function angle()
420
407
*/
421
408
protected function lines ()
422
409
{
423
- for ($ i = 0 ; $ i <= $ this ->lines ; $ i ++)
424
- {
410
+ for ($ i = 0 ; $ i <= $ this ->lines ; $ i ++) {
425
411
$ this ->image ->line (
426
412
rand (0 , $ this ->image ->width ()) + $ i * rand (0 , $ this ->image ->height ()),
427
413
rand (0 , $ this ->image ->height ()),
@@ -432,50 +418,48 @@ function ($draw) {
432
418
}
433
419
);
434
420
}
421
+
435
422
return $ this ->image ;
436
423
}
437
424
438
- /**
439
- * Captcha check
440
- *
441
- * @param $value
442
- * @return bool
443
- */
444
- public function check ($ value )
445
- {
446
- if ( ! $ this ->session ->has ('captcha ' ))
447
- {
448
- return false ;
449
- }
450
-
451
- $ key = $ this ->session ->get ('captcha.key ' );
452
- $ sensitive = $ this ->session ->get ('captcha.sensitive ' );
453
-
454
- if ( ! $ sensitive )
455
- {
456
- $ value = $ this ->str ->lower ($ value );
457
- }
458
-
459
-
460
- $ res = $ this ->hasher ->check ($ value , $ key );
425
+ /**
426
+ * Captcha check
427
+ *
428
+ * @param $value
429
+ * @return bool
430
+ */
431
+ public function check ($ value )
432
+ {
433
+ if (!$ this ->session ->has ('captcha ' )) {
434
+ return false ;
435
+ }
436
+
437
+ $ key = $ this ->session ->get ('captcha.key ' );
438
+ $ sensitive = $ this ->session ->get ('captcha.sensitive ' );
439
+
440
+ if (!$ sensitive ) {
441
+ $ value = $ this ->str ->lower ($ value );
442
+ }
443
+
444
+ $ check = $ this ->hasher ->check ($ value , $ key );
461
445
// if verify pass,remove session
462
- if ($ res ) {
446
+ if ($ check ) {
463
447
$ this ->session ->remove ('captcha ' );
464
448
}
465
449
466
- return $ res ;
467
- }
450
+ return $ check ;
451
+ }
468
452
469
- /**
470
- * Captcha check
471
- *
472
- * @param $value
473
- * @return bool
474
- */
475
- public function check_api ($ value , $ key )
476
- {
477
- return $ this ->hasher ->check ($ value , $ key );
478
- }
453
+ /**
454
+ * Captcha check
455
+ *
456
+ * @param $value
457
+ * @return bool
458
+ */
459
+ public function check_api ($ value , $ key )
460
+ {
461
+ return $ this ->hasher ->check ($ value , $ key );
462
+ }
479
463
480
464
/**
481
465
* Generate captcha image source
@@ -499,14 +483,14 @@ public function src($config = null)
499
483
public function img ($ config = null , $ attrs = [])
500
484
{
501
485
$ attrs_str = '' ;
502
- foreach ($ attrs as $ attr => $ value ){
503
- if ($ attr == 'src ' ){
486
+ foreach ($ attrs as $ attr => $ value ) {
487
+ if ($ attr == 'src ' ) {
504
488
//Neglect src attribute
505
489
continue ;
506
490
}
507
- $ attrs_str .= $ attr .'=" ' .$ value .'" ' ;
491
+
492
+ $ attrs_str .= $ attr . '=" ' . $ value . '" ' ;
508
493
}
509
- return new HtmlString ('<img src=" ' . $ this ->src ($ config ). '" ' . trim ($ attrs_str ). '> ' );
494
+ return new HtmlString ('<img src=" ' . $ this ->src ($ config ) . '" ' . trim ($ attrs_str ) . '> ' );
510
495
}
511
-
512
496
}
0 commit comments