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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要