Skip to content

Commit

Permalink
增加API调用
Browse files Browse the repository at this point in the history
  • Loading branch information
@小小只^v^ committed Nov 24, 2020
1 parent 1eef371 commit 59285f4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function check(string $code): bool
* @param bool $api
* @return Response
*/
public function create(string $config = null, bool $api = false): Response
public function create(string $config = null, bool $api = false)
{
$this->configure($config);

Expand Down Expand Up @@ -232,7 +232,16 @@ public function create(string $config = null, bool $api = false): Response
imagepng($this->im);
$content = ob_get_clean();
imagedestroy($this->im);

/**
* API 调用
* @小小只^v^
*/
if ($api) {
return [
'code' => implode('', $text),
'image' => 'data:image/png;base64,' . chunk_split(base64_encode($content))
];
}
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
}

Expand Down Expand Up @@ -336,5 +345,4 @@ protected function background(): void
@imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height);
@imagedestroy($bgImage);
}

}
14 changes: 13 additions & 1 deletion src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ function captcha($config = null): Response
return Captcha::create($config);
}

/**
* API 调用
* @小小只^v^
* @param string $config
* @return array
*
*/
function captcha_api($config = null): array
{
return Captcha::create($config, true);
}

/**
* @param $config
* @return string
Expand All @@ -38,7 +50,7 @@ function captcha_src($config = null): string
function captcha_img($id = '', $domid = ''): string
{
$src = captcha_src($id);

$domid = empty($domid) ? $domid : "id='" . $domid . "'";

return "<img src='{$src}' alt='captcha' " . $domid . " onclick='this.src=\"{$src}?\"+Math.random();' />";
Expand Down

0 comments on commit 59285f4

Please sign in to comment.