【问题标题】:Laravel - Convert a base64 string to a valid imageLaravel - 将 base64 字符串转换为有效图像
【发布时间】:2018-11-14 14:33:04
【问题描述】:

为了让您了解我想要做什么,这是我当前的代码

此代码在向特定 URL 发出 post 请求时运行

public function uploadImage(Request $request) {
    $request->file = base64_decode(explode(',', $request->file)[1]);

    $request->validate([
        'file' => 'image|required|mimes:jpg,png',
    ]);

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

    $complete = $image->getClientOriginalName();
    $name = pathinfo($complete, PATHINFO_FILENAME);
    $extension = $image->getClientOriginalExtension();
    $storageName = $name.'_'.time().'.'.$extension;

    Storage::disk('public')->put($storageName, File::get($image));

    return Storage::disk('public')->path($storageName);
}

在第一行,我试图变得聪明,首先将 base64 解码为一个文件(如果我是正确的?)。

接下来是验证请求中的file参数是否存在、是否为图片、是否为.jpg.png

(下一行只是将“图像”保存到文件系统)

但验证不会通过,因为file 参数不是图像。所以我的问题是:是否可以将 base64 字符串转换为 Laravel 中的有效图像?如果是,如何实现?

【问题讨论】:

标签: php laravel laravel-5


【解决方案1】:

Laravel 无法将 base 64 字符串解析为文件,因此文件验证器失败。您也无法通过->file('file') 访问您的数据,因为您再次发送的不是有效的文件上传。

相反,您可以进行自己的简单字符串检查(可能是data:image 的索引或更广泛的索引,例如imagecreatefromstring)或查看writing a custom Laravel validation rule,然后将发送的字符串保存到带有@987654323 的文件中@。

如果您确实想要上传文件并在服务器端将其转换为 base 64,那么您需要使用<input type="file">。从那里,您将能够使用内置的 Laravel 验证和文件存储功能。 Refer to this extensive guide. 获得文件后,您可以使用 base64_encode (see this answer for a more thorough guide) 将其转换为 base64。

我意识到这个答案不是很具体,但如果你明确说明你想要做什么,我可以提供更多的实践指导。

【讨论】:

    【解决方案2】:

    你可以有效的图像base64
    请跟着我一步一步来:

    1 .转到文件夹 yourProjectName\app\Providers\AppServiceProvider.php
    2.在函数 boot()

    处复制此代码并粘贴到此文件中 Validator::extend('is_image', function ($attribute, $value, $parameters, $validator) { preg_match_all('/([^\.]+)\.([a-zA-Z]+)/',$value,$matchedExt); if (isset($matchedExt[2][0]) && in_array($matchedExt[2][0],$parameters)) 返回真; preg_match_all('/data\:image\/([a-zA-Z]+)\;base64/',$value,$matched); $ext = isset($matched[1][0]) ? $matched[1][0]:假; print_r($值); 返回 in_array($ext,$parameters) ?真假; });
    1. 转到您的函数 uploadImage() 并通过此代码
    公共函数uploadImage(请求$request){ $请求->验证([ '文件' => '图像|必需|is_image:jpg,png', ]); $image = $request->file('file'); $complete = $image->getClientOriginalName(); $name = pathinfo($complete, PATHINFO_FILENAME); $extension = $image->getClientOriginalExtension(); $storageName = $name.'_'.time().'.'.$extension; Storage::disk('public')->put($storageName, File::get($image)); return Storage::disk('public')->path($storageName); }

    希望我能为你解决问题

    【讨论】:

      【解决方案3】:

      我正在使用此代码将 base64 字符串转换为图像:

          public function getImage($img, $pid)
      {
          if($img == "")
              return null;
          $imagecode = base64_decode($img);
      
          $directory = public_path('img/uploads/x' . $pid);
          if (!file_exists($directory))
              \File::makeDirectory($directory);
          $id = uniqid('img_');
          $filename = time() . '-' . $id . '.png';
          $path = public_path('img/uploads/product_img/p' . $pid . '/' . $filename);
          $tpath = public_path('img/uploads/product_img/p' . $pid . '/small-' . $filename);
      
          try {
              $image = Image::make($imagecode)->widen(600, function ($constraint) {
                  $constraint->upsize();
              })->save($path);
              $image->fit(200, 200)->save($tpath);
          } catch (Exception $e) {
              return null;
          }
      
          return "x" . $pid . "/" . $filename;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-07
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        • 2014-11-05
        • 2011-06-17
        相关资源
        最近更新 更多