new
This commit is contained in:
73
app/Http/Controllers/QRCodeController.php
Normal file
73
app/Http/Controllers/QRCodeController.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\QRCodeService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class QRCodeController extends Controller
|
||||
{
|
||||
protected $qrCodeService;
|
||||
|
||||
public function __construct(QRCodeService $qrCodeService)
|
||||
{
|
||||
$this->qrCodeService = $qrCodeService;
|
||||
}
|
||||
|
||||
public function generate(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'text' => 'required|string|max:2048',
|
||||
'size' => 'required|integer|min:100|max:800',
|
||||
'errorCorrection' => 'required|in:L,M,Q,H',
|
||||
'logo' => 'nullable|string'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
$cacheKey = md5($request->text . $request->size . $request->errorCorrection . $request->logo);
|
||||
|
||||
try {
|
||||
$qrCode = Cache::remember($cacheKey, 3600, function () use ($request) {
|
||||
return $this->qrCodeService->generate(
|
||||
$request->text,
|
||||
$request->size,
|
||||
$request->errorCorrection,
|
||||
$request->logo
|
||||
);
|
||||
});
|
||||
|
||||
return response($qrCode)
|
||||
->header('Content-Type', 'image/png')
|
||||
->header('Cache-Control', 'public, max-age=3600');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => '二维码生成失败'], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadLogo(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'file' => 'required|image|mimes:jpeg,png,jpg|max:2048'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
$path = $request->file('file')->store('logos', 'public');
|
||||
return response()->json([
|
||||
'url' => asset('storage/' . $path)
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => 'Logo上传失败'], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
78
app/Services/QRCodeService.php
Normal file
78
app/Services/QRCodeService.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Intervention\Image\Facades\Image;
|
||||
use Exception;
|
||||
|
||||
class QRCodeService
|
||||
{
|
||||
protected $phpqrcode;
|
||||
protected $tempDir;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->tempDir = storage_path('app/temp');
|
||||
if (!file_exists($this->tempDir)) {
|
||||
mkdir($this->tempDir, 0777, true);
|
||||
}
|
||||
|
||||
// 引入PHP QR Code库
|
||||
require_once base_path('vendor/phpqrcode/qrlib.php');
|
||||
$this->phpqrcode = new \QRcode();
|
||||
}
|
||||
|
||||
public function generate(string $text, int $size, string $errorCorrection = 'M', ?string $logoPath = null)
|
||||
{
|
||||
$tempFile = $this->tempDir . '/' . md5($text . time()) . '.png';
|
||||
|
||||
// 设置错误纠正级别
|
||||
$errorCorrectionLevel = match($errorCorrection) {
|
||||
'L' => QR_ECLEVEL_L,
|
||||
'M' => QR_ECLEVEL_M,
|
||||
'Q' => QR_ECLEVEL_Q,
|
||||
'H' => QR_ECLEVEL_H,
|
||||
default => QR_ECLEVEL_M
|
||||
};
|
||||
|
||||
try {
|
||||
// 生成二维码
|
||||
$this->phpqrcode->png($text, $tempFile, $errorCorrectionLevel, $size);
|
||||
|
||||
// 如果提供了Logo,则合并Logo
|
||||
if ($logoPath) {
|
||||
$this->mergeLogo($tempFile, $logoPath);
|
||||
}
|
||||
|
||||
// 读取生成的图片
|
||||
$qrCode = file_get_contents($tempFile);
|
||||
|
||||
// 删除临时文件
|
||||
unlink($tempFile);
|
||||
|
||||
return $qrCode;
|
||||
|
||||
} catch (Exception $e) {
|
||||
if (file_exists($tempFile)) {
|
||||
unlink($tempFile);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
protected function mergeLogo(string $qrCodePath, string $logoPath)
|
||||
{
|
||||
$qrCode = Image::make($qrCodePath);
|
||||
$logo = Image::make($logoPath);
|
||||
|
||||
// 调整Logo大小,不超过二维码的1/4
|
||||
$logoSize = min($qrCode->width(), $qrCode->height()) / 4;
|
||||
$logo->resize($logoSize, $logoSize, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
|
||||
// 在二维码中心添加Logo
|
||||
$qrCode->insert($logo, 'center');
|
||||
$qrCode->save($qrCodePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user