<?php

// header('Content-Type: text/plain');
// header('Allow-Control-Allow-Origin: *');
// $json_data = file_get_contents("php://input");
// $data = json_decode($json_data, true);


// Include the QRcode library
include('phpqrcode/qrlib.php');

// Path to your CSV file
$csvFile = 'employee_users.csv';
$outputFolder = 'qr_codes/'; // Folder to save QR code images

// Check if output folder exists, create if not
if (!is_dir($outputFolder)) {
    mkdir($outputFolder, 0755, true);
}

// Open the CSV file for reading
if (($handle = fopen($csvFile, 'r')) !== false) {
    // Read the header
    $header = fgetcsv($handle);

    // Open a temporary CSV file to store the updated data
    $updatedCsvFile = fopen('updated_employee_users.csv', 'w');

    // Write the header to the new CSV file
    fputcsv($updatedCsvFile, $header);

    // Find the indexes for 'url' and 'qr' columns
    $urlIndex = array_search('url', $header);
    $qrIndex = array_search('qr', $header);

    // Loop through each row of the CSV
    while (($data = fgetcsv($handle, 1000, ',')) !== false) {
        $url = $data[$urlIndex]; // Get the URL from the 'url' column

        // Generate a unique filename for each QR code image
        $fileName = $outputFolder . uniqid('qrcode_') . '.png';

        // Create a temporary image
        $tempQR = tempnam(sys_get_temp_dir(), 'qr_') . '.png';

        // Error correction level and size for the QR code
        $errorCorrectionLevel = 'H';
        $matrixPointSize = 10;

        // Generate the QR code as a temporary image
        QRcode::png($url, $tempQR, $errorCorrectionLevel, $matrixPointSize);

        // Load the QR code image
        $qrImage = imagecreatefrompng($tempQR);

        // Create a new true color image with alpha transparency
        $transparentQR = imagecreatetruecolor(imagesx($qrImage), imagesy($qrImage));
        imagealphablending($transparentQR, false);
        imagesavealpha($transparentQR, true);
        $transparentColor = imagecolorallocatealpha($transparentQR, 0, 0, 0, 127); // Fully transparent background
        imagefill($transparentQR, 0, 0, $transparentColor);

        // Loop through pixels and replace white (255, 255, 255) with transparency
        for ($x = 0; $x < imagesx($qrImage); $x++) {
            for ($y = 0; $y < imagesy($qrImage); $y++) {
                $rgb = imagecolorat($qrImage, $x, $y);
                $colors = imagecolorsforindex($qrImage, $rgb);
                
                // If the pixel is white, make it transparent
                if ($colors['red'] == 255 && $colors['green'] == 255 && $colors['blue'] == 255) {
                    imagesetpixel($transparentQR, $x, $y, $transparentColor);
                } else {
                    imagesetpixel($transparentQR, $x, $y, $rgb);
                }
            }
        }

        // Save the new QR code with a transparent background
        imagepng($transparentQR, $fileName);

        // Free up memory
        imagedestroy($qrImage);
        imagedestroy($transparentQR);

        // Update the 'qr' column with the file path of the generated QR code
        $data[$qrIndex] = "https://www.jobaaj.com/emp/$fileName";

        // Write the updated data back into the new CSV file
        fputcsv($updatedCsvFile, $data);

        // Remove the temporary QR code image
        unlink($tempQR);
    }

    // Close the files
    fclose($handle);
    fclose($updatedCsvFile);
    
    echo "QR codes with transparent background generated and saved. Updated CSV file created as 'updated_employee_users.csv'.";
} else {
    echo "Error opening CSV file.";
}
?>
