public function makeNewQrCodeAction()
{
//获取用户头像并转string
$avatarUrl = $this->_req->getQuery('avatarUrl', "");
// if (!$avatarUrl) {
// response::err_lack_param();
//}
if (!$avatarUrl) {
$avatarUrl = file_get_content(APP_PATH . "/public/imgs/default.png"); //这边如果微信用户 没有设置头像,给一个默认的头像,不然得到的二维码是空白图。
}
$avatar_file = file_get_contents($avatarUrl);
$logo = $this->changeAvatar($avatar_file);
//获取小程序码
$data['scene'] = $this->_req->getQuery('code', 1);
$data['width'] = (int)$this->_req->getQuery('width', 280);
$data['auto_color'] = $this->_req->getQuery('auto_color');
$data['line_color'] = $this->_req->getQuery('line_color'); //看了很多人说设置线条颜色失败,我也尝试了下,发现失败可能存在这两个原因其一:1、没有设置auto_color的值为true;2、设置的颜色微信还不支持。我尝试的rgb(255,0,0)是可以的,但是rgb(0,255,0)就不支持了。所以遇到设置线条颜色无效的可以先设置rgb(255,0,0)看看先
$data['is_hyaline'] = $this->_req->getQuery('is_hyaline'); //设置二维码底色是否透明,默认false
$data['page'] = $this->_req->getQuery('path');
$wxModel = new \Hd\WxAuthModel();
$Qr_code = $wxModel->getShareCode($data); //生成小程序码接口
// file_put_contents('/tmp/tmp_qr.png',$Qr_code); exit; //这里先看一下生成的小程序码是否是自己设置的格式
//小程序码与头像进行拼接
$url = $this->makeOnePic($Qr_code, $logo);
response::result($url);
}
private function makeOnePic($qr_code, $logo) //二维码与头像组合
{
$qr_code = imagecreatefromstring($qr_code); //生成的二维码底色为白色
//设置二维码为透明底
imagesavealpha($qr_code, true); //这个设置一定要加上
$bg = imagecolorallocatealpha($qr_code, 255, 255, 255, 127); //拾取一个完全透明的颜色,最后一个参数127为全透明
imagefill($qr_code, 0, 0, $bg);
$icon = imagecreatefromstring($logo); //生成中间圆形logo (微信头像获取到的logo的大小为132px 132px)
$qr_width = imagesx($qr_code); //二维码图片宽度
// $qr_height = imagesy($qr_code); //二维码图片高度
$lg_width = imagesx($icon); //logo图片宽度
$lg_height = imagesy($icon); //logo图片高度
// var_dump($qr_width,$qr_height);
// var_dump($lg_width,$lg_height);
$qr_lg_width = $qr_width / 2.2;
$scale = $lg_width / $qr_lg_width;
$qr_lg_height = $lg_height / $scale;
$start_width = ($qr_width - $lg_width) / 2 + 2; //(获取logo的左上方的位置:( 外部的正方形-logo的宽 ) / 2,我这边存在1px的偏差 我就给+2啦)
// var_dump($scale,$qr_lg_height);
// var_dump($start_width);
imagecopyresampled($qr_code, $icon, $start_width, $start_width, 0, 0, $qr_lg_width, $qr_lg_height, $lg_width, $lg_height);
//传回处理好的图片url
// $qrcode = "/imgs/qrCode" . time() . ".png";
// $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// $tmp_url = $protocol . $_SERVER['HTTP_HOST'] . $qrcode; //LCT这个需上线后除去
// response::result($tmp_url);
imagepng($qr_code); //保存
imagedestroy($qr_code);
imagedestroy($icon);
exit;
}
private function changeAvatar($avatar)
{
//处理用户头像为圆形icon
$avatar = imagecreatefromstring($avatar);
$w = imagesx($avatar);
$h = imagesy($avatar);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
imagesavealpha($img, true);
$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($avatar, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
ob_start();
imagepng($img);
imagedestroy($img);
imagedestroy($avatar);
$contents = ob_get_contents(); 、、读取缓存区的内容
ob_end_clean(); //清空缓存区
return $contents;
}
public function getShareCode($data) //生成小程序码
{
$access_token = $this->getAccessToken(); //获取access_token这个要设置token缓存,具体可以查看我的另一篇文章
$res_url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$access_token";
header('content-type:image/png');
$data = json_encode($data);
$Qr_code = $this->http_request($res_url, $data);
return $Qr_code;
}
php实现自定义中间logo的微信小程序码
最新推荐文章于 2023-07-26 15:28:53 发布