/**
* 查看通行证
*/
public function viewPassCode() {
$visitor_id = $this->input_data['visitor_id'];
$customer_visitor = M('customer_visitor');
$customer_visitor_info = $customer_visitor->where(['id'=>$visitor_id])->find();
$qr_content = $customer_visitor_info['qr_content'];
// $qrCode = new QrCode($qr_content);
// // 指定内容类型
// header('Content-Type: '.$qrCode->getContentType());
// // 输出二维码
// echo $qrCode->writeString();
$qrCode = new QrCode($qr_content);
$imgContent = $qrCode->writeString();
$base64 = ImageUtil::contentToBase64($imgContent);
$this->json->ok($base64);
}
<?php
/**
* 图片工具
* User: Eden
* Date: 19-4-26 上午9:23
*/
namespace CommonUtil;
class ImageUtil extends CommonUtil
{
/**
* 获取网络图片的Base64编码
* $img_url 网络图片地址
* $hasPre 是否有前缀
* @return string
*/
public static function imgToBase64($img_url,$hasPre = true)
{
$img_base64 = '';
$imageInfo = getimagesize($img_url);
if (!$imageInfo) {
return false;
}
$img_base64 = "" . chunk_split(base64_encode(file_get_contents($img_url)));
if ($hasPre) {
$img_base64 = 'data:' . $imageInfo['mime'] . ';base64,'.$img_base64;
}
return $img_base64;
}
/**
* 获取网络图片的Base64编码
* $img_url 网络图片地址
* $hasPre 是否有前缀
* @return string
*/
public static function contentToBase64($content,$hasPre = true)
{
$img_base64 = '';
$img_base64 = "" . chunk_split(base64_encode($content));
if ($hasPre) {
$img_base64 = 'data:image/png;base64,'.$img_base64;
}
return $img_base64;
}
}