Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IMG_FILTER_SCATTER added in PHP 7.4 #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/Gregwar/Captcha/CaptchaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class CaptchaBuilder implements CaptchaBuilderInterface
*/
protected $ignoreAllEffects = false;

/**
* @var bool
*/
protected $scatterEffect = true;

/**
* Allowed image types for the background images
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -289,23 +304,34 @@ 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);
}

// Contrast
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));
}


}

/**
Expand Down Expand Up @@ -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;
Expand Down