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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46816339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:49:37  来源:igfitidea点击:

How do I store a base64 encoded image inside of laravel using the storeas method()?

phpimagelaravelfilecroppie

提问by Benny

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

我在尝试使用 Laravel 中的 StoreAs 方法来存储尚未由 html 文件处理程序附加并且已通过“文件”类型的请求发送的图像时,我的智慧已经尽头。

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

我使用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.

但是,croppie 似乎并没有为我提供一个简单的解决方案来将裁剪的图像附加到标准的 html 文件处理程序。所以我决定将图像信息和图像名称附加到一个隐藏的文件元素,并将它与我的 post 请求一起发送到我的 laravel 实例。但是,我现在的问题是我想使用 StoreAs 方法,因为它允许我将我的图像附加到我可以检索的特定 url,在检索大量图像时,我觉得这非常有用。

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.

Laravel 中的 StoreAs() 方法显然只允许使用 UploadedFile 类型的对象,该对象基于 Symfony\Component\HttpFoundation\File\UploadedFile 类。当在 html 中使用文件处理程序时,这个 UploadedFile 对象显然是与文件一起自动发送的。

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

我有一个与此类似的 base64 编码图像: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4nOy9d3RUZ5ruuyvnnJNyzpEgcg4mO+e23U7t3Hl63TTAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4nOy9d3RUZ5ruuyvnnJNyzpEgcg4mO+e23U7t3Hl63TTAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAgAElEQVR4nOy9d3RUZ5ruuyvnnJNyzpEgcg4mO+e23U7t3Hl63TTAANSUhEUgAAASwAAAEsCAYAAAB5FY51AGA

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.

我发现我可以自己在 Laravel 中创建一个 UploadedFile 对象,但我很难理解我必须如何创建这个 UploadedFile,然后使用 StoreAs() 方法将它链接到生成的 url。

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

我在这里找到了这个链接:Laravel - UploadedFile 实例的文件路径而这个在这里: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.

但是我仍然不完全了解如何从我拥有的图像和图像名称中创建一个 UploadedFile 对象。

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?

有谁知道如何做到这一点?或者甚至可以为我的问题提出更优雅的解决方案?也许有人有更好的解决方案来说明在使用了croppie 库后如何在post 请求中发送一个File 对象?

Thanks in advance for any help!

在此先感谢您的帮助!

回答by Saeed Vaziry

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