2017-06-01 25 views
1

我在laravel 5.4 ...Laravel interventation图像突破图像保存方法

我需要上传一些图片,让缩略图为每一个图像并保存缩略图另一个名字。

嗯,上传工作正常,但当我尝试调整图像大小并使用intervention包缩略图时,保存的缩略图似乎被打破。

请看看这张图片: show images

这是我的代码:

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

我会得到一个错误说:

不能图像数据写入路径(/存储/ ajxpmXZF2rPfyOu39CcdEgC7Gpi5AlGviysrug88/872130_1496335864_jpg)

请你帮我找到我的问题......?

由于提前

回答

0

相应地改变图像的存储路径。如果图片上传失败,请优雅处理,而不是在这里例外。

$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}"));