Files
citrus-cms/app/Http/Controllers/Api/QRCodeController.php
T
2026-04-28 21:14:25 +03:00

110 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Endroid\QrCode\QrCode as EndroidQrCode;
use Endroid\QrCode\Writer\PngWriter;
class QRCodeController extends Controller
{
public function generate(Request $request): JsonResponse
{
try {
$request->validate([
'shape_id' => 'required|string',
]);
$shapeId = $request->get('shape_id');
// QR kod içeriği - shape ID ile URL oluştur
$qrContent = url('/api/pdf-editor/annotations/' . $shapeId);
// QR kod PNG olarak üret (Endroid QR Code v6)
$qrCode = new EndroidQrCode(
data: $qrContent,
size: 200,
margin: 10
);
$writer = new PngWriter();
$result = $writer->write($qrCode);
$qrCodeData = $result->getString();
// QR kod dosyasını kaydet
$fileName = 'qr_' . $shapeId . '_' . time() . '.png';
// Klasörü oluştur
$qrDir = storage_path('app/public/qr-codes');
if (!file_exists($qrDir)) {
mkdir($qrDir, 0755, true);
}
// QR kod dosyasını kaydet
$filePath = $qrDir . '/' . $fileName;
file_put_contents($filePath, $qrCodeData);
// Public klasörüne de kopyala
$publicQrDir = public_path('storage/qr-codes');
if (!file_exists($publicQrDir)) {
mkdir($publicQrDir, 0755, true);
}
$publicFilePath = $publicQrDir . '/' . $fileName;
file_put_contents($publicFilePath, $qrCodeData);
return response()->json([
'success' => true,
'data' => [
'shape_id' => $shapeId,
'qr_content' => $qrContent,
'qr_code_url' => url('storage/qr-codes/' . $fileName)
]
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage(),
'data' => null
], 500);
}
}
public function generateForAnnotation($shapeId): JsonResponse
{
try {
// QR kod içeriği
$qrContent = url('/api/annotations/' . $shapeId);
// QR kod PNG olarak üret (Endroid QR Code v6)
$qrCode = new EndroidQrCode(
data: $qrContent,
size: 150,
margin: 5
);
$writer = new PngWriter();
$result = $writer->write($qrCode);
$qrCodeData = $result->getString();
// Base64 encode for frontend
$base64QrCode = 'data:image/png;base64,' . base64_encode($qrCodeData);
return response()->json([
'success' => true,
'data' => [
'shape_id' => $shapeId,
'qr_code' => $base64QrCode,
'qr_content' => $qrContent
]
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage(),
'data' => null
], 500);
}
}
}