dongpao1918 2019-07-06 19:35
浏览 112

使用文件名查找并替换多个base64图像

Before I start, I am using Laravel 5.7 for my project but any php solution would work. I am making a simple editor on my site where user can do simple editing along with adding images. I am using contenteditable div for this purpose. When I add images to the div it gets added as base64 image. After submitting the form I want to save those images as files on server side and then want to replace the src of those images with the path to newly save images in the string before saving it in database. I didn't find a way to submit contenteditable div so I am transfering all the text from div to a hidden textarea before submitting. correct me if there is any other reliable way of doing that. Here is what I am looking for:

Original sample content of textarea in request :-

"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"

Intended output :-

"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"

Can someone please point me in the right direction or provide me with the php code to achieve this?

  • 写回答

1条回答 默认 最新

  • dpno17028 2019-07-07 07:45
    关注

    I eventually solved my problem with following code. I am posting it here just in case someone else need it.

    $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;
    

    Above programe save all my images in tempStoryImages folder and return html to show all image files. I can now store this returned string into database with image path embeded into it.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)