php 验证码转base64

  • 慢点
  • 2020-05-08 17:02:22
  • 51已阅读
php-captcha扩展,验证码输出base64

    ob_start();

    // 输出图像

    imagepng($this->im);

    $content = ob_get_clean();

    imagedestroy($this->im);

    return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');


上面是php-captcha扩展的entry()输出验证码

直接对输出$content进行base64_encode就可以得到base64的图片

echo 'data:image/jpg;base64,' . base64_encode($captcha->entry()->getData());

需要加上base64头才能识别出是图片