• PHP生成二维码



    createQrcode.php

    
    <?php
    
    /**
     * composer 安装 composer require aferrandini/phpqrcode
     */
     
    /**
    * 二维码生成
    * Time:2018/7/20 0020
    */
    public function createQrcode(){
    	$id=input('id');
    	$url = 'http://' . $_SERVER['HTTP_HOST'] . '/index/user/center?car_id=' . $id;
    //		$res = create_qrcode($id,$url,'car');  //生成不带logo的二维码
    	$res = create_png($url, true, './uploads/qrcode/car/car_'.$id.'.png', 'H');  //生成带logo的二维码
        $re = $this->logicCar->editField($id,'qrcode',$res);
        $this->ajaxJump($re);
    }/**二维码生成器
     * User:wanglu
     * Time:2018/7/20 0020
     * @param $id
     * @return bool|string
     */
    
    function create_qrcode($id, $url, $model)
    {
        $pash = './uploads/qrcode/'.$model.'/'.$model.'_' . $id . '.png';
        if (!file_exists(dirname($pash))){
            mkdir(dirname($pash),0777,true);
        }
        if(file_exists($pash)){
            unlink($pash);
        }
        PHPQRCodeQRcode::png($url, $pash, 'H', 10,2);
        $path = substr($pash, 1);
    
        return $path;
    }
    
    /**
     * 生成带logo的二维码
     * @param $text         二维码内容
     * @param bool $logo    是否带log
     * @param bool $outfile 输出地址
     * @param string $level 容错等级
     * @param int $size     尺寸
     * @param int $margin   边框
     * @param bool $saveandprint
     * @return bool|string
     * User: Dh106
     * Date: 2018/8/10
     * Time: 9:16
     */
    function create_png($text, $logo = false, $outfile = false, $level = 'H', $size = 10, $margin = 2, $saveandprint=false)
    {
        if ( $outfile ) {
            if (!file_exists(dirname($outfile))){
                mkdir(dirname($outfile),0777,true);
            }
            PHPQRCodeQRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
            $QR = file_get_contents($outfile);
        } else {
            ob_start();
            PHPQRCodeQRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
            $QR = ob_get_contents();
            ob_end_clean();
        }
        $logopath = ROOT_PATH.'public/static/platform/images/yycxlogo.png';
    
        //合并logo
        if ( $logo !== false && $logo = file_get_contents($logopath) ) {
            $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 / 5;
            // $scale          = $logo_width/$logo_qr_width;
            // $logo_qr_height = $logo_height/$scale;
            // $from_width     = ($QR_width - $logo_qr_width) / 2;
            // imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    
            //logo不失真
            $dstX = $QR_width/2 - $logo_width/2;
            $dstY = $QR_width/2 - $logo_width/2;
            imagecopyresampled($QR, $logo, $dstX, $dstY, 0, 0, $logo_width, $logo_height, $logo_width, $logo_height);
    
            if ( $outfile ) {
                imagepng($QR, $outfile);
            } else {
                ob_start();
                imagepng($QR);
                ob_get_contents();
                ob_end_clean();
            }
        }
    
        return substr($outfile,1);
    }
    
    
  • 相关阅读:
    python基础之for循环
    python基础之数据类型转换
    python基础之集合set
    python基础之元祖tuple
    python基础之字典dict
    python基础之列表list
    Java基础之数据类型、运算符、标识符
    Java 基础之面向对象
    Java基础之Javadoc的使用
    MYSQL基础之安装、启动、停止、添加、移除、初始化服务
  • 原文地址:https://www.cnblogs.com/qixidi/p/10206078.html
Copyright © 2020-2023  润新知