doufutao4428 2017-01-18 07: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 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

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分