<?php
/**
 * CSV → QR Code Generator
 * Saves QR image as last numeric segment in the URL (e.g., 315252001.png)
 */

require 'phpqrcode.php';

$csvFile = 'urls.csv';
$outputDir = 'qrcodes';

// Create directory if not exists
if (!is_dir($outputDir)) {
    mkdir($outputDir, 0777, true);
}

$rows = array_map('str_getcsv', file($csvFile));

foreach ($rows as $row) {
    if (!isset($row[0]) || trim($row[0]) === '') {
        continue;
    }

    $url = trim($row[0]);

    // Extract last numeric ID from URL
    if (preg_match('/(\d+)\s*$/', $url, $matches)) {
        $id = $matches[1];
    } else {
        echo "Skipping URL, no numeric ID found: $url\n";
        continue;
    }

    $fileName = $outputDir . '/' . $id . '.png';

    // Generate QR code
    QRcode::png($url, $fileName, QR_ECLEVEL_L, 6, 2);

    echo "Generated: $fileName\n";
}

echo "\nDone.\n";
