diff --git a/src/Captcha.php b/src/Captcha.php index 0789087..2f391c6 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -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); @@ -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'); } @@ -336,5 +345,4 @@ protected function background(): void @imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height); @imagedestroy($bgImage); } - } diff --git a/src/helper.php b/src/helper.php index 7196606..4ec924b 100644 --- a/src/helper.php +++ b/src/helper.php @@ -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 @@ -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 "captcha";