码迷,mamicode.com
首页 > Web开发 > 详细

PHP生成二维码

时间:2019-01-01 21:46:24      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:put   evel   esx   server   image   ges   dex   pat   out   


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);
    }
    \PHPQRCode\QRcode::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);
        }
        \PHPQRCode\QRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
        $QR = file_get_contents($outfile);
    } else {
        ob_start();
        \PHPQRCode\QRcode::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);
}

PHP生成二维码

标签:put   evel   esx   server   image   ges   dex   pat   out   

原文地址:https://www.cnblogs.com/qixidi/p/10206078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!