移除图片替换相关内容
This commit is contained in:
@@ -1,34 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
function downloadImage($url, $saveDir) {
|
|
||||||
// 获取图片内容
|
|
||||||
$imageContent = file_get_contents($url);
|
|
||||||
if ($imageContent === FALSE) {
|
|
||||||
return $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成唯一的文件名
|
|
||||||
$imageName = uniqid() . basename($url);
|
|
||||||
$savePath = rtrim($saveDir, '/') . '/' . $imageName;
|
|
||||||
|
|
||||||
// 保存图片到本地
|
|
||||||
if (file_put_contents($savePath, $imageContent) === FALSE) {
|
|
||||||
return $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回保存路径
|
|
||||||
return $savePath;
|
|
||||||
}
|
|
||||||
function MarkdownEntities($markdown) {
|
function MarkdownEntities($markdown) {
|
||||||
// 对markdown中的内容进行转义
|
// 对markdown中的内容进行转义
|
||||||
$markdown = preg_replace_callback('/\$\$(.*?)\$\$/s', function ($matches) {
|
$markdown = preg_replace_callback('/\$\$(.*?)\$\$/s', function ($matches) {
|
||||||
return str_replace(['<', '>'], ['\\lt', '\\gt'], $matches[0]);
|
return str_replace(['<', '>'], ['\\lt', '\\gt'], $matches[0]);
|
||||||
}, $markdown);
|
}, $markdown);
|
||||||
// 对图片内容存储到本机并替换
|
|
||||||
$saveDir = "/home/judge/src/web/upload/markdown";
|
|
||||||
$markdown = preg_replace_callback('/!\[.*?\]\((.*?)\)/', function ($matches) use ($saveDir) {
|
|
||||||
$imageUrl = $matches[1];
|
|
||||||
$savedPath = downloadImage($imageUrl, $saveDir);
|
|
||||||
return str_replace($imageUrl, $savedPath, $matches[0]);
|
|
||||||
}, $markdown);
|
|
||||||
return $markdown;
|
return $markdown;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user