1、调用方法
//调用get_qrcode方法,获取二维码图片
$base64_img = get_qrcode($id);
//调用base64_save方法,将图片保存到指定位置,并获取图片新名称
$new_name = base64_save($base64_img,'./Uploads/qrcode/');
2、生成二维码图片
//生成二维码
function get_qrcode($id){
$APPID = "xxxx";
$APPSECRET = "xxxxxxxxx";
//获取access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
//缓存access_token
session_start();
$_SESSION['access_token'] = "";
$_SESSION['expires_in'] = 0;
$ACCESS_TOKEN = "";
if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in'])){
$json = httpRequest( $access_token );
$json = json_decode($json,true);
$_SESSION['access_token'] = $json['access_token'];
$_SESSION['expires_in'] = time()+7200;
$ACCESS_TOKEN = $json["access_token"];
}else{
$ACCESS_TOKEN = $_SESSION["access_token"];
}
//构建请求二维码参数
//path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
//width是二维码宽度
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
$param = json_encode(array("path"=>"pages/weixiurenyuan/weixiurenyuan?id=$id","width"=>150));
//POST参数
$result = httpRequest( $qcode, $param,"POST");
//生成二维码
// file_put_contents("qrcode.png", $result);
$base64_image = "data:image/jpeg;base64,".base64_encode( $result );
return $base64_image; //返回base64图片数据
}
//把请求发送到微信服务器换取二维码
function httpRequest($url, $data='', $method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST'){
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != ''){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/*
* 保存base64格式图片到指定文件夹中
* $base64_img 图片数据
* $path 要保存到的图片地址
*/
function base64_save($base64_img,$path){
$img_base = str_replace('data:image/jpeg;base64,', '', $base64_img);
$img_name = time().rand(1000,9999).'.jpg'; //图片新名称
$img_path = $path.$img_name;
file_put_contents($img_path,base64_decode($img_base));
return $img_name; //返回新名称
}
3、此时生成的二维码图片如下,到此二维码生成完成。
4、根据项目需要,将图片下方文字更换,项目使用TP3.2框架
//之前获取到的二维码图片
$qrcode = '/Uploads/qrcode/'.$new_name;
//编号
$number = 'WLSB000100';
//裁剪图片
$image = new \Think\Image();
$image->open($_SERVER['DOCUMENT_ROOT'].$qrcode);
$image->crop(282,278)->save('./Uploads/qrcode/'.$new_name);
//调用createQrcodejpg方法,拼接二维码图片,加上编号
createQrcodejpg($number,'./Uploads/qrcode/'.$new_name,'./Uploads/qrcode/'.$new_name);
5、拼接图片方法
/**
* 拼接图片生成
* @param $title 文字内容
* @param $codeName 二维码图片
* @param $fileName string 保存文件名,默认空则直接输入图片
*/
function createQrcodejpg($title,$codeName,$fileName = ''){
//创建画布
$im = imagecreatetruecolor(282, 321);
//填充画布背景色
$color = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $color);
//字体文件
$font_file = "./Public/Common/msyh.ttf";
//设定字体的颜色
$font_color_1 = ImageColorAllocate ($im, 28, 28, 28);
$font_color_2 = ImageColorAllocate ($im, 129, 129, 129);
//二维码图片
list($code_w,$code_h) = getimagesize($codeName);
$codeImg = createImageFromFile($codeName);
imagecopyresized($im, $codeImg, 0, 0, 0, 0, 282, 278, $code_w, $code_h);
//文字内容
imagettftext($im, 16,0, 76, 300, $font_color_1 ,$font_file, $title);
//输出图片
if($fileName){
imagepng ($im,$fileName);
}else{
Header("Content-Type: image/jpg");
imagepng ($im);
}
//释放空间
imagedestroy($im);
imagedestroy($goodImg);
imagedestroy($codeImg);
}
/**
* 从图片文件创建Image资源
* @param $file 图片文件,支持url
* @return bool|resource 成功返回图片image资源,失败返回false
*/
function createImageFromFile($file){
if(preg_match('/http(s)?:\/\//',$file)){
$fileSuffix = getNetworkImgType($file);
}else{
$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
}
if(!$fileSuffix) return false;
switch ($fileSuffix){
case 'jpeg':
$theImage = @imagecreatefromjpeg($file);
break;
case 'jpg':
$theImage = @imagecreatefromjpeg($file);
break;
case 'png':
$theImage = @imagecreatefrompng($file);
break;
case 'gif':
$theImage = @imagecreatefromgif($file);
break;
default:
$theImage = @imagecreatefromstring(file_get_contents($file));
break;
}
return $theImage;
}
/**
* 获取网络图片类型
* @param $url 网络图片url,支持不带后缀名url
* @return bool
*/
function getNetworkImgType($url){
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
curl_exec($ch);//执行curl会话
$http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
curl_close($ch);//关闭资源连接
if ($http_code['http_code'] == 200) {
$theImgType = explode('/',$http_code['content_type']);
if($theImgType[0] == 'image'){
return $theImgType[1];
}else{
return false;
}
}else{
return false;
}
}
6、此时生成的图片如下