Php 查找并用文件名替换多个base64映像

Php 查找并用文件名替换多个base64映像,php,laravel,image,base64,Php,Laravel,Image,Base64,在我开始之前,我正在为我的项目使用Laravel5.7,但任何php解决方案都可以。 我正在我的网站上制作一个简单的编辑器,用户可以在添加图像的同时进行简单的编辑。为此,我正在使用contenteditable div。当我将图像添加到div时,它被添加为base64图像。提交表单后,我希望将这些图像保存为服务器端的文件,然后在将其保存到数据库之前,将这些图像的src替换为字符串中新保存图像的路径。我没有找到提交contenteditable div的方法,所以我在提交之前将所有文本从div传输

在我开始之前,我正在为我的项目使用Laravel5.7,但任何php解决方案都可以。 我正在我的网站上制作一个简单的编辑器,用户可以在添加图像的同时进行简单的编辑。为此,我正在使用contenteditable div。当我将图像添加到div时,它被添加为base64图像。提交表单后,我希望将这些图像保存为服务器端的文件,然后在将其保存到数据库之前,将这些图像的src替换为字符串中新保存图像的路径。我没有找到提交contenteditable div的方法,所以我在提交之前将所有文本从div传输到一个隐藏的文本区域。如果有其他可靠的方法,请纠正我。 以下是我想要的:

请求中textarea的原始样本内容:-

"This is a test post with some images like this one 
<img src="data:image/jpeg;base64,/9j.....long base 64 string...." id="img02">
and it can contain more than one images like here is the second image
<img src="data:image/png;base64,/8Aue.....long base 64 string...." id="img01">
and more images like this"
"This is a test post with some images like this one  
<img src="my_public_path/Unique_file_name.jpg" id="img02"> 
and it can contain more than one images like here is the second image 
<img src="my_public_path/Unique_file_name.png" id="img01"> 
and more images like this"
“这是一个测试帖子,有一些像这样的图片
它可以包含多个图像,比如第二个图像
还有更多像这样的图片”
预期输出:-

"This is a test post with some images like this one 
<img src="data:image/jpeg;base64,/9j.....long base 64 string...." id="img02">
and it can contain more than one images like here is the second image
<img src="data:image/png;base64,/8Aue.....long base 64 string...." id="img01">
and more images like this"
"This is a test post with some images like this one  
<img src="my_public_path/Unique_file_name.jpg" id="img02"> 
and it can contain more than one images like here is the second image 
<img src="my_public_path/Unique_file_name.png" id="img01"> 
and more images like this"
“这是一个测试帖子,有一些像这样的图片
它可以包含多个图像,比如第二个图像
还有更多像这样的图片”

有人能告诉我正确的方向吗?或者给我提供php代码来实现这一点吗?

我最终用下面的代码解决了我的问题。我把它贴在这里,以防别人需要它

$stt = $request->storytextTemp; // string containing base64 encoded image files.
preg_match('#data:image/(gif|png|jpeg);base64,([\w=+/]++)#', $stt, $x);
while(isset($x[0]))
{
    $imgdata = base64_decode($x[0]);
    $info = explode(";", explode("/", $x[0])[1])[0];
    $textareaImages = $request->imagesTextarea;

    $imm = Image::make(file_get_contents($x[0]));
    $imp = public_path().'/images/tempStoryImages/';
    $imn = date('y_m_d') . "_" . uniqid().".".$info;
    $imm->save($imp.$imn);

    $stt = str_replace($x[0], 'http://127.0.0.1:8000/images/tempStoryImages/'.$imn, $stt);
    preg_match('#data:image/(gif|png|jpeg);base64,([\w=+/]++)#', $stt, $x);
}
return $stt;

以上程序将所有我的图像保存在tempStoryImages文件夹中,并返回html以显示所有图像文件。现在,我可以将返回的字符串存储到数据库中,并将图像路径嵌入其中。

您使用什么编辑器添加图像?很多编辑器都有一种内置的方式来在amazon s3、digital ocean spaces或您自己的服务器上存储图像。我正在制作自己的编辑器,我希望保持这种方式。明白了。因此,您需要决定是动态存储图像,还是在保存所有内容时存储图像。例如,使用AmazonS3及其sdk,您可以上传图像,然后在回调中获取完整的URL。然后您可以使用javascript按id获取元素并替换为新字符串。我希望在最后存储所有内容,因为在用户最终存储帖子之前可能会有很多编辑。在用户决定存储之前,我希望将图像保持为base64格式。当时我想将base64图像转换为文件,存储它们并用保存的图像路径替换src,我还尝试将文本中使用的每个图像作为base64使用带有fakepath的隐藏文件输入,并在请求中传递这些输入,我可以成功地在服务器端保存这些文件,但问题是如何用保存的图像的路径替换base64图像的src。我对这两个文件都使用类似的id,例如,对于id为“image01”的相应base64图像,文件输入id为“img01”。如果有帮助的话。