[英]Upload an image and create its thumbnail 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
. 我做到了,不同的用户将在
/uploads/products
创建不同的文件。
Also I upload the original picture and the resized so the I should have like: picture.jpg
and thumb_picture.jpg
. 我也上传原始图片并调整大小,所以我应该像:
picture.jpg
和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)
无法将图像数据写入路径(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. 当我注释6,7,8行时,该功能有效,但它仅按原样上传原始图片。 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!
一切将不胜感激!
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/
. 因此,这段代码在
/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
另外,创建缩略图时,它会保留原始图片的比例,因此不会丢失比例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.