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);
    }
    \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);
}

posted @ 2019-01-01 21:23  栖息地  阅读(438)  评论(0编辑  收藏  举报