class SitemapImageBuilder
{
public function __construct(string $imagesDir)
{
$this->dir = $imagesDir;
}
public function run()
{
$imageFiles = $this->getImageFiles($this->dir);
$this->generateXml($imageFiles);
}
public function getImageFiles(string $path)
{
$ret = [];
$files = scandir($path);
$explodeNames = [
'.',
'..',
];
$mimeRe = "/^image/";
foreach ($files as $name) {
if (in_array($name, $explodeNames)) {
continue;
}
$filePath = $path .'/'. $name;
if (is_dir($filePath)) {
$childs = $this->getImageFiles($filePath);
if ($childs) {
array_push($ret, ...$childs);
}
}
else if (is_file($filePath)) {
$mime = mime_content_type($name);
if (preg_match($mimeRe, $mime)) {
$ret[] = $filePath;
}
}
}
return $ret;
}
public function generateXml(array $pathes)
{
// генерация XML sitemap
}
}
$imagesDir = 'path/to/static';
(new SitemapImageBuilder($imagesDir))->run();