tp6.0.12在验证码去掉了api判断是处于啥原因呢?前后分离API获取不到验证码

浏览:585 发布日期:2022/01/30 分类:ThinkPHP6专区
public function create(string $config = null, bool $api = false)
{
$this->configure($config);

$generator = $this->generate();

// 图片宽(px)
$this->imageW || $this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2;
// 图片高(px)
$this->imageH || $this->imageH = $this->fontSize * 2.5;
// 建立一幅 $this->imageW x $this->imageH 的图像
$this->im = imagecreate($this->imageW, $this->imageH);
// 设置背景
imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]);

// 验证码字体随机颜色
$this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150));

// 验证码使用随机字体
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';

if (empty($this->fontttf)) {
$dir = dir($ttfPath);
$ttfs = [];
while (false !== ($file = $dir->read())) {
if ('.' != $file[0] && substr($file, -4) == '.ttf') {
$ttfs[] = $file;
}
}
$dir->close();
$this->fontttf = $ttfs[array_rand($ttfs)];
}

$fontttf = $ttfPath . $this->fontttf;

if ($this->useImgBg) {
$this->background();
}

if ($this->useNoise) {
// 绘杂点
$this->writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->writeCurve();
}

// 绘验证码
$text = $this->useZh ? preg_split('/(?<!^)(?!$)/u', $generator['value']) : str_split($generator['value']); // 验证码

foreach ($text as $index => $char) {

$x = $this->fontSize * ($index + 1) * mt_rand(1.2, 1.6) * ($this->math ? 1 : 1.5);
$y = $this->fontSize + mt_rand(10, 20);
$angle = $this->math ? 0 : mt_rand(-40, 40);

imagettftext($this->im, $this->fontSize, $angle, $x, $y, $this->color, $fontttf, $char);
}

ob_start();
// 输出图像
imagepng($this->im);
$content = ob_get_clean();
imagedestroy($this->im);

// api调用 这里6.0.12删除了
if ($api) {
return [
'code' => implode('', $text),
'img' => 'data:image/png;base64,' . chunk_split(base64_encode($content))
];
}


return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>