tp5.1 生成小程序码 并替换 中间的 logo(默认是小程序的头像)

 先上代码  如果有不明白的可以加我微信zwj4_15

 直接请求这个方法即可 id  为文章id      path 是小程序的路径  如: /page/index/index

​
    public function sharImg(Request $request){
        (new IDMustBePositiveInt())->goCheck('id'); // 这个是我项目进行参数的校验 这里的id是传过来的文章id 
        $typeid = $request->post('id');//接收传过来的  文章id
        $path = $request->post('path');//path 是 小程序给的路径 比如“/page/index/index”
        $result = $this->curl_get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('setting.appid')."&secret=".config('setting.appsecret'));//小程序调用小程序码需要一个token ,token是需要appid 和appsecret
        $token = json_decode($result,true);
        $json_data=json_encode(array("path"=>"$path",'is_hyaline'=>true));//参数转为json格式  (生成小程序码参数格式必须为 json 格式)
        $url ="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token['access_token'];
        $Binary = $this->curl_post($url,$json_data);//发送请求
//        $img = (new ProductsModel())->findImg($typeid);//查找发表这篇文章人的头像  如果是每篇文章都要换中间的logo ,就可以用数据库的存起来的图片,如果只是不想用自己的logo 可以采用我下面的 file_get_contents(固定图片的路径)方法
//        $img_path = config('setting.img_path').$img['smallPic'];//把头像进行拼接成一个可以直接用浏览器打开的网址
        //用户头像图片变圆形
//        $avatar = file_get_contents("$img_path");//打开这张图片
        $avatar = file_get_contents("https://wxapi.bjjlythy.com/uploads/wuliuyuan/5niu.jpg");
        $logo   = $this->yuanImg($avatar);//返回的是图片数据流
        //二维码与头像结合
        $sharePic = $this->qrcodeWithLogo($Binary,$logo);
        //将生成的二维码保存到本地
        $path = $this->binary_to_file($sharePic);//二进制流转换成图片并保存下来 ,返回图片的路径
        $shar = (new ProductsModel())->sharimg($typeid,$path);//把图片路径保存在数据库对应的文章中
        if($shar){
            //成功则返回图片路径
            return $this->errorCode(20000,config('setting.sharimg').substr($path,1));
        }else{
            //错误抛出异常    我的抛出异常是自己写的,按自己的方法处理即可
            throw new SuccessMessage([
                'msg' => '生成失败',
                'errorCode' => 20004
            ]);
        }
    }

​

 curl_get 方法  

     /**
     * @param string $url get请求地址
     * @param int $httpCode 返回状态码
     * @return mixed
     */
    function curl_get($url, &$httpCode = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }

 curl_post 请求  注意这个 参数是json 格式

    
    //json post 请求
    public function curl_post($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8'));
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

 

    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);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $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($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
        /**
         * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
         * 并且去掉缓存区函数
         */
        //获取输出缓存,否则imagepng会把图片输出到浏览器
        ob_start();
        imagepng ( $img );
        imagedestroy($img);
        $contents =  ob_get_contents();
        ob_end_clean();
        return $contents;
    }
   public  function qrcodeWithLogo($QR,$logo){
        $QR   = imagecreatefromstring ($QR);
        $logo = imagecreatefromstring ($logo);
        $QR_width    = imagesx ( $QR );//二维码图片宽度
        $QR_height   = imagesy ( $QR );//二维码图片高度
        $logo_width  = imagesx ( $logo );//logo图片宽度
        $logo_height = imagesy ( $logo );//logo图片高度
        $logo_qr_width  = $QR_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
        $scale  = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
        $logo_qr_height = $logo_height / $scale;//组合之后logo的高度
        $from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
        /**
         * 重新组合图片并调整大小
         * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
         */
        imagecopyresampled ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
        /**
         * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
         * 并且去掉缓存区函数
         */
        //获取输出缓存,否则imagepng会把图片输出到浏览器
        ob_start();
        imagepng ( $QR );
        imagedestroy($QR);
        imagedestroy($logo);
        $contents =  ob_get_contents();
        ob_end_clean();
        return $contents;
    }

二进制流转换为照片并 保存在本地项目中

    /**
     * $file 二进制流
     */
    function binary_to_file($file){
        $imgDir = './uploads/*****/';
        //要生成的图片名字
        $filename = md5(time().mt_rand(10, 99)).".png"; //新图片名称
        $newFilePath = $imgDir.$filename;
        $data = $file;
        $newFile = fopen($newFilePath,"w"); //打开文件准备写入
        fwrite($newFile,$data); //写入二进制流到文件
        fclose($newFile); //关闭文件
        return $newFilePath;
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TP5.1程序后下载是指在使用ThinkPHP5.1框架进行开发时,在开发环境中完成程序编写后,将程序部署到服务器并进行下载的过程。 首先,我们需要确保服务器环境满足ThinkPHP5.1框架的运行要求,包括PHP版本、扩展等。然后,将开发好的程序文件打包成压缩文件,使用FTP工具或者服务器的文件管理功能将压缩文件上传到服务器的指定目录。 接下来,解压缩上传的文件,并根据服务器配置进行相关的设置。主要包括数据库配置、域名配置等。在服务器上安装好相关的依赖库,并在服务器端使用Composer对项目进行依赖管理。 完成配置后,我们可以通过浏览器访问服务器上的程序,例如域名指向的URL地址。如果配置无误,页面应该可以正常显示。可以在本地进行一些代和功能的测试,确保程序可以正常运行。 最后,我们可以通过浏览器下载程序的安装包或者源代。只需在浏览器中输入程序的下载链接即可。用户可以根据自己的需求选择下载方式,如果是安装包,可以直接安装到本地进行使用;如果是源代,可以在本地进行二次开发或者学习研究。 总结来说,TP5.1程序后下载需要进行服务器环境配置、文件上传、解压缩、依赖安装等步骤,并使用浏览器下载安装包或者源代。这样,我们就可以在服务器上部署完整的TP5.1程序,并在本地进行使用和学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值