isFile()) { continue; } $ext = strtolower($file->getExtension()); if (!in_array($ext, $extensions, true)) { continue; } $basename = pathinfo($file->getFilename(), PATHINFO_FILENAME); $key = normalize($basename); if (!isset($files[$key])) { $files[$key] = []; } $files[$key][] = $file->getPathname(); } $copied = 0; $missing = 0; $skipped = 0; foreach ($channels as $id => $channel) { if (hasExistingLogo($targetDir, $id, $extensions)) { $skipped++; continue; } $names = candidateNames($id, $channel); $match = null; foreach ($names as $name) { $key = normalize($name); if (isset($files[$key])) { $match = pickBestFile($files[$key]); break; } } if (!$match) { $missing++; echo "missing: {$id} | " . ($channel['name'] ?? '') . PHP_EOL; continue; } $ext = strtolower(pathinfo($match, PATHINFO_EXTENSION)); $target = $targetDir . '/' . $id . '.' . $ext; copy($match, $target); $copied++; echo "copied: {$id} <- {$match}" . PHP_EOL; } echo PHP_EOL; echo "copied: {$copied}" . PHP_EOL; echo "skipped: {$skipped}" . PHP_EOL; echo "missing: {$missing}" . PHP_EOL; function candidateNames(string $id, array $channel): array { $name = $channel['name'] ?? $id; $baseId = preg_replace('/\.(at|de|ch)$/i', '', $id); return array_unique(array_filter([ $id, $baseId, $name, str_replace(['+', '&'], ['plus', 'and'], $name), str_replace([' HD', ' SD', ' UHD'], '', $name), str_replace([' eins'], [' 1'], $name), str_replace(['III'], ['3'], $name), str_replace(['II'], ['2'], $name), ])); } function normalize(string $value): string { $value = strtolower($value); $value = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $value); $value = preg_replace('/\.(at|de|ch)$/', '', $value); $value = preg_replace('/[^a-z0-9]+/', '', $value); return trim($value); } function hasExistingLogo(string $dir, string $id, array $extensions): bool { foreach ($extensions as $ext) { if (file_exists($dir . '/' . $id . '.' . $ext)) { return true; } } return false; } function pickBestFile(array $files): string { usort($files, function ($a, $b) { $priority = ['svg' => 0, 'png' => 1, 'webp' => 2, 'jpg' => 3, 'jpeg' => 4]; $ea = strtolower(pathinfo($a, PATHINFO_EXTENSION)); $eb = strtolower(pathinfo($b, PATHINFO_EXTENSION)); return ($priority[$ea] ?? 99) <=> ($priority[$eb] ?? 99); }); return $files[0]; }