php 小程序码中间logo替换
微信小程序码获取方式
请求地址: https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=;
access_token 调用微信接口的凭证
请求方式: POST
请求参数:
$data = array(
‘scene’ => $scene, //生成二维码中的参数数据
‘page’ => $path, // 已发布小程序的页面路径
‘width’ => 280 // 二维码的宽度
);
请求的数据格式 : json
请求方法 curl
方法详情:
public function getqrcode(){
$access_token = '' //你的access_token
$data = array(
'scene' => $scene, //生成二维码中的参数数据
'page' => $path, // 已发布小程序的页面路径
'width' => 280 // 二维码的宽度
);
$logo = ''; //要替换的图片,可以是带域名的图片链接
//微信小程序二维码的请求地址
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='. $access_token ;
$data = json_encode($data);
$result = $this->curl_http($url, $data);
//正常返回的是微信二维码的二进制数据流
$resultjson = json_decode($result, true);
if (!isset($resultjson['errcode'])) {
$my_dir = './upload/qrcode';
if (!file_exists($my_dir)) {
mkdir($my_dir, 0777); //创建存放二维码的目录
}
$path = $my_dir . '/' . md5($applet['appid'] . '/' . $scene) . '.jpg'; //新的文件名
//将剪切好的logo与返回的二维码进行拼接
$rt = $this->qrcodeWithLogo($logo,$result);
$res = file_put_contents($path, $rt);
$temp_url = '/shop'; //项目根目录临时的存放路径
$image_data = 'https://' . $_SERVER['SERVER_NAME'] . $temp_url . substr($path, 1);
} else {
//默认的图片二维码
$image_data = option('config.site_url') .'/template/user/default/images/logo.png';
}
return $image_data;
}
//拼接
public function qrcodeWithLogo($logo,$qrcode)
{
$QR = imagecreatefromstring($qrcode);
$lg= $this->yuanimg($logo);
$logo = imagecreatefromstring($lg);
$QR_width = imagesx($QR);//二维码图片宽度
$QR_height = imagesy($QR);//二维码图片高度
$logo_width = imagesx($logo);//logo图片宽度
$logo_height = imagesy($logo);//logo图片高度
$logo_qr_width = $QR_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
$scale = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height / $scale;//组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
/**
* 重新组合图片并调整大小
* imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
*/
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng($QR);
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
//将需更换的logo剪切成圆形图片
public function yuanimg($logo){
$src_img = imagecreatefromstring(file_get_contents($logo));
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng($img);
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
//curl 请求方法
public function curl_http($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers = array(
"Content-Type: application/json",
"Accept: application/json",
));
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36');
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}