php实现小程序码自定义中间icon

小程序码生成的时候是默认使用小程序后台设置的小程序icon图片的,但是在有些场景我们可能要替换成我们自己想要的icon。
下面先放代码:

public function makeNewQrCodeAction() { //获取用户头像并转string $avatarUrl = $this->_req->getQuery('avatarUrl', ""); if (!$avatarUrl) { response::err_lack_param(); } $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'); $data['is_hyaline'] = $this->_req->getQuery('is_hyaline'); $data['page'] = $this->_req->getQuery('path'); $wxModel = new HdWxAuthModel(); $Qr_code = $wxModel->getShareCode($data); //生成小程序码接口 //小程序码与头像进行拼接 $url = $this->makeOnePic($Qr_code, $logo); response::result($url); } private function makeOnePic($qr_code, $logo) //二维码与头像组合 { $qr_code = imagecreatefromstring($qr_code); $icon = imagecreatefromstring($logo); $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 + 1;// 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); //拾取一个完全透明的颜色,最后一个参数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($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实现小程序码自定义中间icon

存在的问题:

最后生成的二维码中间的icon可能会出现位置不居中的问题。这个我这边设置大小width大小为280左右的时候就大概能看。应该是算icon图位置的时候没有设好。这个有空要重新来一遍。

相关文章