上传图片并创建其缩略图 Laravel 5.2
Upload an image and create its thumbnail Laravel 5.2
所以昨天我试着做了一个,因为用户在制作他的产品时,他也可以上传图片。
但是在遍历items的时候图片太大了,所以我决定用intervention package调整图片的大小,同时创建一个缩略图。
我制作了这个功能,但它部分工作。
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;
}
我是这样做的,不同的用户会在/uploads/products
中创建不同的文件。
我还上传了原始图片和调整大小的图片,所以我应该喜欢:
picture.jpg
和 thumb_picture.jpg
.
当自定义文件未创建时(从用户名)我得到这个错误:
Can't write image data to path
(C:\xampp\htdocs\shop\public/uploads/products/book/thumb_Jellyfish.jpg)
当我评论第6、7、8行时,该功能有效,但它只按预期上传原始图片。如果我删除评论,缩略图也有效!
所以我猜想,在创建自定义文件夹之后,整个功能都可以正常工作,但是在它出现可写问题之前。
有什么想法吗?一切都会受到赞赏!
对于任何想知道如何解决这个问题或做类似事情的人,我刚刚找到了解决方案:
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;
}
因此这段代码在/uploads/products/
中创建了一个动态文件夹(我选择了经过身份验证的用户的用户名)。在该文件夹中,它会上传图片并创建一个调整大小的图片,供缩略图使用。此外,当它创建缩略图时,它会保留原始图片的比例,因此不会丢失比例
所以昨天我试着做了一个
但是在遍历items的时候图片太大了,所以我决定用intervention package调整图片的大小,同时创建一个缩略图。
我制作了这个功能,但它部分工作。
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;
}
我是这样做的,不同的用户会在/uploads/products
中创建不同的文件。
我还上传了原始图片和调整大小的图片,所以我应该喜欢:
picture.jpg
和 thumb_picture.jpg
.
当自定义文件未创建时(从用户名)我得到这个错误:
Can't write image data to path (C:\xampp\htdocs\shop\public/uploads/products/book/thumb_Jellyfish.jpg)
当我评论第6、7、8行时,该功能有效,但它只按预期上传原始图片。如果我删除评论,缩略图也有效!
所以我猜想,在创建自定义文件夹之后,整个功能都可以正常工作,但是在它出现可写问题之前。
有什么想法吗?一切都会受到赞赏!
对于任何想知道如何解决这个问题或做类似事情的人,我刚刚找到了解决方案:
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;
}
因此这段代码在/uploads/products/
中创建了一个动态文件夹(我选择了经过身份验证的用户的用户名)。在该文件夹中,它会上传图片并创建一个调整大小的图片,供缩略图使用。此外,当它创建缩略图时,它会保留原始图片的比例,因此不会丢失比例