php+qrcode类+生成二维码方法

时间:2022-05-22 17:56:19
  //生成二维码
public function qrcode()
{
$data = input();
if(!$data['param']){
return json(['code ' => 2, 'data' => '', 'msg' => '您有必填参数未携带']);exit;
}
if ($data['imgtype'] == 1) { //图片类型 $imgpath = "./web/logo.png";
} elseif ($data['imgtype'] == 2) { $imgpath = "./web/icon-close.png";
} else { $imgpath = "./web/logo_cart.png";
} if ($data['param']) {
$param = json_decode($data['param'], true);
$crl = '?';
foreach ($param as $key => $value) {
$crl .= $key . '=' . $value . '&';
}
}
$url = config('wapsite') . trim($crl, '&');
$name = uniqid() . '.png';
$upload_dir = ENV::get('root_path') . 'public/static/qrcode/'; if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true);
}
$filedir = $upload_dir . $name;
$qrCode = new QrCode($url);
if ($data['type'] == 1) { //logo二维码
$qrCode->setLogoPath($imgpath); //设置logo图片路径
$qrCode->setLogoWidth(60); //设置logo图片宽度
$qrCode->setLogoHeight(60); //设置logo图片高度
$qrCode->writeFile($filedir);
} else { //普通文字二维码
$qrCode->writeFile($filedir);
}
$qrsrc = request()->domain() . '/static/qrcode/' . $name;
if ($qrsrc) {
return json(['code ' => 1, 'data' => $qrsrc, 'msg' => '获取数据成功']);
} else {
return json(['code ' => 2, 'data' => '', 'msg' => '获取数据失败']);
}
}