Somewhat simple PHP script to read and extract information from images to a json. Fetches dimensions, EXIF taken date, adds tags based on folder structure. Built from arguing with Grok… refined, fixed, and mastered by Claude Code. Like Lemon said, “Grok stupid bro”.
If I could offer you only one tip for the future, choose life.
Choose Claude.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php $images = []; $fullDir = __DIR__ . '/full'; $sourceDir = __DIR__ . '/original'; $files = glob($fullDir . '/*_full.webp'); foreach ($files as $file) { $filename = basename($file); // Extract day number from filename (e.g. day-03 or Day25) preg_match('/day[-_]?(\d+)/i', $filename, $m); $day = str_pad($m[1] ?? '00', 2, '0', STR_PAD_LEFT); // Base name without the _full.webp suffix $base = str_replace('_full.webp', '', $filename); // Day-level source folder (unpadded int to match folder names like /original/3/) $daySourceDir = $sourceDir . '/' . (int)$day; // Detect category tags and find original filename in one pass $tags = []; $originalExt = 'jpg'; // fallback $foundOriginal = false; $originalPath = null; $originalCategory = null; foreach (['promo', 'off', 'bts'] as $category) { $dir = $daySourceDir . '/' . $category . '/'; $matches = glob($dir . $base . '.*'); // matches any extension, Windows-safe if (!empty($matches)) { $tags[] = $category; if (!$foundOriginal) { $originalExt = strtolower(pathinfo($matches[0], PATHINFO_EXTENSION)); $originalPath = $matches[0]; $originalCategory = $category; $foundOriginal = true; } } } // Read dimensions from the full webp $dims = @getimagesize($file); $width = $dims ? $dims[0] : 3008; $height = $dims ? $dims[1] : 2000; // Read EXIF date from the original JPG, fall back to filename $dateTaken = null; if ($originalPath && $originalExt === 'jpg') { $exif = @exif_read_data($originalPath); $raw = $exif['DateTimeOriginal'] ?? $exif['DateTime'] ?? null; if ($raw) { // EXIF format is "YYYY:MM:DD HH:MM:SS" — fix separators then parse $dateTaken = date('Y-m-d', strtotime(str_replace(':', '-', substr($raw, 0, 10)))); } } // Fallback: parse date from filename e.g. killing-heat_day-2_2008-06-30_... if (!$dateTaken) { preg_match('/(\d{4}-\d{2}-\d{2})/', $base, $dm); $dateTaken = $dm[1] ?? null; } $images[] = [ "filename" => $filename, "day" => $day, "width" => $width, "height" => $height, "dateTaken" => $dateTaken, "tags" => $tags, "featured" => false, "hide" => false, "caption" => "", "originalFilename" => $base . '.' . $originalExt, "originalPath" => $foundOriginal ? 'original/' . (int)$day . '/' . $originalCategory . '/' . $base . '.' . $originalExt : null, ]; } // Sort by day (ascending), then alphabetically by filename within each day usort($images, function ($a, $b) { if ($a['day'] !== $b['day']) return (int)$a['day'] - (int)$b['day']; return strcmp($a['filename'], $b['filename']); }); file_put_contents('images.json', json_encode($images, JSON_PRETTY_PRINT)); echo "✅ Done! " . count($images) . " images written to images.json\n"; echo "Tags (#promo, #off, #bts) detected from source subfolders.\n"; echo "Sorted by day (Day 1 first), then by filename.\n"; |
Script used in process to make gallery function for Killing Heat website.
