php实现自定义中间logo的微信小程序码
生活随笔
收集整理的這篇文章主要介紹了
php实现自定义中间logo的微信小程序码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
php實現自定義中間logo的微信小程序碼這里寫自定義目錄標題
小程序碼生成的時候是默認使用小程序后臺設置的小程序icon圖片的,但是在有些場景我們可能要替換成我們自己想要的icon。
下面先放代碼: 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的微信小程序码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 令人头痛的WH_CBT钩子,使窗口前置—
- 下一篇: java-php-python-ssm新