【问题标题】:Laravel malformed UTF-8 characters, possibly incorrectly encoded using image interventionLaravel 格式错误的 UTF-8 字符,可能使用图像干预错误编码
【发布时间】:2020-08-31 06:11:42
【问题描述】:

我有一个上传图片的 laravel 项目。我使用图像干预库进行上传。问题是我收到500 错误并说Malformed UTF-8 characters, possibly incorrectly encoded。但是当我查看要保存的目录时,图像已保存但数据未保存到数据库中。只保存了图像。似乎图像保存成功,但请求未成功。似乎是什么问题?

控制器

 public function store(Request $request)
    {
        $product = new Product;
        $validator = \Validator::make($request->all(), [
            'product_name' => 'required',
            'barcode' => 'required|unique:products',
            'price'=> 'required',
            'category' => 'required',
            'supplier' => 'required',
            // 'image' => 'required|image64:jpeg,jpg,png'
        ]);

        if ($validator->fails()) {
            $errors = json_encode($validator->errors());
            return response()->json([
                'success' => false,
                'message' => $errors
            ],422);
        } else {
            $product->barcode = $request->barcode;
            $product->product_name = $request->product_name;
            $product->price = $request->price;
            $product->quantity = 0;
            $product->category = $request->category;
            $product->supplier_id = $request->supplier;

            //image
            $imageData = $request->image;
            $fileName = time().'.'. explode('/', explode(':', substr($imageData, 0, strpos($imageData, ';'))) [1])[1];
            $product->image = \Image::make($request->image)->save(public_path('img/').$fileName);

            $product->save();

            broadcast(new ProductsEvent(\Auth::user()->name, 'add', $product))->toOthers();
        }
    }

表单改变时的Vue组件事件

  onFileChange(e) {
                let file = e.target.files[0];
                console.log(file);
                var reader = new FileReader();
                reader.onloadend = (file)=>{this.image = reader.result}
                reader.readAsDataURL(file);
            },        

【问题讨论】:

    标签: php laravel image file-upload


    【解决方案1】:

    您的$filename 一代似乎出现了问题。

    只要您保存了正确的图像,命名约定就在您手中。

    我建议您使用更简单的方法,例如 $fileName = now()->timestamp . '_' . $imageData->name; 并且您无需花哨的文件名。

    $imageData 的值无法预测,您执行的所有操作都可能导致该问题。

    问题已经被问到Laravel "Malformed UTF-8 characters, possibly incorrectly encoded", how to fix?

    --- 编辑---

    您可以直接从您的 javascript 中获取文件名,因为您可以在最后进行所有操作,例如,您可以将 this.name = file.name; 添加到您发送的数据中,然后在您的 ajax 中您可以像这样发送该数据 -

    axios.post('/image/store',{
        image: this.image,
        imageName: this.name
    }).then(response => {...});
    

    在您的后端$fileName = now()->timestamp . '_' . $request->imageName;

    【讨论】:

    • 我不能用这样的$fileName = now()->timestamp . '_' . $imageData->name; 写图像,因为它是从我的前端用base64 编码的
    • 那么在你的js中你可以发送请求的文件名,或者只是将它保存为时间戳,因为它将是唯一的$fileName = now()->timestamp . '.jpg'(扩展可以通过正则表达式获取,或者仍然通过js)
    【解决方案2】:

    问题是这一行,我将实际的图像对象保存在数据库中。

     $product->image = \Image::make($request->image)->save(public_path('img/').$fileName);
    

    我改成

    $imageData = $request->image;
    $fileName = time().'.'. explode('/', explode(':', substr($imageData, 0, strpos($imageData, ';'))) [1])[1];
    
    \Image::make($request->image)->save(public_path('img/').$fileName);
    
    $product->image = $fileName;
    $product->save();;
    

    【讨论】:

      猜你喜欢
      • 2019-06-30
      • 2017-11-27
      • 2018-05-24
      • 2017-02-17
      • 2016-11-13
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多