├── assets ├── backgrounds │ ├── 01.png │ ├── 02.png │ ├── 03.png │ ├── 04.png │ ├── 05.png │ ├── 06.png │ ├── 07.png │ ├── 08.png │ ├── 09.png │ ├── 10.png │ ├── 11.png │ └── 12.png └── fonts │ ├── Asap_700.ttf │ ├── Khand_500.ttf │ ├── Roboto_regular.ttf │ ├── Ubuntu_regular.ttf │ ├── ABeeZee_regular.ttf │ ├── Open_Sans_regular.ttf │ └── license │ ├── OFL.txt │ ├── ubuntu-font-licence-1.0.txt │ └── LICENSE-2.0.txt ├── LICENCE.md ├── src ├── Facades │ └── Captcha.php ├── LumenCaptchaController.php ├── CaptchaController.php ├── helpers.php ├── CaptchaServiceProvider.php └── Captcha.php ├── LICENSE ├── composer.json ├── config └── captcha.php └── README.md /assets/backgrounds/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/01.png -------------------------------------------------------------------------------- /assets/backgrounds/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/02.png -------------------------------------------------------------------------------- /assets/backgrounds/03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/03.png -------------------------------------------------------------------------------- /assets/backgrounds/04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/04.png -------------------------------------------------------------------------------- /assets/backgrounds/05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/05.png -------------------------------------------------------------------------------- /assets/backgrounds/06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/06.png -------------------------------------------------------------------------------- /assets/backgrounds/07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/07.png -------------------------------------------------------------------------------- /assets/backgrounds/08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/08.png -------------------------------------------------------------------------------- /assets/backgrounds/09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/09.png -------------------------------------------------------------------------------- /assets/backgrounds/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/10.png -------------------------------------------------------------------------------- /assets/backgrounds/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/11.png -------------------------------------------------------------------------------- /assets/backgrounds/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/backgrounds/12.png -------------------------------------------------------------------------------- /assets/fonts/Asap_700.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/Asap_700.ttf -------------------------------------------------------------------------------- /assets/fonts/Khand_500.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/Khand_500.ttf -------------------------------------------------------------------------------- /assets/fonts/Roboto_regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/Roboto_regular.ttf -------------------------------------------------------------------------------- /assets/fonts/Ubuntu_regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/Ubuntu_regular.ttf -------------------------------------------------------------------------------- /assets/fonts/ABeeZee_regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/ABeeZee_regular.ttf -------------------------------------------------------------------------------- /assets/fonts/Open_Sans_regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mewebstudio/captcha/HEAD/assets/fonts/Open_Sans_regular.ttf -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Muharrem ERİN (me@mewebstudio.com) 2 | 3 | http://www.opensource.org/licenses/mit-license.php The MIT License 4 | -------------------------------------------------------------------------------- /src/Facades/Captcha.php: -------------------------------------------------------------------------------- 1 | create($config); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/CaptchaController.php: -------------------------------------------------------------------------------- 1 | create($config); 29 | } 30 | 31 | /** 32 | * get CAPTCHA api 33 | * 34 | * @param Captcha $captcha 35 | * @param string $config 36 | * @return array|mixed 37 | * @throws Exception 38 | */ 39 | public function getCaptchaApi(Captcha $captcha, string $config = 'default') 40 | { 41 | return $captcha->create($config, true); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 MeWebStudio - Muharrem ERİN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mews/captcha", 3 | "type": "package", 4 | "description": "Laravel 5/6/7/8/9/10/11/12 Captcha Package", 5 | "keywords": ["laravel12 Captcha", "laravel12 Security", "laravel12 Captcha", "laravel5 Security", "Captcha"], 6 | "homepage": "https://github.com/mewebstudio/captcha", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Muharrem ERİN", 11 | "email": "me@mewebstudio.com", 12 | "homepage": "https://github.com/mewebstudio", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": "^7.2|^8.1|^8.2|^8.3", 18 | "ext-gd": "*", 19 | "illuminate/config": "~5|^6|^7|^8|^9|^10|^11|^12", 20 | "illuminate/filesystem": "~5|^6|^7|^8|^9|^10|^11|^12", 21 | "illuminate/support": "~5|^6|^7|^8|^9|^10|^11|^12", 22 | "illuminate/hashing": "~5|^6|^7|^8|^9|^10|^11|^12", 23 | "illuminate/session": "~5|^6|^7|^8|^9|^10|^11|^12", 24 | "intervention/image": "^3.7" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": "^8.5|^9.5.10|^10.5|^11", 28 | "mockery/mockery": "^1.0" 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Mews\\Captcha\\": "src/" 33 | }, 34 | "files": [ 35 | "src/helpers.php" 36 | ] 37 | }, 38 | "autoload-dev": { 39 | "classmap": [ 40 | "tests" 41 | ], 42 | "psr-4": { 43 | "Mews\\Test\\": "tests/" 44 | } 45 | }, 46 | "minimum-stability": "stable", 47 | "extra": { 48 | "laravel": { 49 | "providers": [ 50 | "Mews\\Captcha\\CaptchaServiceProvider" 51 | ], 52 | "aliases": { 53 | "Captcha": "Mews\\Captcha\\Facades\\Captcha" 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | create($config); 14 | } 15 | } 16 | 17 | if (!function_exists('captcha_src')) { 18 | /** 19 | * @param string $config 20 | * @return string 21 | */ 22 | function captcha_src(string $config = 'default'): string 23 | { 24 | return app('captcha')->src($config); 25 | } 26 | } 27 | 28 | if (!function_exists('captcha_img')) { 29 | 30 | /** 31 | * @param string $config 32 | * @param array $attrs 33 | * @return string 34 | */ 35 | function captcha_img(string $config = 'default', array $attrs = []): string 36 | { 37 | return app('captcha')->img($config, $attrs); 38 | } 39 | } 40 | 41 | if (!function_exists('captcha_check')) { 42 | /** 43 | * @param string $value 44 | * @return bool 45 | */ 46 | function captcha_check(string $value): bool 47 | { 48 | return app('captcha')->check($value); 49 | } 50 | } 51 | 52 | if (!function_exists('captcha_api_check')) { 53 | /** 54 | * @param string $value 55 | * @param string $key 56 | * @param string $config 57 | * @return bool 58 | */ 59 | function captcha_api_check(string $value, string $key, string $config = 'default'): bool 60 | { 61 | return app('captcha')->check_api($value, $key, $config); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /config/captcha.php: -------------------------------------------------------------------------------- 1 | env('CAPTCHA_DISABLE', false), 5 | 'characters' => ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 6 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 7 | 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 8 | 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 9 | 'fontsDirectory' => dirname(__DIR__) . '/assets/fonts', 10 | 'bgsDirectory' => dirname(__DIR__) . '/assets/backgrounds', 11 | 'default' => [ 12 | 'length' => 6, 13 | 'width' => 345, 14 | 'height' => 65, 15 | 'quality' => 90, 16 | 'math' => false, 17 | 'expire' => 60, 18 | 'encrypt' => false, 19 | ], 20 | 'flat' => [ 21 | 'length' => 6, 22 | 'fontColors' => ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'], 23 | 'width' => 345, 24 | 'height' => 65, 25 | 'math' => false, 26 | 'quality' => 100, 27 | 'lines' => 6, 28 | 'bgImage' => true, 29 | 'bgColor' => '#28faef', 30 | 'contrast' => 0, 31 | ], 32 | 'mini' => [ 33 | 'length' => 3, 34 | 'width' => 60, 35 | 'height' => 32, 36 | ], 37 | 'inverse' => [ 38 | 'length' => 5, 39 | 'width' => 120, 40 | 'height' => 36, 41 | 'quality' => 90, 42 | 'sensitive' => true, 43 | 'angle' => 12, 44 | 'sharpen' => 10, 45 | 'blur' => 2, 46 | 'invert' => false, 47 | 'contrast' => -5, 48 | ], 49 | 'math' => [ 50 | 'length' => 9, 51 | 'width' => 120, 52 | 'height' => 36, 53 | 'quality' => 90, 54 | ], 55 | ]; 56 | -------------------------------------------------------------------------------- /src/CaptchaServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 27 | __DIR__ . '/../config/captcha.php' => config_path('captcha.php') 28 | ], 'config'); 29 | 30 | // HTTP routing 31 | if(!config('captcha.disable')){ 32 | if (strpos($this->app->version(), 'Lumen') !== false) { 33 | /* @var Router $router */ 34 | $router = $this->app; 35 | $router->get('captcha[/api/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptchaApi'); 36 | $router->get('captcha[/{config}]', 'Mews\Captcha\LumenCaptchaController@getCaptcha'); 37 | } else { 38 | /* @var Router $router */ 39 | $router = $this->app['router']; 40 | if ((double)$this->app->version() >= 5.2) { 41 | $router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi')->middleware('web'); 42 | $router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha')->middleware('web'); 43 | } else { 44 | $router->get('captcha/api/{config?}', '\Mews\Captcha\CaptchaController@getCaptchaApi'); 45 | $router->get('captcha/{config?}', '\Mews\Captcha\CaptchaController@getCaptcha'); 46 | } 47 | } 48 | } 49 | 50 | /* @var Factory $validator */ 51 | $validator = $this->app['validator']; 52 | 53 | // Validator extensions 54 | $validator->extend('captcha', function ($attribute, $value, $parameters) { 55 | return config('captcha.disable') || ($value && captcha_check($value)); 56 | }); 57 | 58 | // Validator extensions 59 | $validator->extend('captcha_api', function ($attribute, $value, $parameters) { 60 | return config('captcha.disable') || ($value && captcha_api_check($value, $parameters[0], $parameters[1] ?? 'default')); 61 | }); 62 | } 63 | 64 | /** 65 | * Register the service provider. 66 | * 67 | * @return void 68 | */ 69 | public function register(): void 70 | { 71 | // Merge configs 72 | $this->mergeConfigFrom( 73 | __DIR__ . '/../config/captcha.php', 74 | 'captcha' 75 | ); 76 | 77 | // Bind the ImageManager with an explicit driver 78 | if (!$this->app->bound('Intervention\Image\ImageManager')) { 79 | $this->app->singleton('Intervention\Image\ImageManager', function ($app) { 80 | // Determine which driver to use, defaulting to 'gd' 81 | $driver = config('captcha.driver', 'gd') === 'imagick' ? new ImagickDriver() : new GdDriver(); 82 | 83 | return new ImageManager($driver); 84 | }); 85 | } 86 | 87 | // Bind captcha 88 | $this->app->bind('captcha', function ($app) { 89 | return new Captcha( 90 | $app['Illuminate\Filesystem\Filesystem'], 91 | $app['Illuminate\Contracts\Config\Repository'], 92 | $app['Intervention\Image\ImageManager'], 93 | $app['Illuminate\Session\Store'], 94 | $app['Illuminate\Hashing\BcryptHasher'], 95 | $app['Illuminate\Support\Str'] 96 | ); 97 | }); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /assets/fonts/license/OFL.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) , (), with Reserved Font Name . Copyright (c) , (), with Reserved Font Name . Copyright (c) , (). This Font Software is licensed under the SIL Open Font License, Version 1.1. This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL ----------------------------------------------------------- SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 ----------------------------------------------------------- PREAMBLE The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others. The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives. DEFINITIONS "Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation. "Reserved Font Name" refers to any names specified as such after the copyright statement(s). "Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s). "Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment. "Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software. PERMISSION & CONDITIONS Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions: 1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself. 2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user. 3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users. 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission. 5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software. TERMINATION This license becomes null and void if any of the above conditions are not met. DISCLAIMER THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE. -------------------------------------------------------------------------------- /assets/fonts/license/ubuntu-font-licence-1.0.txt: -------------------------------------------------------------------------------- 1 | ------------------------------- 2 | UBUNTU FONT LICENCE Version 1.0 3 | ------------------------------- 4 | 5 | PREAMBLE 6 | This licence allows the licensed fonts to be used, studied, modified and 7 | redistributed freely. The fonts, including any derivative works, can be 8 | bundled, embedded, and redistributed provided the terms of this licence 9 | are met. The fonts and derivatives, however, cannot be released under 10 | any other licence. The requirement for fonts to remain under this 11 | licence does not require any document created using the fonts or their 12 | derivatives to be published under this licence, as long as the primary 13 | purpose of the document is not to be a vehicle for the distribution of 14 | the fonts. 15 | 16 | DEFINITIONS 17 | "Font Software" refers to the set of files released by the Copyright 18 | Holder(s) under this licence and clearly marked as such. This may 19 | include source files, build scripts and documentation. 20 | 21 | "Original Version" refers to the collection of Font Software components 22 | as received under this licence. 23 | 24 | "Modified Version" refers to any derivative made by adding to, deleting, 25 | or substituting -- in part or in whole -- any of the components of the 26 | Original Version, by changing formats or by porting the Font Software to 27 | a new environment. 28 | 29 | "Copyright Holder(s)" refers to all individuals and companies who have a 30 | copyright ownership of the Font Software. 31 | 32 | "Substantially Changed" refers to Modified Versions which can be easily 33 | identified as dissimilar to the Font Software by users of the Font 34 | Software comparing the Original Version with the Modified Version. 35 | 36 | To "Propagate" a work means to do anything with it that, without 37 | permission, would make you directly or secondarily liable for 38 | infringement under applicable copyright law, except executing it on a 39 | computer or modifying a private copy. Propagation includes copying, 40 | distribution (with or without modification and with or without charging 41 | a redistribution fee), making available to the public, and in some 42 | countries other activities as well. 43 | 44 | PERMISSION & CONDITIONS 45 | This licence does not grant any rights under trademark law and all such 46 | rights are reserved. 47 | 48 | Permission is hereby granted, free of charge, to any person obtaining a 49 | copy of the Font Software, to propagate the Font Software, subject to 50 | the below conditions: 51 | 52 | 1) Each copy of the Font Software must contain the above copyright 53 | notice and this licence. These can be included either as stand-alone 54 | text files, human-readable headers or in the appropriate machine- 55 | readable metadata fields within text or binary files as long as those 56 | fields can be easily viewed by the user. 57 | 58 | 2) The font name complies with the following: 59 | (a) The Original Version must retain its name, unmodified. 60 | (b) Modified Versions which are Substantially Changed must be renamed to 61 | avoid use of the name of the Original Version or similar names entirely. 62 | (c) Modified Versions which are not Substantially Changed must be 63 | renamed to both (i) retain the name of the Original Version and (ii) add 64 | additional naming elements to distinguish the Modified Version from the 65 | Original Version. The name of such Modified Versions must be the name of 66 | the Original Version, with "derivative X" where X represents the name of 67 | the new work, appended to that name. 68 | 69 | 3) The name(s) of the Copyright Holder(s) and any contributor to the 70 | Font Software shall not be used to promote, endorse or advertise any 71 | Modified Version, except (i) as required by this licence, (ii) to 72 | acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with 73 | their explicit written permission. 74 | 75 | 4) The Font Software, modified or unmodified, in part or in whole, must 76 | be distributed entirely under this licence, and must not be distributed 77 | under any other licence. The requirement for fonts to remain under this 78 | licence does not affect any document created using the Font Software, 79 | except any version of the Font Software extracted from a document 80 | created using the Font Software may only be distributed under this 81 | licence. 82 | 83 | TERMINATION 84 | This licence becomes null and void if any of the above conditions are 85 | not met. 86 | 87 | DISCLAIMER 88 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 89 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 90 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 91 | COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 92 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 93 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 94 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 95 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER 96 | DEALINGS IN THE FONT SOFTWARE. 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Captcha for Laravel 10/11/12 2 | 3 | [![Build Status](https://travis-ci.org/mewebstudio/captcha.svg?branch=master)](https://travis-ci.org/mewebstudio/captcha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mewebstudio/captcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mewebstudio/captcha/?branch=master) 4 | [![Latest Stable Version](https://poser.pugx.org/mews/captcha/v/stable.svg)](https://packagist.org/packages/mews/captcha) 5 | [![Latest Unstable Version](https://poser.pugx.org/mews/captcha/v/unstable.svg)](https://packagist.org/packages/mews/captcha) 6 | [![License](https://poser.pugx.org/mews/captcha/license.svg)](https://packagist.org/packages/mews/captcha) 7 | [![Total Downloads](https://poser.pugx.org/mews/captcha/downloads.svg)](https://packagist.org/packages/mews/captcha) 8 | 9 | A simple [Laravel 5/6/7/8/9/10/11/12](http://www.laravel.com/) service provider for including the [Captcha for Laravel](https://github.com/mewebstudio/captcha). 10 | 11 | for Laravel 4 [Captcha for Laravel Laravel 4](https://github.com/mewebstudio/captcha/tree/master-l4) 12 | 13 | for Laravel 5 to 12 [Captcha for Laravel Laravel 5 and Newer versions](https://github.com/mewebstudio/captcha/tree/master-l5-l9) 14 | 15 | ## Preview 16 | ![Preview](https://image.ibb.co/kZxMLm/image.png) 17 | 18 | - [Captcha for Laravel 5/6/7/8/9/10/11/12](#captcha-for-laravel-5-6-7) 19 | * [Preview](#preview) 20 | * [Installation](#installation) 21 | * [Usage](#usage) 22 | * [Configuration](#configuration) 23 | + [Custom settings:](#custom-settings) 24 | + [Disable validation:](#disable-validation) 25 | * [Example Usage](#example-usage) 26 | + [Session Mode:](#session-mode) 27 | + [Stateless Mode:](#stateless-mode) 28 | - [Return Image](#return-image) 29 | - [Return URL](#return-url) 30 | - [Return HTML](#return-html) 31 | - [To use different configurations](#to-use-different-configurations) 32 | * [Links](#links) 33 | 34 | ## Installation 35 | 36 | The Captcha Service Provider can be installed via [Composer](http://getcomposer.org) by requiring the 37 | `mews/captcha` package and setting the `minimum-stability` to `dev` (required for Laravel 5) in your 38 | project's `composer.json`. 39 | 40 | ```json 41 | { 42 | "require": { 43 | "laravel/framework": "5.0.*", 44 | "mews/captcha": "~3.0" 45 | }, 46 | "minimum-stability": "stable" 47 | } 48 | ``` 49 | 50 | or 51 | 52 | Require this package with composer: 53 | ``` 54 | composer require mews/captcha 55 | ``` 56 | 57 | Update your packages with ```composer update``` or install with ```composer install```. 58 | 59 | In Windows, you'll need to include the GD2 DLL `php_gd2.dll` in php.ini. And you also need include `php_fileinfo.dll` and `php_mbstring.dll` to fit the requirements of `mews/captcha`'s dependencies. 60 | 61 | 62 | 63 | 64 | ## Usage 65 | 66 | To use the Captcha Service Provider, you must register the provider when bootstrapping your Laravel application. There are 67 | essentially two ways to do this. 68 | 69 | Find the `providers` key in `config/app.php` and register the Captcha Service Provider. 70 | 71 | ```php 72 | 'providers' => [ 73 | // ... 74 | 'Mews\Captcha\CaptchaServiceProvider', 75 | ] 76 | ``` 77 | for Laravel 5.1+ 78 | ```php 79 | 'providers' => [ 80 | // ... 81 | Mews\Captcha\CaptchaServiceProvider::class, 82 | ] 83 | ``` 84 | For Laravel 11+ you can add the provider to `bootstrap\providers.php`. 85 | ```php 86 | return [ 87 | // ... 88 | Mews\Captcha\CaptchaServiceProvider::class 89 | ]; 90 | ``` 91 | 92 | Find the `aliases` key in `config/app.php`. 93 | 94 | ```php 95 | 'aliases' => [ 96 | // ... 97 | 'Captcha' => 'Mews\Captcha\Facades\Captcha', 98 | ] 99 | ``` 100 | for Laravel 5.1+ 101 | ```php 102 | 'aliases' => [ 103 | // ... 104 | 'Captcha' => Mews\Captcha\Facades\Captcha::class, 105 | ] 106 | ``` 107 | 108 | For Laravel 11+ : you do not need to add the alias, it will be added automatically. 109 | 110 | 111 | ## Configuration 112 | ### Custom settings: 113 | To use your own settings, publish config. 114 | 115 | ```$ php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"``` 116 | 117 | `config/captcha.php` 118 | 119 | ```php 120 | return [ 121 | 'default' => [ 122 | 'length' => 5, 123 | 'width' => 120, 124 | 'height' => 36, 125 | 'quality' => 90, 126 | 'math' => true, //Enable Math Captcha 127 | 'expire' => 60, //Captcha expiration 128 | ], 129 | // ... 130 | ]; 131 | ``` 132 | ### Images 133 | To use your own custom images for a background, set 'bgImage' to true and change the 'bgsDirectory' setting to your directory you want the image(s) to be used. 134 | If you just want to change the background color, then set 'bgImage' to false and the 'bgColor' will be applied. 135 | 136 | ### Disable validation: 137 | To disable the captcha validation use `CAPTCHA_DISABLE` environment variable. e.g. **.env** config: 138 | 139 | ```php 140 | CAPTCHA_DISABLE=true 141 | ``` 142 | 143 | 144 | ## Example Usage 145 | ### Session Mode: 146 | ```php 147 | // [your site path]/Http/routes.php 148 | Route::any('captcha-test', function() { 149 | if (request()->getMethod() == 'POST') { 150 | $rules = ['captcha' => 'required|captcha']; 151 | $validator = validator()->make(request()->all(), $rules); 152 | if ($validator->fails()) { 153 | echo '

