doufutao4428 2017-01-17 23:16
浏览 87
已采纳

上传图片并创建其缩略图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 2017-01-18 00: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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部