From aec42f9d7d6facf01316f2d7142efd93d7ce17bc Mon Sep 17 00:00:00 2001 From: HWVS <35441792+hwvs@users.noreply.github.com> Date: Wed, 22 Jan 2020 12:27:55 -0800 Subject: [PATCH] Update CaptchaBuilder.php added support for IMG_FILTER_SCATTER added in PHP 7.4 --- src/Gregwar/Captcha/CaptchaBuilder.php | 34 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Gregwar/Captcha/CaptchaBuilder.php b/src/Gregwar/Captcha/CaptchaBuilder.php index 16c5658..de29ced 100644 --- a/src/Gregwar/Captcha/CaptchaBuilder.php +++ b/src/Gregwar/Captcha/CaptchaBuilder.php @@ -94,6 +94,11 @@ class CaptchaBuilder implements CaptchaBuilderInterface */ protected $ignoreAllEffects = false; + /** + * @var bool + */ + protected $scatterEffect = true; + /** * Allowed image types for the background images * @@ -157,6 +162,16 @@ public function setDistortion($distortion) return $this; } + /** + * Enables/disable scatter effect - Only applies to PHP 7.4+ + */ + public function setscatterEffect($scatterEffect) + { + $this->scatterEffect = (bool) $scatterEffect; + + return $this; + } + public function setMaxBehindLines($maxBehindLines) { $this->maxBehindLines = $maxBehindLines; @@ -279,7 +294,7 @@ protected function drawLine($image, $width, $height, $tcol = null) /** * Apply some post effects */ - protected function postEffect($image) + protected function postEffect($image, $bg) { if (!function_exists('imagefilter')) { return; @@ -289,13 +304,22 @@ protected function postEffect($image) return; } + // Scatter/Noise - Added in PHP 7.4 + $scattered = false; + if (version_compare(PHP_VERSION, '7.4.0') >= 0) { + if($this->scatterEffect && $this->rand(0, 3) != 0 && $bg != null) { + $scattered = true; + imagefilter($image, IMG_FILTER_SCATTER, 0, 2, array($bg)); + } + } + // Negate ? if ($this->rand(0, 1) == 0) { imagefilter($image, IMG_FILTER_NEGATE); } // Edge ? - if ($this->rand(0, 10) == 0) { + if (!$scattered && $this->rand(0, 10) == 0) { imagefilter($image, IMG_FILTER_EDGEDETECT); } @@ -303,9 +327,11 @@ protected function postEffect($image) imagefilter($image, IMG_FILTER_CONTRAST, $this->rand(-50, 10)); // Colorize - if ($this->rand(0, 5) == 0) { + if (!$scattered && $this->rand(0, 5) == 0) { imagefilter($image, IMG_FILTER_COLORIZE, $this->rand(-80, 50), $this->rand(-80, 50), $this->rand(-80, 50)); } + + } /** @@ -460,7 +486,7 @@ public function build($width = 150, $height = 40, $font = null, $fingerprint = n // Post effects if (!$this->ignoreAllEffects) { - $this->postEffect($image); + $this->postEffect($image, $bg); } $this->contents = $image;