doufutao4428
2017-01-18 07:16 阅读 52
已采纳

上传图片并创建其缩略图Laravel 5.2

So yesterday I tried to make an upload file function , for when user makes his products, he can also upload a picture too.

But the picture was too big when I was iterating through the items, so I decided to use intervention package to resize the picture and also create a thumbnail picture.

I made the function but its partially working.

if($file = $request->hasFile('image')) {
        $file = $request->file('image');
        $extension = $file->getClientOriginalName();
        $username = Auth::user()->username;
        $destinationPath = public_path('/uploads/products/' . $username);
        $thumb = Image::make($file->getRealPath())->resize(100, 100, function ($constraint) {
            $constraint->aspectRatio(); //maintain image ratio
        });
        $thumb->save($destinationPath.'/thumb_'.$extension);
        $destinationPath = public_path('/uploads/products/' . $username);
        $file->move($destinationPath, $extension);
        $product['imagePath'] = '/uploads/products/'. $username . '/' . $extension;
        $product['thumbnail'] = '/uploads/products/'. $username . '/thumb_' . $extension;
    }

I made it so, different user will create a different file in /uploads/products.

Also I upload the original picture and the resized so the I should have like: picture.jpg and thumb_picture.jpg.

When the custom file is not created (from the name of the user) I get this error:

Can't write image data to path (C:\xampp\htdocs\shop\public/uploads/products/book/thumb_Jellyfish.jpg)

When I comment 6,7,8 lines, the function works but it uploads only the original picture as it supposed to. If I remove the comment, the thumbnail works too!

So I guess, after the custom folder has been created, the whole function works fine, but before it has a writable problem.

Any ideas? Everything will be appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douke7431 douke7431 2017-01-18 08:46

    For anyone wonder how to fix this or do something similar, I just found the solution:

    if($file = $request->hasFile('image')) {
            $file = $request->file('image');
            $extension = $file->getClientOriginalName();
            $username = Auth::user()->username;
            $thumb = Image::make($file->getRealPath())->resize(100, 100, function ($constraint) {
                $constraint->aspectRatio(); //maintain image ratio
            });
            $destinationPath = public_path('/uploads/products/' . $username);
            $file->move($destinationPath, $extension);
            $thumb->save($destinationPath.'/thumb_'.$extension);
            $product['imagePath'] = '/uploads/products/'. $username . '/' . $extension;
            $product['thumbnail'] = '/uploads/products/'. $username . '/thumb_' . $extension;
        }
    

    So this piece of code makes a dynamic folder (I chose the username of the authenticated user) inside /uploads/products/. In that folder it uploads the picture and also creates a resized one, for thumbnail use. Also, when it creates the thumbnail, it holds the ratio of the original picture so it doesn't lose proportions

    点赞 评论 复制链接分享

相关推荐