代码
composer require endroid/qr-code
<?php
namespace App\Service;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
class ImageService{
public static function create($config = [], $filename = ''){
$imageDefault = array(
'left' => 0,
'top' => 0,
'width' => 100,
'height' => 100,
'opacity' => 100,
'circle' => 0
);
$qrcodeDefault = [
'text' => 'qrcode content',
'size' => 100,
'margin' => 2,
'left' => 0,
'top' => 0,
'color' => '0,0,0'
];
$textDefault = array(
'text' => '',
'left' => 0,
'top' => 0,
'align' => '',
'font-size' => 32,
'color' => '0,0,0',
'angle' => 0,
'font-path' => RUNTIME_PATH . '/font/SourceHanSansCN-Regular.otf'
);
$background = $config['background'];
$backgroundInfo = getimagesize($background);
$backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
$background = $backgroundFun($background);
$backgroundWidth = imagesx($background);
$backgroundHeight = imagesy($background);
$imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
$color = imagecolorallocate($imageRes, 0, 0, 0);
imagefill($imageRes, 0, 0, $color);
imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy