记录一下PHP生成小程序海报(含转圆角的微信头像和小程序二维码)

记录一下PHP 生成带小程序二维码的海报

  • 含文字 微信头像(转圆角)
  • 生成小程序二维码的
  • 本文只是记录了样例,实际开发还需根据业务逻辑自行处理
<?php

    /**
     * @param $user (用户信息)
     * 生成分享图片
     */  
  public function img_sc($user){
      $img =$this->yuan_img($user->avatar);//微信头像转圆形
      //载入圆形的微信头像
      $imgg=imagecreatefrompng( $_SERVER['DOCUMENT_ROOT'].$img);
      list($l_w,$l_h) = getimagesize($user->avatar);
      // 从现在文件载入一幅图像 背景图
      $im = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].'/2.jpg');
      // 从现在文件载入一幅图像 二维码图片
      //获取微信二维码图片
      $wx_code=$this->wx_code($user->id,'/pages/index/login/login');
      $codeIm = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].$wx_code);
      // 获取图片的宽、高
      list($w, $h) = getimagesize($_SERVER['DOCUMENT_ROOT'].$wx_code);
      //设置字
      $boldFontFile = $_SERVER['DOCUMENT_ROOT'] . '/stsong.ttf';
      // 为画布分配一个颜色,主要用于写文字
      $font_color_1 = imagecolorallocate($im, 255, 148, 44);
      $title = $user->nick_name;
       $dibu='长按识别二维码 进入小程序学习课程';
      // 向画布中写文件
      //昵称
      imagettftext($im, 26, 0, $x, 450, $font_color_1, $boldFontFile, $title);
      //画板
      imagecopyresized($im, $codeIm, 135, 800, 0, 0, 300, 300, $w, $h);
      imagecopyresized($im, $imgg, 235, 285, 0, 0, 120, 120, $l_w, $l_h);
      imagettftext($im, 12, 0, 170, 1120, $font_color_1, $boldFontFile, $dibu);
     // 输出图片
      header('content-type: image/png');
       $IM=imagepng ($im);
      die;
  }
    /*
     * @fun 图片转换成圆形png,传入源路径和转换后的路径,均用相对于当前程序文件的路径
     * @memo 对于非正方形的图片,以短边作为图片的直径
     * @param string $src 源路径
     * @param string $dst 转换后的路径
     */
    public  function yuan_img($src) {
        //获取原图尺寸,并设置新图片的宽度和高度
        list($w, $h) = getimagesize($src);
        if( $w > $h ){
            $w = $h;
        }else{
            $h = $w;
        }
        $oimgSrc = imagecreatefromstring(file_get_contents($src));
        $oimgDst = imagecreatetruecolor($w, $h);
        imagealphablending($oimgDst,false);
        $transparent = imagecolorallocatealpha($oimgDst, 0, 0, 0, 127);
        $r=$w/2;
        for($x=0;$x<$w;$x++){
            for($y=0;$y<$h;$y++){
                $c = imagecolorat($oimgSrc,$x,$y);
                $_x = $x - $w/2;
                $_y = $y - $h/2;
                if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
                    imagesetpixel($oimgDst,$x,$y,$c);
                }else{
                    imagesetpixel($oimgDst,$x,$y,$transparent);
                }
            }
        }
        $str = uniqid(mt_rand(),1);
        $file='/uploads/user/wx_tx/'.date('Ymd',time()).'/';
        $file2=$_SERVER['DOCUMENT_ROOT'].$file;
        if(!is_dir($file2))//检测目录是否存在
        {
            mkdir($file2,0777,true);
        }
        imagesavealpha($oimgDst, true);
        imagepng($oimgDst, $file2.md5($str).'.png');
        imagedestroy($oimgDst);
        imagedestroy($oimgSrc);
         return  $file.md5($str).'.png';
    }
        //获取微信生成二维码
    public  function wx_code($user_id,$JumpUrl){
        $appid ='此处填写微信的appid';
        $secret ='此处填写微信的AppSecret';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        //开启session
        // 保存2小时
        $lifeTime = 2 * 3600;
        setcookie(session_name(), session_id(), time() + $lifeTime, "/");
        if(empty($access_token)){
            $access_token_data = $this->getJson($url);
            $access_token = $access_token_data['access_token'];
            $_SESSION['access_token'] = $access_token;
        }
        $access_token = $_SESSION['access_token'];
        if(!empty($access_token)){
            $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
            $data['path'] = $JumpUrl;
            $data['scene'] =$user_id;//(string类型,必须是数字)
            $data['width'] = 430;
            $result = $this->curlPost($url,$data,'POST');
            $str = uniqid(mt_rand(),1);
            $file='/uploads/user/qr_code/'.date('Ymd',time()).'/';
            $file2=$_SERVER['DOCUMENT_ROOT'].$file;
            if(!is_dir($file2))//检测目录是否存在
            {
                mkdir($file2,0777,true);
            }
            file_put_contents($file2.$user_id.md5($str).'.png', $result, true);
            return  $file.$user_id.md5($str).'.png';
        }
    }
    //封装curl请求
    private   function curlPost($url,$data,$method){
        $ch = curl_init();   //1.初始化
        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
        //4.参数如下
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        if($method=="POST"){//5.post方式的时候添加数据
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);//6.执行

        if (curl_errno($ch)) {//7.如果出错
            return curl_error($ch);
        }
        curl_close($ch);//8.关闭
        return $tmpInfo;
    }
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是誰萆微了承諾

你的鼓励是对我最大的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值