<?php

namespace App\Services;

use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Illuminate\Support\Facades\Storage;
class QrCodeService
{
    public function saveQrCode($text, $filename) {


   // Create QR code
   $qrCode = QrCode::create($text)
   ->setSize(400)  // Increase size of the QR code
   ->setMargin(10) // Margin around the QR code
   ->setBackgroundColor(new Color(255, 255, 255)) // White background
   ->setForegroundColor(new Color(0, 0, 0)); // Black QR code color

   $writer = new PngWriter();  
 
   // Initialize logo as null 

   $logoPath = public_path('admin/assets/images/logo/imageqr.png'); 
   $logo = null;

   // If a logo path is provided and the file exists, embed the logo into the QR code
   if (!empty($logoPath) && file_exists($logoPath)) {
    $logo = Logo::create($logoPath)
    ->setResizeToWidth(50); // Resize logo to a smaller width
   }

   // Generate the QR code with or without a logo
   $result = $writer->write($qrCode, $logo);

   // Define the file path to save the QR code
   $filePath = 'qr_codes/' . $filename . '.png';

   // Save the QR code image in the public storage
   Storage::disk('public')->put($filePath, $result->getString());

   return $filePath; 
 
} 

}
