在Laravel 5.0中创建图像缩略图

5

如何创建图像缩略图?我使用了不同的库,但是缩略图没有被创建。

 public function store(Request $request){
     $user_id = \Auth::user()->id;
    $image = new Image();
    $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'image' => 'required'
    ]);
    $image->title = $request->title;
    $image->description = $request->description;
    $image->user_id = $user_id;
    if($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
      $name = $timestamp. '-' .$file->getClientOriginalName();
         $image->filePath = $name;
      $file->move(public_path().'/images/', $name);
  }
    $image->save();
     return $this->upload()->with('success', 'Image Uploaded Successfully');
}

如何创建图像缩略图,我使用了不同的库,但缩略图未能创建。

1个回答

10

我建议您使用intervention/image包,它非常适合与Laravel配合使用。

安装后,以下是上传和保存图像并创建缩略图的示例:

$image = Image::make(Input::file('photo')->getRealPath());
$image->save(public_path('photos/'. $id .'.jpg'));
$image->fit(300, 200)->save(public_path('photos/'. $id .'-thumbs.jpg'));

https://github.com/Intervention/image


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接