<?php
require 'parseJpeg.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Simple JPEG segment parser</title>
    <style>
        pre {
            max-height: 80vh;
            max-width: max-content;
            overflow: auto;
            border: 1px solid blue;
            padding: 1rem 5rem 1rem 1rem;
        }
    </style>
</head>
<body>
<h1>Simple JPEG segment parser</h1>
<form method="post" enctype="multipart/form-data">
    <div>
        <label>
            Image:
            <input type="file" name="image" accept="image/jpeg">
        </label>
    </div>
    <div>
        <button type="submit">Parse</button>
    </div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
    if ((
$_FILES['image']['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_OK){
        echo 
'<h2>' htmlspecialchars($_FILES['image']['name']) . '</h2>';
        foreach (
parseJpeg($_FILES['image']['tmp_name']) as $section){
            
printf('<p>Found marker %02X @ 0x%08X</p>'$section['marker'], $section['offset']);
            if (isset(
$section['segmentData'])){
                
printf('<p>Segment is %d bytes long</p>'strlen($section['segmentData']));
                echo 
'<pre>'htmlspecialchars(hexdump($section['segmentData'], $section['offset'] + 4)), '</pre>';
            }
            if (isset(
$section['imageData'])){
                
printf('<p>Image data is %d bytes long</p>'strlen($section['imageData']));
                echo 
'<pre>'htmlspecialchars(hexdump($section['imageData'], $section['offset'] + strlen($section['segmentData']))), '</pre>';
            }
            if (isset(
$section['headerData'])){
                
printf('<p>Format Identifier: %s</p>'$section['headerData']['id']);
                
printf('<p>Version: %s</p>'$section['headerData']['version']);
                
printf('<p>Units: %s</p>'$section['headerData']['units']);
                
printf('<p>Density: %s</p>'$section['headerData']['density']);
                
printf('<p>Thumbnail: %s</p>'$section['headerData']['thumbnail']);
            }
            if (isset(
$section['iccData'])){
                
printf('<p>ICC header fields</p><ul>');
                foreach (
$section['iccData'] as $field => $value){
                    
printf('<li>%s: %s'$fieldprint_r($valuetrue));
                }
                
printf('</ul>');
            }
        }
    } else {
        echo 
"Image upload failed, unable to parse.";
    }
}
?>
<p>[<a href="./?source=parseJpeg.php">view source</a>]</p>
</body>
</html>