【问题标题】:Convert image to base 64 string with Laravel使用 Laravel 将图像转换为 base 64 字符串
【发布时间】:2017-09-13 21:18:33
【问题描述】:

我想使用 Laravel 将图像转换为 base 64。我从表单中获取图像。 我在我的控制器中试过这个:

public function newEvent(Request $request){
    $parametre =$request->all();

    if ($request->hasFile('image')) {
        if($request->file('image')->isValid()) {
            try {
                $file = $request->file('image');
                $image = base64_encode($file);
                echo $image;


            } catch (FileNotFoundException $e) {
                echo "catch";

            }
        }
    }

我只知道这个:

L3RtcC9waHBya0NqQlQ=

【问题讨论】:

  • $request->file() 不返回实际文件内容,而是返回UploadedFile 的一个实例。您需要加载实际文件进行转换。试试:$image = base64_encode(file_get_contents($request->file('image')->path()));
  • 请进行逆运算? base64转图片
  • base64_decode($image)?第一条评论对您有帮助吗?
  • 首先我认为这项工作 base64_encode(file_get_contents($request->file('image')));
  • 现在我想解码并保存,但这不起作用文件(base64_decode($image))->move("images", $name);

标签: php laravel base64 encode


【解决方案1】:

Laravel 的 $request->file() 不会返回实际的文件内容。它返回UploadedFile-class 的一个实例。

您需要加载实际文件才能对其进行转换:

$image = base64_encode(file_get_contents($request->file('image')->pat‌​h()));

【讨论】:

    【解决方案2】:

    它通过以下方式为我工作:

    $image = base64_encode(file_get_contents($request->file('image')));
    

    这部分我删了->pat‌​h();

    【讨论】:

      猜你喜欢
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 2011-07-20
      • 1970-01-01
      • 2018-01-22
      • 2013-07-09
      相关资源
      最近更新 更多