duanlao1552 2017-10-18 17:55
浏览 288
已采纳

如何使用storeas方法()在laravel中存储base64编码图像?

I am at the end of my wisdom trying to use the StoreAs method in Laravel to store an image that has not been attached by the html file handler and has been sent through request being of type "File".

I have created a cropped image using croppie: https://foliotek.github.io/Croppie/

However, croppie does not seem to provide me with an easy solution to attach the cropped image to the standard html filehandler. So I decided to attach the image information and the image name to a hidden file element and send it along with my post request to my laravel instance. However, my problem is now that I want to use the StoreAs method, because it allows me to attach my image to a specific url which I can retrieve which I find very useful, when retrieving a big amount of images.

The StoreAs() method in Laravel apparently only allows to use an object of type UploadedFile, which is based on the Symfony\Component\HttpFoundation\File\UploadedFile class. This UploadedFile object apparently is sent automatically along with the file when using the filehandler in html.

I have a base64 encoded image similar to this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4nOy9d3RUZ5ruuyvnnJNyzpEgcg4mO+e23U7t3Hl6wu2ZOTPn3Dtn8unTTTAYMGBjbDAYCZRBApRIkkpSqZ and an image name.

I found that I can create a UploadedFile object in Laravel myself but I struggle to understand how I have to create this UploadedFile and then use the StoreAs() method to link it to a generated url.

I found this link here: Laravel - file path to UploadedFile instance And this one here: https://gist.github.com/m241802/0b5d0a98318304ff08cf

But I still do not completely get how I can create an UploadedFile object out of the image I have and the image name.

Does anyone have an idea how to accomplish this? Or even can suggest a more elegant solution to my problem?? Perhaps someone has a better solution as to how I can send an File object in the post request after having used the croppie library?

Thanks in advance for any help!

  • 写回答

1条回答 默认 最新

  • doucai6663 2017-10-18 19:52
    关注

    Try this :

    $image = $request->input('image'); // your base64 encoded
    $image = str_replace('data:image/png;base64,', '', $image);
    $image = str_replace(' ', '+', $image);
    $imageName = 'test.png';
    \File::put(storage_path(). '/' . $imageName, base64_decode($image));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器