【问题标题】:Laravel interventation image break images on save methodLaravel 干预图像在保存方法上中断图像
【发布时间】:2017-06-02 02:05:35
【问题描述】:

我在 laravel 5.4...

我需要上传一些图片并为每张图片制作缩略图并以另一个名称保存缩略图。

好吧,上传工作正常,但是当我尝试使用 intervention 包调整图像大小并制作缩略图时,保存的缩略图似乎已损坏。

请看这张图:

这是我的代码:

    $image = $request->file('ax');
    $imagename = mt_rand(999, 999999) . "_" . time() . "_" . $image->getClientOriginalExtension();

    $img = Image::make($image->getRealPath());
    $width = $img->width();
    $height = $img->height();

    $img->resize(200, null, function ($constraint) {
            $constraint->aspectRatio();
        })->save(public_path('storage/' . $token) . '/' . $imagename);

如果我将storage path 更改为:

Storage::url($token)

我会得到一个错误说:

无法将图像数据写入路径 (/storage/ajxpmXZF2rPfyOu39CcdEgC7Gpi5AlGviysrug88/872130_1496335864_jpg)

您能帮我找出我的问题吗...?

提前致谢

【问题讨论】:

    标签: php image laravel-5 intervention


    【解决方案1】:

    相应地更改图像的存储路径。如果图片上传失败,请从容处理,而不是这里的异常。

        $image = $request->file('ax');
    
        if (!$image->isValid()) {
            throw new \Exception('Failed to upload image.');
        }
    
        $imagename = mt_rand(999, 999999) . "_" . time() . "_" . $image->getClientOriginalExtension();
    
        $img = Image::make($image);
    
        $img->resize(200, null, function ($constraint) {
            $constraint->aspectRatio();
        });
    
        $img->save(public_path("storage/{$token}/{$imagename}"));
    

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2017-01-28
      • 1970-01-01
      相关资源
      最近更新 更多