<?php
$markdownDir = './';
$imagesDir = 'images';
if (!is_dir($imagesDir)) {
mkdir($imagesDir, 0777, true);
}
function downloadImage($url, $filepath)
{
$ch = curl_init($url);
$fp = fopen($filepath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://ft07.com');
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "Downloaded: $filepath\n";
}
function replaceImageLinksInFile($filePath, $imagesDir)
{
$data = file_get_contents($filePath);
$regex = '/!\[.*?\]\((https:\/\/r2\.ft07\.com\/.*?\.(jpg|png|gif|bmp|webp))\)/';
preg_match_all($regex, $data, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$imageUrl = $match[1];
$imageName = basename($imageUrl);
$localImagePath = $imagesDir . '/' . $imageName;
$data = str_replace($imageUrl, $localImagePath, $data);
downloadImage($imageUrl, $localImagePath);
}
file_put_contents($filePath, $data);
echo "Updated file: $filePath\n";
}
$files = scandir($markdownDir);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'md') {
$filePath = $markdownDir . '/' . $file;
replaceImageLinksInFile($filePath, $imagesDir);
}
}
echo "Done.\n";