广州小程序开发

微信小程序生成推广海报 并打印头像 thinkphp5开发

作者:广州小程序开发 时间:2018-10-02 21:08
小程序推广海报比较容易分享,有利于推广小程序,一个好的小程序,可以快速传播。微信小程序的推广海报生成原理和公众号的渠道二维码原理差不多。
 
话不多上代码 小程序前端代码
 
  tosharePng: function (uid) {
    var that = this;
    var user = wx.getStorageSync("user");
    if(user.level==1){
      return;
    }
    wx.showLoading({
      title: '邀请码生成中',
      mask: true
    })
  
   var pic = user.avatar;
   
    var openid = wx.getStorageSync("openid");
    var params = {
      openid: openid,
      pic: pic,
      uid: uid
    };
    wx.request({
      url: config.API_URL + 'api/Tocode/get_code',
      data: params,
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: "json",
      success: function (res) {
        if (res.data.code === 20000) {
          wx.showToast({
            title: '生成成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            invationimg: config.IMGURL + res.data.data + "?" + Math.random(),
          });
   
        }
      },
      fail: function () {
 
      },
      complete: function () {
 
      }
    })
 
  },
 
小程序后台代码 :
 
/**
* 小程序获取二维码
* time 2017.9.30
*/
public function get_code() {
$data['page'] = 'pages/list/index';
$data['width'] = input('post.width/d',430);
$data['auto_color'] = input('post.auto_color/s');
$data['line_color'] = input('post.line_color/s');
$uid = input('post.uid');
//$pid = input('post.pid/d');
$pic = input('post.pic');
$openid = input('post.openid');
//$data['scene'] = $pid;
if(!$data['page']){
return json_encode(array('code'=>40000,'msg'=>'跳转链接不能为空'));
 
}
       $username = db('users')->where('id',$uid)->value('username');
$info = $this->get_wxa_code($data,$uid);
//var_dump($info);exit;
$path = $_SERVER['DOCUMENT_ROOT'].'/uploads/code/';
if(!is_dir($path)){
mkdir(iconv("UTF-8", "GBK", $path),0777,true);
}
//$file = rand(10000000,99999999).'.png';
$file = $uid.'.png';
$paths = $path . $file;
$url = '/uploads/code/' . $file;
$res = file_put_contents( $paths,$info );
$path_head = $path.'head'.$file;
 
if($res){
$res = file_put_contents( $path_head, file_get_contents($pic));
//$res = file_put_contents( $path_head, file_get_contents('https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTK45iap26R41QyO0cH0QCDzhHRZVghH5RDONiaREwfgTALwiaQRkEu3R2grzxv7nBvexsHhbxsrRecfQ/0'));
 
$imgs = array(
'dst' => $_SERVER['DOCUMENT_ROOT'].'/images/back.png',//str_replace(__ROOT__.'/', '', C('BACKIMGURL')),//
'src' => $paths,
'head' => $path_head,
);
$this->tosize($imgs['head'],100,true);
//$roundImg = $this->toround($imgs);
$roundImg = $this->toround($imgs);
 
$textArr=array(
'name'=>$username,
'tit'=>'邀请你加入大侠之道'
);
 
 
 
$this->mergerImg($imgs,$roundImg);
$this->totxt($imgs,$textArr);
//M('share_pic')->add(array('pid'=>$pid, 'user_id'=>$this->user_id, 'purl'=>$paths, 'createtime'=>time()));
return json_encode(array('code'=>20000, 'msg'=>'success', 'data'=>$url, 'content'=>'' ));
 
 
 
}
 
return json_encode(array('code'=>40000,'msg'=>'生成失败'));
 
 
}
 
  //生成圆二维码
public function toround($imgs,$path='./'){
//       $w = 100;  $h=100; // original size
$sizeImg = $this->tosize($imgs['src'], 400);
//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);
 
 
imagedestroy($src);
 
return $newpic;
}
/**
* 小程序获取二维码
* @param $param
* @return bool|mixed
* time 2017.10.26
*/
public function  get_wxa_code($param,$uid){
 
$tokenInfo = $this->set_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'].'?uid='.$uid;
$parm['width'] = $param['width'];
 
return $this->curl_data($url,json_encode($parm));
}
//添加文字
public function totxt($src,$textArr){
 
//获取图片信息
$info = getimagesize($src['src']);
//        var_dump($info);die;
//获取图片扩展名
$type = image_type_to_extension($info[2],false);
//动态的把图片导入内存中
$fun = "imagecreatefrom{$type}";
$image = $fun($src['src']);
//指定字体颜色
$red    = imagecolorallocatealpha($image, 255, 0, 0, 25);
//指定字体颜色
$col1 = imagecolorallocatealpha($image,255, 255, 254, 20);
$font_file = $_SERVER['DOCUMENT_ROOT'].'/uploads/msyh.ttc';
//$font_file ='/uploads/msyh.ttc';
 
 
//指定字体内容
//$str = iconv('gb2312','utf-8','山水');//解决乱码问题
//imagefttext($image, 18, 0, 314, 1134, $col1, $font_file,$str);
imagefttext($image, 24, 0,294, 1164, $col1, $font_file,mb_convert_encoding($textArr['tit'],'html-entities','UTF-8'));
 
 
 
//姓名
$c = imagettfbbox(20,0, $font_file,$textArr['name'] );
//指定字体内容
imagefttext($image, 20, 0,294, 1114, $red, $font_file,mb_convert_encoding($textArr['name'],'html-entities','UTF-8'));
 
 
 
//指定输入类型
header('Content-type:'.$info['mime']);
//动态的输出图片到浏览器中
$func = "image{$type}";
$func($image,$src['src']);
//销毁图片
//exit;
imagedestroy($image);
 
}
/**
* 设置小程序token
* @return mixed
*/
public function set_token(){
$access_token = cache('access_token');
$tokenInfo = json_decode($access_token,true);
//判断access_token是否失效
if($tokenInfo['expires_in'] - time() < 2000){
//重新获取access_token
$appid= db('wx_user')->where('id',1)->find();
$access_token = $this->get_access_token($appid['appid'],$appid['appsecret']);
$tokenInfo = json_decode($access_token,true);
$tokenInfo['expires_in'] = $tokenInfo['expires_in']+time();
if($tokenInfo['errcode']){
return $tokenInfo;
}
cache('access_token',json_encode($tokenInfo,JSON_UNESCAPED_UNICODE));
}
return json_decode($access_token,true);
 
}
这样就可以时生成带头像和微信昵称的推广海报了

上一篇:小程序读取文章列表判读文章是否为已读
下一篇:最新,在小程序里可以关注公众号了

您的疑惑,我们来解答

多年来我们为上百家企业提供了网站建设服务

感受专业服务,从来电咨询开始
广州:18814374480深圳:13480275394
在线咨询