微信小程序生成推广二维码海报 thinkphp开发
作者:广州小程序开发 时间:2018-08-19 11:02
微信小程序生成带参数渠道二维码方法 ,通过这种方法可以开发小程序渠道来源统计,生成海报分享朋友圈,分销等功能
public function get_wxa_code($param,$uid){
$tokenInfo = $this->token()['access_token'];
//获取二维码
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$tokenInfo;
//$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tokenInfo;
$parm['path'] = $param['page'] ?: 'pages/index?uid='.$uid;
$parm['width'] = $param['width'] ?: 400;
$parm['auto_color'] = false;
if(!$param['page']){
return false;
}
return $this->curl_data($url,json_encode($parm));
}
token 为获取 token方法,百度有很多,海报图片合成方法
//合并
public function mergerImg($imgs,$oth) {
//生成原型图
imagepng($oth, $imgs['src']);
list($max_width, $max_height) = getimagesize($imgs['dst']);
$dests = imagecreatetruecolor($max_width, $max_height);
$dst_im = imagecreatefrompng($imgs['dst']);
imagecopy($dests,$dst_im,0,0,0,0,$max_width,$max_height);
imagedestroy($dst_im);
//合成二维码
$src_im = imagecreatefrompng($imgs['src']);
imagealphablending($src_im,true);
$src_info = getimagesize($imgs['src']);
imagecopy($dests, $src_im,$max_width/2-$src_info[0]/2,$max_height/2-10,0,0,$src_info[0],$src_info[1]);
//$textArr=array(
// 'tit'=>'山水',
// 'name'=>'水域'
//);
$this->totxt($imgs,$textArr);
//合成头像
$head_img = imagecreatefrompng($imgs['head']);
imagealphablending($head_img,true);
//将头像处理圆角
//$a['src'] = $imgs['head'];
//$head_img = $this->toround($a);
//var_dump($head_img);exit;
//获取头像长宽等信息
$head_info = getimagesize($imgs['head']);
imagecopy($dests, $head_img,$max_width/2-$head_info[0]/2,45,0,0, $head_info[0],$head_info[1]);
//imagedestroy($src_im);
header("Content-type: image/png");
imagepng($dests,$imgs['src']);
// imagepng($dests);
unlink($imgs['head']);
}
生成二维码圆形方法
public function toround($imgs,$path='./'){
// $w = 100; $h=100; // original size
$sizeImg = $this->tosize($imgs['src'], 201);
//var_dump($sizeImg);exit;
header('content-type:image/png');
imagepng($sizeImg,$imgs['src']);
// $dest_path = $path.uniqid().'.png';
$src = imagecreatefromstring(file_get_contents($imgs['src']));
//取得源图片的宽度和高度
list($w,$h)=getimagesize($imgs['src']);
$newpic = imagecreatetruecolor($w,$h);
imagealphablending($newpic,false);
$transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127);
//var_dump($transparent);exit;
imageantialias ( $newpic ,true );
$r=$w/2;
for($x=0;$x<$w;$x++)
for($y=0;$y<$h;$y++){
$c = imagecolorat($src,$x,$y);
$_x = $x - $w/2;
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($newpic,$x,$y,$c);
}else{
imagesetpixel($newpic,$x,$y,$transparent);
}
}
imagesavealpha($newpic, true);
// header('content-type:image/png');
// imagepng($newpic);
// imagepng($newpic, $dest_path);
imagedestroy($src);
// unlink($url);
return $newpic;
}
以下图片为案例
上一篇:微信小程序html解析
下一篇:小程序定制开发,助力企业商家轻松超越同行