$folder . '/' . $f, 'type' => $type, 'name' => $f ]; } } usort($media, function($a,$b){ return strcmp($a['name'],$b['name']); }); $totalMedia = count($media); $videoIndices = []; $imageIndices = []; foreach ($media as $i => $m) { if ($m['type'] === 'video') $videoIndices[] = $i; else $imageIndices[] = $i; } // Prefer 8K video as hero if file name contains "8k", else first video $heroVideoIndex = null; if (count($videoIndices)) { $heroVideoIndex = $videoIndices[0]; foreach ($videoIndices as $idx) { $name = strtolower($media[$idx]['name']); if (strpos($name, '8k') !== false) { $heroVideoIndex = $idx; break; } elseif (strpos($name, '4k') !== false && $heroVideoIndex === $videoIndices[0]) { // fallback if later we find a 4k before any 8k $heroVideoIndex = $idx; } } } $sideTileCount = min(16, count($imageIndices)); ?>