Incorrect!

'; 154 | } else { 155 | echo '

Matched :)

'; 156 | } 157 | } 158 | 159 | $form = '
'; 160 | $form .= ''; 161 | $form .= '

' . captcha_img() . '

'; 162 | $form .= '

'; 163 | $form .= '

'; 164 | $form .= '
'; 165 | return $form; 166 | }); 167 | ``` 168 | Detailed Example in Laravel way 169 | view files 170 | ```html 171 | //register.blade.php 172 | captcha 173 |
174 | 177 | @error('captcha') 178 |
{{ $message }}
@enderror 179 | ``` 180 | controller files 181 | ```php 182 | Validator::make($input, [ 183 | 'name' => ['required', 'string', 'max:255'], 184 | 'email' => [ 185 | 'required', 186 | 'string', 187 | 'email', 188 | 'max:255', 189 | Rule::unique(User::class), 190 | ], 191 | 'password' => $this->passwordRules(), 192 | 'captcha' => 'required|captcha' 193 | ])->validate(); 194 | ``` 195 | ### Stateless Mode: 196 | You get key and img from this url 197 | `http://localhost/captcha/api/math` 198 | and verify the captcha using this method: 199 | ```php 200 | //key is the one that you got from json response 201 | // fix validator 202 | // $rules = ['captcha' => 'required|captcha_api:'. request('key')]; 203 | $rules = ['captcha' => 'required|captcha_api:'. request('key') . ',math']; 204 | $validator = validator()->make(request()->all(), $rules); 205 | if ($validator->fails()) { 206 | return response()->json([ 207 | 'message' => 'invalid captcha', 208 | ]); 209 | 210 | } else { 211 | //do the job 212 | } 213 | ``` 214 | 215 | # Return Image 216 | ```php 217 | captcha(); 218 | ``` 219 | or 220 | ```php 221 | Captcha::create(); 222 | ``` 223 | 224 | 225 | # Return URL 226 | ```php 227 | captcha_src(); 228 | ``` 229 | or 230 | ``` 231 | Captcha::src('default'); 232 | ``` 233 | 234 | # Return HTML 235 | ```php 236 | captcha_img(); 237 | ``` 238 | or 239 | ```php 240 | Captcha::img(); 241 | ``` 242 | 243 | # To use different configurations 244 | ```php 245 | captcha_img('flat'); 246 | 247 | Captcha::img('inverse'); 248 | ``` 249 | etc. 250 | 251 | Based on [Intervention Image](https://github.com/Intervention/image) 252 | 253 | ^_^ 254 | 255 | ## Links 256 | * [Intervention Image](https://github.com/Intervention/image) 257 | * [L5 Captcha on Github](https://github.com/mewebstudio/captcha) 258 | * [L5 Captcha on Packagist](https://packagist.org/packages/mews/captcha) 259 | * [For L4 on Github](https://github.com/mewebstudio/captcha/tree/master-l4) 260 | * [License](http://www.opensource.org/licenses/mit-license.php) 261 | * [Laravel website](http://laravel.com) 262 | * [Laravel Turkiye website](http://www.laravel.gen.tr) 263 | * [mewebstudio](https://github.com/mewebstudio/captcha) 264 | -------------------------------------------------------------------------------- /assets/fonts/license/LICENSE-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /src/Captcha.php: -------------------------------------------------------------------------------- 1 | canvas 72 | */ 73 | protected $canvas; 74 | 75 | /** 76 | * @var Image 77 | */ 78 | protected $image; 79 | 80 | /** 81 | * @var array 82 | */ 83 | protected $backgrounds = []; 84 | 85 | /** 86 | * @var array 87 | */ 88 | protected $fonts = []; 89 | 90 | /** 91 | * @var array 92 | */ 93 | protected $fontColors = []; 94 | 95 | /** 96 | * @var int 97 | */ 98 | protected $length = 5; 99 | 100 | /** 101 | * @var int 102 | */ 103 | protected $width = 120; 104 | 105 | /** 106 | * @var int 107 | */ 108 | protected $height = 36; 109 | 110 | /** 111 | * @var int 112 | */ 113 | protected $angle = 15; 114 | 115 | /** 116 | * @var int 117 | */ 118 | protected $lines = 3; 119 | 120 | /** 121 | * @var int 122 | */ 123 | protected $lineWidth = 2; 124 | 125 | /** 126 | * @var string 127 | */ 128 | protected $lineColor = 'ff00ff'; 129 | 130 | /** 131 | * @var string 132 | */ 133 | protected $characters; 134 | 135 | /** 136 | * @var array 137 | */ 138 | protected $text; 139 | 140 | /** 141 | * @var int 142 | */ 143 | protected $contrast = 0; 144 | 145 | /** 146 | * @var int 147 | */ 148 | protected $quality = 90; 149 | 150 | /** 151 | * @var int 152 | */ 153 | protected $sharpen = 0; 154 | 155 | /** 156 | * @var int 157 | */ 158 | protected $blur = 0; 159 | 160 | /** 161 | * @var string 162 | */ 163 | protected $bgsDirectory; 164 | 165 | /** 166 | * @var bool 167 | */ 168 | protected $bgImage = true; 169 | 170 | /** 171 | * @var string 172 | */ 173 | protected $bgColor = '#ffffff'; 174 | 175 | /** 176 | * @var bool 177 | */ 178 | protected $invert = false; 179 | 180 | /** 181 | * @var bool 182 | */ 183 | protected $sensitive = false; 184 | 185 | /** 186 | * @var bool 187 | */ 188 | protected $math = false; 189 | 190 | /** 191 | * @var int 192 | */ 193 | protected $textLeftPadding = 4; 194 | 195 | /** 196 | * @var string 197 | */ 198 | protected $fontsDirectory; 199 | 200 | /** 201 | * @var int 202 | */ 203 | protected $expire = 60; 204 | 205 | /** 206 | * @var bool 207 | */ 208 | protected $encrypt = true; 209 | 210 | /** 211 | * @var int 212 | */ 213 | protected $marginTop = 0; 214 | 215 | /** 216 | * Constructor 217 | * 218 | * @param Filesystem $files 219 | * @param Repository $config 220 | * @param ImageManager $imageManager 221 | * @param Session $session 222 | * @param Hasher $hasher 223 | * @param Str $str 224 | * @throws Exception 225 | * @internal param Validator $validator 226 | */ 227 | public function __construct( 228 | Filesystem $files, 229 | Repository $config, 230 | ImageManager $imageManager, 231 | Session $session, 232 | Hasher $hasher, 233 | Str $str 234 | ) { 235 | $this->files = $files; 236 | $this->config = $config; 237 | $this->imageManager = $imageManager; 238 | $this->session = $session; 239 | $this->hasher = $hasher; 240 | $this->str = $str; 241 | $this->characters = config('captcha.characters', ['1', '2', '3', '4', '6', '7', '8', '9']); 242 | $this->fontsDirectory = config('captcha.fontsDirectory', dirname(__DIR__) . '/assets/fonts'); 243 | $this->bgsDirectory = config('captcha.bgsDirectory', dirname(__DIR__) . '/assets/backgrounds'); 244 | } 245 | 246 | /** 247 | * @param string $config 248 | * @return void 249 | */ 250 | protected function configure($config) 251 | { 252 | if ($this->config->has('captcha.' . $config)) { 253 | foreach ($this->config->get('captcha.' . $config) as $key => $val) { 254 | $this->{$key} = $val; 255 | } 256 | } 257 | } 258 | 259 | /** 260 | * Create captcha image 261 | * 262 | * @param string $config 263 | * @param bool $api 264 | * @return array|mixed 265 | * @throws Exception 266 | */ 267 | public function create(string $config = 'default', bool $api = false) 268 | { 269 | $this->backgrounds = $this->files->files($this->bgsDirectory); 270 | $this->fonts = $this->files->files($this->fontsDirectory); 271 | 272 | if (version_compare(app()->version(), '5.5.0', '>=')) { 273 | $this->fonts = array_map(function ($file) { 274 | /* @var File $file */ 275 | return $file->getPathName(); 276 | }, $this->fonts); 277 | } 278 | 279 | $this->fonts = array_values($this->fonts); //reset fonts array index 280 | 281 | $this->configure($config); 282 | 283 | $generator = $this->generate(); 284 | $this->text = $generator['value']; 285 | 286 | $this->canvas = $this->imageManager->create($this->width , $this->height)->fill($this->bgColor); 287 | 288 | if ($this->bgImage) { 289 | $this->image = $this->imageManager->read($this->background())->resize( 290 | $this->width, 291 | $this->height 292 | ); 293 | $this->canvas->place($this->image); 294 | } else { 295 | $this->image = $this->canvas; 296 | } 297 | 298 | if ($this->contrast != 0) { 299 | $this->image->contrast($this->contrast); 300 | } 301 | 302 | $this->text(); 303 | 304 | $this->lines(); 305 | 306 | if ($this->sharpen) { 307 | $this->image->sharpen($this->sharpen); 308 | } 309 | if ($this->invert) { 310 | $this->image->invert(); 311 | } 312 | if ($this->blur) { 313 | $this->image->blur($this->blur); 314 | } 315 | 316 | Cache::put($this->get_cache_key($generator['key']), $generator['value'], $this->expire); 317 | 318 | return $api ? [ 319 | 'sensitive' => $generator['sensitive'], 320 | 'key' => $generator['key'], 321 | 'img' => $this->image->toJpg()->toDataUri() 322 | ] : new Response($this->image->toJpg(), 200, [ 323 | 'Content-Type' => 'image/jpeg', 324 | 'Content-Disposition' => 'inline; filename="image.jpg"', 325 | ]); 326 | } 327 | 328 | /** 329 | * Image backgrounds 330 | * 331 | * @return string 332 | */ 333 | protected function background(): string 334 | { 335 | return $this->backgrounds[rand(0, count($this->backgrounds) - 1)]; 336 | } 337 | 338 | /** 339 | * Generate captcha text 340 | * 341 | * @return array 342 | * @throws Exception 343 | */ 344 | protected function generate(): array 345 | { 346 | $characters = is_string($this->characters) ? str_split($this->characters) : $this->characters; 347 | 348 | $bag = []; 349 | 350 | if ($this->math) { 351 | $x = random_int(10, 30); 352 | $y = random_int(1, 9); 353 | $bag = "$x + $y = "; 354 | $key = $x + $y; 355 | $key .= ''; 356 | } else { 357 | for ($i = 0; $i < $this->length; $i++) { 358 | $char = $characters[rand(0, count($characters) - 1)]; 359 | $bag[] = $this->sensitive ? $char : $this->str->lower($char); 360 | } 361 | $key = implode('', $bag); 362 | } 363 | 364 | $hash = $this->hasher->make($key); 365 | if($this->encrypt) $hash = Crypt::encrypt($hash); 366 | 367 | $this->session->put('captcha', [ 368 | 'sensitive' => $this->sensitive, 369 | 'key' => $hash, 370 | 'encrypt' => $this->encrypt 371 | ]); 372 | 373 | return [ 374 | 'value' => $bag, 375 | 'sensitive' => $this->sensitive, 376 | 'key' => $hash 377 | ]; 378 | } 379 | 380 | /** 381 | * Writing captcha text 382 | * 383 | * @return void 384 | */ 385 | protected function text(): void 386 | { 387 | $marginTop = $this->image->height() / $this->length; 388 | if ($this->marginTop !== 0) { 389 | $marginTop = $this->marginTop; 390 | } 391 | 392 | $text = $this->text; 393 | if (is_string($text)) { 394 | $text = str_split($text); 395 | } 396 | 397 | foreach ($text as $key => $char) { 398 | $marginLeft = $this->textLeftPadding + ($key * ($this->image->width() - $this->textLeftPadding) / $this->length); 399 | 400 | $this->image->text($char, $marginLeft, $marginTop, function ($font) { 401 | /* @var Font $font */ 402 | $font->file($this->font()); 403 | $font->size($this->fontSize()); 404 | $font->color($this->fontColor()); 405 | $font->align('left'); 406 | $font->valign('top'); 407 | $font->angle($this->angle()); 408 | }); 409 | } 410 | } 411 | 412 | /** 413 | * Image fonts 414 | * 415 | * @return string 416 | */ 417 | protected function font(): string 418 | { 419 | return $this->fonts[rand(0, count($this->fonts) - 1)]; 420 | } 421 | 422 | /** 423 | * Random font size 424 | * 425 | * @return int 426 | */ 427 | protected function fontSize(): int 428 | { 429 | return rand($this->image->height() - 10, $this->image->height()); 430 | } 431 | 432 | /** 433 | * Random font color 434 | * 435 | * @return string 436 | */ 437 | protected function fontColor(): string 438 | { 439 | if (!empty($this->fontColors)) { 440 | $color = $this->fontColors[rand(0, count($this->fontColors) - 1)]; 441 | } else { 442 | $color = '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); 443 | } 444 | 445 | return $color; 446 | } 447 | 448 | /** 449 | * Angle 450 | * 451 | * @return int 452 | */ 453 | protected function angle(): int 454 | { 455 | return rand((-1 * $this->angle), $this->angle); 456 | } 457 | 458 | /** 459 | * Random image lines 460 | * 461 | * @return Image|ImageManager 462 | */ 463 | protected function lines() 464 | { 465 | for ($i = 0; $i <= $this->lines; $i++) { 466 | $this->image->drawLine(function (LineFactory $line) use ($i) { 467 | $line->from(rand(0, $this->image->width()) + $i * rand(0, $this->image->height()) , rand(0, $this->image->height())); 468 | $line->to( rand(0, $this->image->width()), rand(0, $this->image->height())); 469 | $line->color($this->lineColor); // color of line 470 | $line->width($this->lineWidth); // line width in pixels 471 | }); 472 | } 473 | 474 | return $this->image; 475 | } 476 | 477 | /** 478 | * Captcha check 479 | * 480 | * @param string $value 481 | * @return bool 482 | */ 483 | public function check(string $value): bool 484 | { 485 | if (!$this->session->has('captcha')) { 486 | return false; 487 | } 488 | 489 | $key = $this->session->get('captcha.key'); 490 | $sensitive = $this->session->get('captcha.sensitive'); 491 | $encrypt = $this->session->get('captcha.encrypt'); 492 | 493 | if (!Cache::pull($this->get_cache_key($key))) { 494 | $this->session->remove('captcha'); 495 | return false; 496 | } 497 | 498 | if (!$sensitive) { 499 | $value = $this->str->lower($value); 500 | } 501 | 502 | if($encrypt) $key = Crypt::decrypt($key); 503 | $check = $this->hasher->check($value, $key); 504 | // if verify pass,remove session 505 | if ($check) { 506 | $this->session->remove('captcha'); 507 | } 508 | 509 | return $check; 510 | } 511 | 512 | /** 513 | * Returns the md5 short version of the key for cache 514 | * 515 | * @param string $key 516 | * @return string 517 | */ 518 | protected function get_cache_key($key) { 519 | return 'captcha_' . md5($key); 520 | } 521 | 522 | /** 523 | * Captcha check 524 | * 525 | * @param string $value 526 | * @param string $key 527 | * @param string $config 528 | * @return bool 529 | */ 530 | public function check_api($value, $key, $config = 'default'): bool 531 | { 532 | if (!Cache::pull($this->get_cache_key($key))) { 533 | return false; 534 | } 535 | 536 | $this->configure($config); 537 | 538 | if(!$this->sensitive) $value = $this->str->lower($value); 539 | if($this->encrypt) $key = Crypt::decrypt($key); 540 | return $this->hasher->check($value, $key); 541 | } 542 | 543 | /** 544 | * Generate captcha image source 545 | * 546 | * @param string $config 547 | * @return string 548 | */ 549 | public function src(string $config = 'default'): string 550 | { 551 | return url('captcha/' . $config) . '?' . $this->str->random(8); 552 | } 553 | 554 | /** 555 | * Generate captcha image html tag 556 | * 557 | * @param string $config 558 | * @param array $attrs 559 | * $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value 560 | * @return string 561 | */ 562 | public function img(string $config = 'default', array $attrs = []): string 563 | { 564 | $attrs_str = ''; 565 | foreach ($attrs as $attr => $value) { 566 | if ($attr == 'src') { 567 | //Neglect src attribute 568 | continue; 569 | } 570 | 571 | $attrs_str .= $attr . '="' . $value . '" '; 572 | } 573 | return new HtmlString(''); 574 | } 575 | } 576 | --------------------------------------------------------------------------------