110 lines
3.4 KiB
PHP
110 lines
3.4 KiB
PHP
<?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);
|
||
}
|
||
}
|
||
} |