download.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. $markdownDir = './'; // Markdown文件所在目录
  3. $imagesDir = './images'; // 图片下载目录
  4. // 确保图片下载目录存在
  5. if (!is_dir($imagesDir)) {
  6. mkdir($imagesDir, 0777, true);
  7. }
  8. // 使用cURL下载图片
  9. function downloadImage($url, $filepath)
  10. {
  11. $ch = curl_init($url);
  12. $fp = fopen($filepath, 'wb');
  13. curl_setopt($ch, CURLOPT_FILE, $fp);
  14. curl_setopt($ch, CURLOPT_HEADER, 0);
  15. curl_setopt($ch, CURLOPT_REFERER, 'https://ft07.com'); // 设置Referer头部
  16. curl_exec($ch);
  17. curl_close($ch);
  18. fclose($fp);
  19. echo "Downloaded: $filepath\n";
  20. }
  21. // 替换Markdown中的图片链接并下载图片
  22. function replaceImageLinksInFile($filePath, $imagesDir)
  23. {
  24. $data = file_get_contents($filePath);
  25. $regex = '/!\[.*?\]\((https:\/\/r2\.ft07\.com\/.*?\.(jpg|png|gif|bmp|webp))\)/';
  26. preg_match_all($regex, $data, $matches, PREG_SET_ORDER);
  27. foreach ($matches as $match) {
  28. $imageUrl = $match[1];
  29. $imageName = basename($imageUrl);
  30. $localImagePath = $imagesDir . '/' . $imageName;
  31. // 替换Markdown中的图片链接为本地路径
  32. $data = str_replace($imageUrl, $localImagePath, $data);
  33. // 下载图片
  34. downloadImage($imageUrl, $localImagePath);
  35. }
  36. // 保存更新后的Markdown文件
  37. file_put_contents($filePath, $data);
  38. echo "Updated file: $filePath\n";
  39. }
  40. // 读取并处理每个Markdown文件
  41. $files = scandir($markdownDir);
  42. foreach ($files as $file) {
  43. if (pathinfo($file, PATHINFO_EXTENSION) === 'md') {
  44. $filePath = $markdownDir . '/' . $file;
  45. replaceImageLinksInFile($filePath, $imagesDir);
  46. }
  47. }
  48. echo "Done.\n";