亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHP vanilla 或 Laravel 從 Summernote 保存和更新圖像

如何使用 PHP vanilla 或 Laravel 從 Summernote 保存和更新圖像

PHP
拉莫斯之舞 2023-07-30 11:14:31
如何使用 PHP 保存和更新 Summernote 圖像。Summernote圖像是base64的,因此需要解碼、上傳,然后將summnernote內容的圖像src數據修改為圖像上傳的目錄,然后將summnernote內容保存到DB。因為不建議保存圖像信息數據庫。當我將上傳圖像的相同路徑目錄img/位置保存在數據庫中時,當我嘗試從數據庫編輯summernote內容時,summernote無法顯示圖像<textarea>{{$dbData->content}}</textarea>另外,在解碼之前,您需要檢查img src是否真的是base64圖像,因為在編輯已保存的內容時,現有的img src不會是base64圖像,除了尚未上傳的新圖像,因為現有的img src已經解碼并上傳。
查看完整描述

2 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

$content = request()->get('content');


        $dom = new \DomDocument();


        $dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);


        $images = $dom->getElementsByTagName('img');


        foreach ($images as $image) {

            $imageSrc = $image->getAttribute('src');

           

            if (preg_match('/data:image/', $imageSrc)) {

                

                preg_match('/data:image\/(?<mime>.*?)\;/', $imageSrc, $mime);

                $mimeType = $mime['mime'];

                

                $filename = uniqid();


                

                $filePath = "/uploads/$filename.$mimeType";


                

                Image::make($imageSrc)

                    

                    ->encode($mimeType, 100)

                    

                    ->save(public_path($filePath));


                $newImageSrc = asset($filePath);

                $image->removeAttribute('src');

                $image->setAttribute('src', $newImageSrc);

            }

        }

        $newMessageBody = $dom->saveHTML();


查看完整回答
反對 回復 2023-07-30
?
holdtom

TA貢獻1805條經驗 獲得超10個贊

  1. 為了避免錯誤的 HTML 格式引發錯誤,請使用LIBXML_NOWARNING | LIBXML_NOERROR

  2. 檢測img src中的 base64 圖像的一個簡單方法是檢測字符串中的data:image 。雖然不是最佳方式,但可以毫無問題地解決。

  3. 最后,在summernote內容中修改img src前面的正斜杠“ / ”,即可解決從DB獲取結果后summer note內容中圖像不顯示的問題

如果您有任何建議和最佳方法,請發表評論,我將很高興。謝謝。 代碼 (我的解決方案)

   //checking if request  is set

       if (isset($request->long_description)) {

        

        $detail=$request->long_description;

        

        //Prepare HTML & ignore HTML errors

        $dom = new \domdocument();

        $dom->loadHtml($detail, LIBXML_NOWARNING | LIBXML_NOERROR);


        //identify img element

        $images = $dom->getelementsbytagname('img');


         //loop over img elements, decode their base64 source data (src) and save them to folder,

        //and then replace base64 src with stored image URL.

        foreach($images as $k => $img){


            //collect img source data

            $data = $img->getattribute('src');


            //checking if img source data is image by detecting 'data:image' in string

            if (strpos($data, 'data:image')!==false){

            list($type, $data) = explode(';', $data);

            list(, $data)      = explode(',', $data);

            

            //decode base64

            $data = base64_decode($data);


            //naming image file

            $image_name= time().rand(000,999).$k.'.png';


            // image path (path) to use upload file to

            $path = 'img/location/'. $image_name;


            //image path (path2) to save to DB so that summernote can display image in edit mode (When editing summernote content) NB: the difference btwn path and path2 is the forward slash "/" in path2

            $path2 = '/img/location/'. $image_name;


            file_put_contents($path, $data);


            modify image source data in summernote content before upload to DB

            $img->removeattribute('src');

            $img->setattribute('src', $path2);

}


     else {

        // not base64 img


}

            

        }

        // final variable to store in DB

        $detail = $dom->savehtml();

       }


查看完整回答
反對 回復 2023-07-30
  • 2 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號