Skip to content

Commit

Permalink
Merge pull request #161 from Thorn-workshop/master
Browse files Browse the repository at this point in the history
Multibyte string support by pre-splitted characters string
  • Loading branch information
mewebstudio authored Apr 19, 2019
2 parents 4432003 + 1e70e1b commit 401b54c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion config/captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

return [

'characters' => '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ',
'characters' => ['2','3','4','6','7','8','9','a','b','c','d','e','f','g','h','j','m','n','p','q','r','t','u','x','y','z','A','B','C','D','E','F','G','H','J','M','N','P','Q','R','T','U','X','Y','Z'],

'default' => [
'length' => 9,
Expand Down
49 changes: 25 additions & 24 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Captcha
protected $characters;

/**
* @var string
* @var array
*/
protected $text;

Expand Down Expand Up @@ -198,7 +198,7 @@ public function __construct(
$this->session = $session;
$this->hasher = $hasher;
$this->str = $str;
$this->characters = config('captcha.characters','2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ');
$this->characters = config('captcha.characters',['1','2','3','4','6','7','8','9']);
}

/**
Expand Down Expand Up @@ -306,9 +306,9 @@ protected function background()
*/
protected function generate()
{
$characters = str_split($this->characters);
$characters = is_string($this->characters) ? str_split($this->characters) : $this->characters;

$bag = '';
$bag = [];
$key = '';

if ($this->math) {
Expand All @@ -319,9 +319,10 @@ protected function generate()
$key .= '';
} else {
for ($i = 0; $i < $this->length; $i++) {
$bag .= $characters[rand(0, count($characters) - 1)];
$char = $characters[rand(0, count($characters) - 1)];
$bag[] = $this->sensitive ? $char : $this->str->lower($char);
}
$key = $this->sensitive ? $bag : $this->str->lower($bag);
$key = implode('', $bag);
}

$hash = $this->hasher->make($key);
Expand All @@ -342,24 +343,24 @@ protected function generate()
*/
protected function text()
{
$marginTop = $this->image->height() / $this->length;

$i = 0;
foreach(str_split($this->text) as $char)
{
$marginLeft = $this->textLeftPadding + ($i * ($this->image->width() - $this->textLeftPadding) / $this->length);

$this->image->text($char, $marginLeft, $marginTop, function($font) {
$font->file($this->font());
$font->size($this->fontSize());
$font->color($this->fontColor());
$font->align('left');
$font->valign('top');
$font->angle($this->angle());
});

$i++;
}
$marginTop = $this->image->height() / $this->length;

$i = 0;
foreach($this->text as $char)
{
$marginLeft = $this->textLeftPadding + ($i * ($this->image->width() - $this->textLeftPadding) / $this->length);

$this->image->text($char, $marginLeft, $marginTop, function($font) {
$font->file($this->font());
$font->size($this->fontSize());
$font->color($this->fontColor());
$font->align('left');
$font->valign('top');
$font->angle($this->angle());
});

$i++;
}
}

/**
Expand Down

0 comments on commit 401b54c

Please sign in to comment.