<?php
// inspect_qr.php
error_reporting(E_ALL);
ini_set('display_errors',1);

$id = isset($_GET['id']) ? intval($_GET['id']) : 9999;
$fn = __DIR__ . "/Uploads/qrcodes/book_{$id}.png";

header('Content-Type: text/plain; charset=utf-8');

echo "Inspecting file: $fn\n\n";

if (!file_exists($fn)) {
    echo "File not found\n";
    exit;
}

$size = filesize($fn);
echo "Size: {$size} bytes\n\n";

$fh = fopen($fn,'rb');
$start = fread($fh, 512);
fclose($fh);

// show first bytes hex
$hex = implode(' ', str_split(bin2hex(substr($start,0,16)),2));
echo "First 16 bytes (hex): $hex\n\n";

// show first 200 bytes as text (escaped)
$preview = substr($start,0,200);
echo "First 200 bytes (raw, escaped):\n";
$escaped = '';
for ($i=0;$i<strlen($preview);$i++){
    $c = $preview[$i];
    $ord = ord($c);
    $escaped .= ($ord >=32 && $ord <= 126) ? $c : sprintf("\\x%02x",$ord);
}
echo $escaped . "\n\n";

// mime type detection via finfo
if (function_exists('finfo_open')) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mt = finfo_file($finfo, $fn);
    finfo_close($finfo);
    echo "finfo mime-type: $mt\n";
} else {
    echo "finfo not available\n";
}

echo "\nIf you see something starting with 89 50 4e 47 or \\x89PNG then file is PNG.\n";
