猿问

Laravel 转换 base64 并以 php 形式保存

我正在使用 Croppie 脚本在上传到我的服务器之前编辑图像。


https://foliotek.github.io/Croppie/


问题是 Croppie 以 base64 格式对裁剪后的图像进行编码。我创建了一个隐藏输入并附加了 base64 字符串。我需要知道如何获取字符串,以便我可以对其进行解码并将其与我的表单一起发布。


我尝试了很多方法,但我被卡住了。任何帮助将不胜感激


控制器


$post = new Post;


        $post->post_category_id = $request->post_category_id;

        $post->title            = $request->title;

        $post->body             = $request->body;

        $post->meta_description = $request->meta_description;

        $post->slug             = $request->slug;

        $post->user_id          = auth()->id();


 $image = $request->input('featimage'); // image base64 encoded

 preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension

 $image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part

 $image = str_replace(' ', '+', $image);

 $imageName = 'image_' . time() . '.' . $image_extension[1]; //generating unique file name;

 \Storage::disk('public')->put($imageName,base64_decode($image)



        $post->save();

}


四季花海
浏览 378回答 1
1回答

翻阅古今

问题似乎出在您的 jquery 脚本中,您将 base64 字符串设置为&nbsp;<input type="hidden" id="featimage">改变这个:$('#featimage').attr('src',&nbsp;resp);对此`$('#featimage').val(resp);`
随时随地看视频慕课网APP
我要回答