<?php

namespace App\Helpers;

use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;

class QrCodeHelper
{
    public static function generateBarcode($text) 
    {
        $code = $text;
        $fontSize = 10;
        $height = 50;
        
        // Create image
        $img = imagecreate(strlen($code) * 15 + 20, $height + 20);
        
        // Colors
        $white = imagecolorallocate($img, 255, 255, 255);
        $black = imagecolorallocate($img, 0, 0, 0);
        
        // Fill background
        imagefilledrectangle($img, 0, 0, strlen($code) * 15 + 20, $height + 20, $white);
        
        // Draw bars
        $x = 10;
        for($i = 0; $i < strlen($code); $i++) {
            $number = substr($code, $i, 1);
            
            // Draw bar
            imagefilledrectangle($img, $x, 10, $x + 1, $height + 10, $black);
            
            // Add number
            imagestring($img, $fontSize, $x - 3, $height + 10, $number, $black);
            
            $x += 15;
        }
        
        // Start output buffering
        ob_start();
        imagepng($img);
        $barcodeImage = ob_get_clean();
        
        // Free memory
        imagedestroy($img);
        
        // Return base64 encoded image
        return base64_encode($barcodeImage);
    }
}