laravel Intervention\Image\Exception\NotSupportedException 不支持编码格式 (tmp)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50061985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intervention\Image\Exception\NotSupportedException Encoding format (tmp) is not supported
提问by Debjit Roy
I am using the Intervention package with Laravel 5.6, the issue I am getting whenever I am uploading a file I have been presented with the error Encoding format(tmp) is not supported. I have my gdd2 extension enabled also. This is the code where I have used.
我在 Laravel 5.6 中使用干预包,每当我上传文件时都会遇到这个问题,我遇到了错误编码格式(tmp)不受支持。我也启用了我的 gdd2 扩展。这是我使用过的代码。
public function store(Request $request)
{
$this->validate($request , [
'name' => 'required|unique:categories',
'description' => 'max:355',
'image' => 'required|image|mimes:jpeg,bmp,png,jpg'
]);
// Get Form Image
$image = $request->file('image');
$slug = str_slug($request->name);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imageName = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
// Check if Category Dir exists
if (!Storage::disk('public')->exists('category'))
{
Storage::disk('public')->makeDirectory('category');
}
// Resize image for category and upload
$categoryImage = Image::make($image)->resize(1600,479)->save();
Storage::disk('public')->put('category/'.$imageName, $categoryImage);
// Check if Category Slider Dir exists
if (!Storage::disk('public')->exists('category/slider'))
{
Storage::disk('public')->makeDirectory('category/slider');
}
// Resize image for category slider and upload
$categorySlider = Image::make($image)->resize(500,333)->save();
Storage::disk('public')->put('category/slider/'.$imageName, $categorySlider);
}
else
{
$imageName = 'default.png';
}
$category = new Category();
$category->name = $request->name;
$category->slug = $slug;
$category->description = $request->description;
$category->image = $imageName;
$category->save();
Toastr::success('Category Saved Successfully','Success');
return redirect()->route('admin.category.index');
}
采纳答案by Rob Fonseca
The Intervention image save()method requires a filename so it knows what file format (jpg, png, etc..) to save your image in.
Intervention image save()方法需要一个文件名,因此它知道要保存图像的文件格式(jpg、png 等)。
The reason you are getting the error is it does not know what encoding to save the temporary image object (tmp)in.
您收到错误的原因是它不知道将临时图像对象(tmp)保存在什么编码中。
Here is an example
这是一个例子
->save('my-image.jpg', 90)
There is also a optional second parameter that controls the quality output. The above outputs at 90% quality.
还有一个可选的第二个参数来控制质量输出。以上输出质量为 90%。
回答by Mohammad Abbas
You don't need to use the save() function on the Intervention\Image
class as you are saving the file to your public disc via the Storage
Facade.
Intervention\Image
当您通过Storage
Facade将文件保存到公共光盘时,您不需要在类上使用 save() 函数。
Simply replace the line
只需更换线路
$categoryImage = Image::make($image)->resize(1600,479)->save();
$categoryImage = Image::make($image)->resize(1600,479)->save();
with
和
$categoryImage = Image::make($image)->resize(1600,479)->stream();
$categoryImage = Image::make($image)->resize(1600,479)->stream();
to avoid having to store the image to the temp folder under a .tmp extension. Laravel Storage
Facade will handle the stream created by Intervention\Image
and store the file to the public disk.
以避免必须将图像存储到 .tmp 扩展名下的临时文件夹。Laravel Storage
Facade 将处理由创建的流Intervention\Image
并将文件存储到公共磁盘。
回答by Raphael D'Emedion
Saw this somewhere and it worked for me
在某处看到这个,它对我有用
$image->save('foo' . $img->getClientOriginalExtension());
回答by Raihan Islam
The Laravel Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in
Laravel Intervention 图像 save() 方法需要一个文件名,以便它知道将图像保存在什么文件格式(jpg、png 等)中
$categoryImage = Image::make($image)->resize(1600,479)->save( $imageName,90);
回答by Muhammad Zeshan Ghafoor
I've solved this by
我已经解决了这个问题
Trimming my file path, i was using this script inside laravel Artisan Console.
修剪我的文件路径,我在 laravel Artisan Console 中使用了这个脚本。
$img->save(trim('public/uploads/images/thumbnails/'.$subFolder.'/'.$filename));
回答by Julfikar Haidar
Rather you use stream its working without error
相反,您可以使用流来正常工作
$categoryImage = Image::make($image)->resize(1600,479)->save();