Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{
"name": "gregwar/captcha",
"name": "malutanpetronel/captcha",
"type": "library",
"description": "Captcha generator",
"keywords": ["captcha", "spam", "bot"],
"keywords": ["captcha", "spam", "bot", "split", "multiple"],
"homepage": "https://github.com/Gregwar/Captcha",
"license": "MIT",
"authors": [
{
"name": "Grégoire Passault",
"email": "g.passault@gmail.com",
"homepage": "http://www.gregwar.com/"
"homepage": "http://www.gregwar.com/",
"role": "Developer"
},
{
"name": "Jeremy Livingston",
"email": "jeremy.j.livingston@gmail.com"
"email": "jeremy.j.livingston@gmail.com",
"role": "Developer"
},
{
"name": "Petronel Malutan",
"email": "malutanpetronel@gmail.com",
"homepage": "https://www.webnou.ro/",
"role": "Developer"
}
],
"require": {
Expand Down
134 changes: 75 additions & 59 deletions src/Gregwar/Captcha/CaptchaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class CaptchaBuilder implements CaptchaBuilderInterface
*/
protected $contents = null;

/**
* @var array
*/
protected $contentLetters = [];

/**
* @var string
*/
Expand Down Expand Up @@ -406,85 +411,95 @@ public function buildAgainstOCR($width = 150, $height = 40, $font = null, $finge
*/
public function build($width = 150, $height = 40, $font = null, $fingerprint = null)
{
if (null !== $fingerprint) {
$this->fingerprint = $fingerprint;
$this->useFingerprint = true;
} else {
$this->fingerprint = array();
$this->useFingerprint = false;
}
$letters = strlen($this->phrase);
$width = $width/$letters;

if ($font === null) {
$font = __DIR__ . '/Font/captcha'.$this->rand(0, 5).'.ttf';
}

if (empty($this->backgroundImages)) {
// if background images list is not set, use a color fill as a background
$image = imagecreatetruecolor($width, $height);
if ($this->backgroundColor == null) {
$bg = imagecolorallocate($image, $this->rand(200, 255), $this->rand(200, 255), $this->rand(200, 255));
for ($i = 0; $i < $letters; $i++) {
if (null !== $fingerprint) {
$this->fingerprint = $fingerprint;
$this->useFingerprint = true;
} else {
$color = $this->backgroundColor;
$bg = imagecolorallocate($image, $color[0], $color[1], $color[2]);
$this->fingerprint = array();
$this->useFingerprint = false;
}
imagefill($image, 0, 0, $bg);
} else {
// use a random background image
$randomBackgroundImage = $this->backgroundImages[rand(0, count($this->backgroundImages)-1)];

$imageType = $this->validateBackgroundImage($randomBackgroundImage);
if ($font === null) {
$font = __DIR__ . '/Font/captcha'.$this->rand(0, 5).'.ttf';
}

$image = $this->createBackgroundImageFromType($randomBackgroundImage, $imageType);
}
if (empty($this->backgroundImages)) {
// if background images list is not set, use a color fill as a background
$image = imagecreatetruecolor($width, $height);
if ($this->backgroundColor == null) {
$bg = imagecolorallocate($image, $this->rand(200, 255), $this->rand(200, 255), $this->rand(200, 255));
} else {
$color = $this->backgroundColor;
$bg = imagecolorallocate($image, $color[0], $color[1], $color[2]);
}
imagefill($image, 0, 0, $bg);
} else {
// use a random background image
$randomBackgroundImage = $this->backgroundImages[rand(0, count($this->backgroundImages)-1)];

// Apply effects
if (!$this->ignoreAllEffects) {
$square = $width * $height;
$effects = $this->rand($square/3000, $square/2000);
$imageType = $this->validateBackgroundImage($randomBackgroundImage);

// set the maximum number of lines to draw in front of the text
if ($this->maxBehindLines != null && $this->maxBehindLines > 0) {
$effects = min($this->maxBehindLines, $effects);
$image = $this->createBackgroundImageFromType($randomBackgroundImage, $imageType);
}

if ($this->maxBehindLines !== 0) {
for ($e = 0; $e < $effects; $e++) {
$this->drawLine($image, $width, $height);
// Apply effects
if (!$this->ignoreAllEffects) {
$square = $width * $height;
$effects = $this->rand($square/3000, $square/2000);

// set the maximum number of lines to draw in front of the text
if ($this->maxBehindLines != null && $this->maxBehindLines > 0) {
$effects = min($this->maxBehindLines, $effects);
}

if ($this->maxBehindLines !== 0) {
for ($e = 0; $e < $effects; $e++) {
$this->drawLine($image, $width, $height);
}
}
}
}

// Write CAPTCHA text
$color = $this->writePhrase($image, $this->phrase, $font, $width, $height);
// Write CAPTCHA text
$color = $this->writePhrase($image, substr($this->phrase, $i,1), $font, $width, $height);

// Apply effects
if (!$this->ignoreAllEffects) {
$square = $width * $height;
$effects = $this->rand($square/3000, $square/2000);
// Apply effects
if (!$this->ignoreAllEffects) {
$square = $width * $height;
$effects = $this->rand($square/3000, $square/2000);

// set the maximum number of lines to draw in front of the text
if ($this->maxFrontLines != null && $this->maxFrontLines > 0) {
$effects = min($this->maxFrontLines, $effects);
}
// set the maximum number of lines to draw in front of the text
if ($this->maxFrontLines != null && $this->maxFrontLines > 0) {
$effects = min($this->maxFrontLines, $effects);
}

if ($this->maxFrontLines !== 0) {
for ($e = 0; $e < $effects; $e++) {
$this->drawLine($image, $width, $height, $color);
if ($this->maxFrontLines !== 0) {
for ($e = 0; $e < $effects; $e++) {
$this->drawLine($image, $width, $height, $color);
}
}
}
}

// Distort the image
if ($this->distortion && !$this->ignoreAllEffects) {
$image = $this->distort($image, $width, $height, $bg);
}
// Distort the image
if ($this->distortion && !$this->ignoreAllEffects) {
$image = $this->distort($image, $width, $height, $bg);
}

// Post effects
if (!$this->ignoreAllEffects) {
$this->postEffect($image);
}

$this->contentLetters[] = $image;

// Post effects
if (!$this->ignoreAllEffects) {
$this->postEffect($image);
}

$this->contents = $image;
//dd($this->contentLetters);
$this->contents = $this->contentLetters[0];
// dd($this);

return $this;
}
Expand Down Expand Up @@ -552,7 +567,8 @@ public function save($filename, $quality = 90)
*/
public function getGd()
{
return $this->contents;
// return $this->contents;
return $this->contentLetters;
}

/**
Expand Down