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

上传图片并创建其缩略图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!

所以昨天我试图制作一个上传文件功能,当用户制作他的产品时,他也可以上传图片。 p>

但是当我迭代这些项目时图片太大了,所以我决定使用干预包来调整图片大小并创建缩略图。 p> \ n

我创建了该函数但部分工作。 p>

  if($ file = $ request-> hasFile('image')){
 $ file =  $ request->文件('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(); //维护图像比率
}); 
 $ 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; 
} 
  code>  pre> 
 
 

我这样做了,不同的用户将在 / uploads / products code>中创建一个不同的文件。 p>

此外,我上传了原始图片并调整了大小,以便我应该: picture.jpg code>和 thumb_picture.jpg code>。

当未创建自定义文件时(来自用户名)我收到此错误: p>

无法写入 图像数据到路径 (C:\ xampp \ htdocs \ shop \ public / uploads / products / book / thumb_Jellyfish.jpg) p> blockquote>

当我评论6时, 7,8行,该功能有效,但它只上传原始图片,因为它应该。 如果删除注释,缩略图也会起作用! p>

所以我想,在创建自定义文件夹之后,整个函数工作正常,但在它有可写问题之前。 p>

有什么想法吗? 一切都将不胜感激! p> div>

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