diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php deleted file mode 100644 index 0bd3720..0000000 --- a/.php-cs-fixer.php +++ /dev/null @@ -1,18 +0,0 @@ -exclude([ - 'demo', - 'fonts', - ]) - ->in(__DIR__) -; - -$config = new PhpCsFixer\Config(); - -return $config->setRules([ - '@PSR12' => true, - 'array_syntax' => ['syntax' => 'short'], - ]) - ->setFinder($finder) -; diff --git a/composer.json b/composer.json index 5c59571..0956fe0 100644 --- a/composer.json +++ b/composer.json @@ -7,8 +7,7 @@ "keywords": ["captcha", "anti-spam", "spam-protection", "spam", "obfuscation", "security", "anti-bot", "no-bot", "bot-protection"], "homepage": "https://codeberg.org/S1SYPHOS/php-simple-captcha", "scripts": { - "cs-dry": "php-cs-fixer fix --dry-run --diff", - "cs-fix": "php-cs-fixer fix", + "lint": "pint --verbose", "test": "phpunit" }, "authors": [ @@ -39,9 +38,10 @@ "thiagoalessio/tesseract_ocr": "Provides better results when building against OCR. Requires 'tesseract-ocr' to be installed." }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.4", + "laravel/pint": "^1.1", "phpunit/phpunit": "^8.5|^9.0", - "mikey179/vfsstream": "^1.6" + "mikey179/vfsstream": "^1.6", + "vimeo/psalm": "^4.26" }, "autoload": { "psr-4": { diff --git a/demo/demo.php b/demo/demo.php index 2ebc91d..8cc6f6b 100644 --- a/demo/demo.php +++ b/demo/demo.php @@ -14,4 +14,5 @@ F::remove($file); } -Builder::create()->build()->save('tmp/out.jpg');; +Builder::create()->build()->save('tmp/out.jpg'); +; diff --git a/demo/form.php b/demo/form.php index 39b6e5f..589b0bb 100644 --- a/demo/form.php +++ b/demo/form.php @@ -28,15 +28,13 @@ if (isset($_SESSION['phrase']) && $captcha->compare($_SESSION['phrase'], $_POST['phrase'])) { echo 'Captcha is valid!'; - } - - else { + } else { echo 'Captcha is invalid!'; } # Delete phrase stored in session unset($_SESSION['phrase']); } - ?> +?> diff --git a/demo/ocr.php b/demo/ocr.php index 3135414..2705525 100644 --- a/demo/ocr.php +++ b/demo/ocr.php @@ -23,7 +23,7 @@ for ($i = 0; $i < $total; $i++) { echo sprintf('Captcha %s/%s ..', $i + 1, $total); - $captcha = new Builder; + $captcha = new Builder(); $captcha->distortion = false; $captcha->build(); @@ -34,9 +34,7 @@ $captcha->save("tmp/passed-$passed.jpg"); echo ' passed!
'; - } - - else { + } else { echo ' failed!
'; } diff --git a/phpunit.xml b/phpunit.xml index 4926dfd..03bd70f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,16 +1,27 @@ - - - - ./ - - - ./vendor - - - - - ./tests/ - - + + + + ./src + + + ./vendor + + + + + ./tests/ + + + + + diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..5f414fb --- /dev/null +++ b/pint.json @@ -0,0 +1,7 @@ +{ + "preset": "psr12", + "rules": {}, + "exclude": [ + "src/Helpers" + ] +} diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..c6b2562 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/src/Builder.php b/src/Builder.php index 210a0ad..e8f9281 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -10,6 +10,8 @@ use SimpleCaptcha\Helpers\Str; use SimpleCaptcha\Helpers\Mime; +use \thiagoalessio\TesseractOCR\TesseractOCR; + use GdImage; use resource; use Exception; @@ -299,25 +301,25 @@ private function drawLine(?int $color = null): void */ private function applyNoise(): void { - for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) { + for ($i = 0; $i < Str::length($this->phrase) * $this->noiseFactor; $i++) { # Determine random letter .. - $character = static::randomCharacter(); + $character = static::randomCharacter(); $font = $this->randomFont(); # .. of random size & color, .. - $fontSize = mt_rand(5, 10); + $fontSize = mt_rand(5, 10); $textColor = imagecolorallocate($this->image, mt_rand(0, 128), mt_rand(0, 128), mt_rand(0, 128)); # .. random position .. $x = mt_rand(0, $this->width); - $y = mt_rand(0, $this->height); + $y = mt_rand(0, $this->height); # .. random angle .. - $angle = mt_rand(-45, 45); + $angle = mt_rand(-45, 45); # .. and apply it - imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character); - } + imagettftext($this->image, $fontSize, $angle, $x, $y, $textColor, $font, $character); + } } @@ -545,7 +547,6 @@ private function distort(): void $Vn2 = $Vn + 4 * sin($Vn / 30); $nX = $X + ($Vx * $Vn2 / $Vn); $nY = $Y + ($Vy * $Vn2 / $Vn); - } else { $nX = $X; $nY = $Y; @@ -562,7 +563,6 @@ private function distort(): void $this->pixel2int(floor($nX), ceil($nY)), $this->pixel2int(ceil($nX), ceil($nY)) ); - } else { $p = $this->pixel2int($this->round($nX), $this->round($nY)); } @@ -695,12 +695,12 @@ public function build(int $width = 150, int $height = 40): self * See https://priteshgupta.com/2011/09/advanced-image-functions-using-php * See https://github.com/raoulduke/phpocrad * - * @param string $file Output file + * @param string $output Output file * @param int $amount * @param int $threshold * @return void */ - private function img2ocr(?string $output = null, int $amount = 80, int $threshold = 3): void + private function img2ocr(string $output, int $amount = 80, int $threshold = 3): void { $image = $this->image; @@ -752,9 +752,7 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol } } } - } - - else { + } else { for ($x = 0; $x < $this->width; $x++) { # each row for ($y = 0; $y < $this->height; $y++) { # each pixel $rgbOrig = imagecolorat($image, $x, $y); @@ -770,14 +768,23 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol $bBlur = ($rgbBlur & 0xFF); $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig; - if ($rNew > 255) { $rNew = 255; } - elseif ($rNew < 0) { $rNew = 0; } + if ($rNew > 255) { + $rNew = 255; + } elseif ($rNew < 0) { + $rNew = 0; + } $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig; - if ($gNew > 255) { $gNew = 255; } - elseif ($gNew < 0) { $gNew = 0; } + if ($gNew > 255) { + $gNew = 255; + } elseif ($gNew < 0) { + $gNew = 0; + } $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig; - if ($bNew > 255) { $bNew = 255; } - elseif ($bNew < 0) { $bNew = 0; } + if ($bNew > 255) { + $bNew = 255; + } elseif ($bNew < 0) { + $bNew = 0; + } $rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew; imagesetpixel($image, $x, $y, $rgbNew); @@ -802,15 +809,11 @@ private function img2ocr(?string $output = null, int $amount = 80, int $threshol $green = $this->round(0.59 * $colors['green']); $blue = $this->round(0.11 * $colors['blue']); - # Create single-byte string from them + # Create single-byte string from them $pgm .= chr($red + $green + $blue); } } - if (empty($output)) { - $output = sprintf('%s/%s.pgm', F::dirname($file), F::name($file)); - } - F::write($output, $pgm); } @@ -858,9 +861,9 @@ public function isOCRReadable(string $tmpDir = '.tmp'): bool # Iterate over available modes .. foreach ($modes as $mode) { # .. using (suggested) external library (if available), otherwise .. - if ($mode == 'tesseract' && class_exists('\thiagoalessio\TesseractOCR\TesseractOCR')) { + if ($mode == 'tesseract' && class_exists(TesseractOCR::class)) { # Execute `tesseract-ocr-for-php` & store its output - $tesseract = new \thiagoalessio\TesseractOCR\TesseractOCR($pgmFile); + $tesseract = new TesseractOCR($pgmFile); $outputs[] = $tesseract->allowlist(range(0, 9), range('a', 'z'), range('A', 'Z'))->dpi(2200)->run(); } @@ -956,13 +959,9 @@ protected function gd2img(int $quality = 90, ?string $filename = null, string $t if ($type == 'gif') { imagegif($this->image, $filename); - } - - elseif ($type == 'jpg') { + } elseif ($type == 'jpg') { imagejpeg($this->image, $filename, $quality); - } - - elseif ($type == 'png') { + } elseif ($type == 'png') { # Normalize quality if ($quality > 9) { $quality = -1; @@ -1041,7 +1040,7 @@ public function inline(int $quality = 90, string $type = 'jpg'): string /** * Rounds float to integer - * + * * @param float $number * @return int */ @@ -1053,7 +1052,7 @@ private function round(float $number): int /** * Creates random float between two digits - * + * * @param float|int $min * @param float|int $max * @return float diff --git a/src/BuilderAbstract.php b/src/BuilderAbstract.php index d055d21..44752ef 100644 --- a/src/BuilderAbstract.php +++ b/src/BuilderAbstract.php @@ -1,10 +1,11 @@