$accesstoken = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$accesstoken;
$param = json_encode(array("path"=>"pages/index/index?user_id=".$superId,"width"=> 150));
$result = curl_request($url,'post',$param);
$avatarUrl = db('user')->where('id',$this->user_id)->value('avatar');
$avatar = file_get_contents($avatarUrl);
$logo = yuanImg($avatar);
$sharePic = qrcodeWithLogo($result,$logo);
file_put_contents($file_path,$sharePic);
function qrcodeWithLogo($QR,$logo){
$QR = imagecreatefromstring ($QR);
$logo = imagecreatefromstring ($logo);
$QR_width = imagesx ( $QR );
$QR_height = imagesy ( $QR );
$logo_width = imagesx ( $logo );
$logo_height = imagesy ( $logo );
$logo_qr_width = $QR_width / 2.2;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
ob_start();
imagepng ( $QR );
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
function yuanImg($picture) {
$src_img = imagecreatefromstring($picture);
$w = imagesx($src_img);
$h = imagesy($src_img);
$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;
$y_y = $r;
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);
}
}
}
ob_start();
imagepng ( $img );
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}