PHP 生成透明图片 透明海报二维码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/sym134/article/details/100201659
    /**
     * 生成车贴
     *
     */
    private function makeCarPoster($user_id)
    {
        $code ='邀请码';
        $code_url = 'http://www.xxx.com?id=' . $code;
        // 短链接
        vendor('autoload');  //tp框架引入微信插件
        $wx_qr = new \WeChat\Qrcode([
            'appid'     => '',
            'appsecret' => '',
            'mch_id'    => '',
            'mch_key'   => '',
        ]);
        $code_url = $wx_qr->shortUrl($code_url);
        if ($code_url['errcode'] !== 0) {
            if (empty($order)) $this->error('短链接生成失败');
        }
        $code_url = $code_url['short_url'];

        // 引入phpqrcode 生成二维码
        Vendor('phpqrcode.phpqrcode');
        // 生成二维码
        $file = 'Uploads/CarPoster/QRcode/' . $code . '.png';
        if (!file_exists($file)) {
            $url = urldecode($code_url);
            \QRcode::png($url, $file, 'L', 5, 0);   // $url 二维码内容  $file 保存路径  L容错  5 像素 0 外边距、外白边
        }
        $data = [
            'name'       => $code . '-' . date('YmdHis'),
            'bigImgPath' => "Uploads/CarPoster/6.png", // 海报、车贴背景图
            'qCodePath'  => $file,  // 二维码路径
            'left'       => '100',  // 二维码定位
            'top'        => '500',
        ];
        // 生成图保存目录
        $data['filename'] = 'Uploads/CarPoster/' . $data['name'] . ".png";

        $this->createImage($data);
        @unlink($file);// 删除二维码
        return $data;
    }

    /**
     * 创建图
     *
     * @param $data
     * Date: 2019/9/2
     * Time: 10:41
     * Author:sym
     */
    private function createImage($data)
    {
        $left = isset($data['left']) ? $data['left'] : 100;
        $top = isset($data['top']) ? $data['top'] : 100;
        
        // 背景资源
        $image = imagecreatefrompng($data['bigImgPath']);
        imagesavealpha($image, true);
        imagealphablending($image, false);
        imagefilter($image, IMG_FILTER_COLORIZE, 0, 0, 0, 0);
        // 二维码资源
        $qCodeImg = imagecreatefromstring(file_get_contents($data['qCodePath']));
        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($data['qCodePath']);
        imagecopymerge($image, $qCodeImg, $left, $top, 0, 0, $qCodeWidth, $qCodeHight, 100);

        imagepng($image, $data['filename'], 0);
        imagedestroy($image);
        imagedestroy($qCodeImg);
    }

    /**
     * 下载文件
     *
     * @param string $file_url
     * @param string $name
     * Date: 2019/8/29
     * Time: 09:51
     * Author:sym
     */
    private function downFile($file_url, $name)
    {
        $name = $name . '-' . date("Ymd-H:i:m");
        header("Content-type:  application/octet-stream ");
        header("Accept-Ranges:  bytes ");
        header("Content-Disposition:  attachment;  filename= {$name}.png");
        header("Accept-Length: " . readfile($file_url));
    }

phpqrcode  http://phpqrcode.sourceforge.net/docs/html/index.html

推荐看一下这个,功能很不错!

GImage https://github.com/joseluisq/gimage

猜你喜欢

转载自blog.csdn.net/sym134/article/details/100201659