我使用的存储门面存储的正常工作一个化身,但我想调整图片的大小就像我在laravel以前的版本一样。我怎么能去这样做?这里是我到目前为止(不工作)
$path = $request->file('createcommunityavatar');
$resize = Image::make($path)->fit(300);
$store = Storage::putFile('public/image', $resize);
$url = Storage::url($store);
错误信息:
Command (hashName) is not available for driver (Gd).
你想传递到PUTFILE错了对象。该方法期望文件对象(未图片)。
$path = $request->file('createcommunityavatar');
// returns \Intervention\Image\Image - OK
$resize = Image::make($path)->fit(300);
// expects 2nd arg - \Illuminate\Http\UploadedFile - ERROR, because Image does not have hashName method
$store = Storage::putFile('public/image', $resize);
$url = Storage::url($store);
好了,现在我们了解的主要原因,让我们修改代码
// returns Intervention\Image\Image
$resize = Image::make($path)->fit(300)->encode('jpg');
// calculate md5 hash of encoded image
$hash = md5($resize->__toString());
// use hash as a name
$path = "images/{$hash}.jpg";
// save it locally to ~/public/images/{$hash}.jpg
$resize->save(public_path($path));
// $url = "/images/{$hash}.jpg"
$url = "/" . $path;
让我们想象一下,您要使用存储门面:
// does not work - Storage::putFile('public/image', $resize);
// Storage::put($path, $contents, $visibility = null)
Storage::put('public/image/myUniqueFileNameHere.jpg', $resize->__toString());
PUT方法的工作原理与图像输出周期。该PUTFILE方法接受任一种照亮\ HTTP \文件或照亮\ HTTP \ UploadedFile的实例。
$photo = Image::make($request->file('photo'))
->resize(400, null, function ($constraint) { $constraint->aspectRatio(); } )
->encode('jpg',80);
Storage::disk('public')->put( 'photo.jpg', $photo);
上面的代码上传的文件大小调整到400像素宽度,同时保持高宽比。然后编码为80%质量为JPG。然后将文件存储到公共盘。请注意,您必须提供一个文件名,而不仅仅是目录。
我做这种方式:
注:当然,你可以根据自己的需要修改它。
$file = $request->file('portfolio_thumb_image');
$image = Image::make($file);
$image->resize(570, 326, function ($constraint) {
$constraint->aspectRatio();
});
$thumbnail_image_name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME).'.'.$file->getClientOriginalExtension();
$image->save(public_path('images/'.$thumbnail_image_name));
$saved_image_uri = $image->dirname.'/'.$image->basename;
//Now use laravel filesystem.
$uploaded_thumbnail_image = Storage::putFileAs('public/thumbnails/'.$portfolio_returned->id, new File($saved_image_uri), $thumbnail_image_name);
//Now delete temporary intervention image as we have moved it to Storage folder with Laravel filesystem.
$image->destroy();
unlink($saved_image_uri);
当试图这样一个保存和加载Image
读取图像文件与Storage
当我有类似的问题。
除了所有的答案,我不知道为什么它不工作。
异常时Image
试图读取该文件
干预\图片\异常\ NotReadableException:无法从给定的二进制数据来初始化。
加入->encode()
solved问题
http://image.intervention.io/api/encode
基本上,我有这样一个测试
Storage::fake();
$photo = factory(Photo::class)->create();
$file = \Image::make(
UploadedFile::fake()->image($photo->file_name, 300, 300)
);
Storage::disk($photo->disk)
->put(
$photo->fullPath(),
$file
);
而在控制器我有这样的事情
return \Image::make(
Storage::disk($photo->disk)
->get(
$photo->fullPath()
)
)->response();
调查后,我意识到,通过Image
创建并由Storage
保存任何文件的大小为0字节。望着这个职位后几个小时,所有的解决方案之后,我发现每个人都使用encode()
了,但没人做提到它是。于是,我就和它的工作。
调查多一点,Image
呢,其实,编码引擎盖下保存之前。 https://github.com/Intervention/image/blob/master/src/Intervention/Image/Image.php#L146
所以,我的解决办法是简单的这样做
$file = \Image::make(
\Illuminate\Http\UploadedFile::fake()->image('filename.jpg', 300, 300)
)->encode();
\Storage::put('photos/test.jpg', $file);
在廷克可测试,这将创建一个黑色图像
我曾与下面的方式,它的简单,没有任何路径混淆了这事。
//Get file
$path= $request->file('createcommunityavatar');
// Resize and encode to required type
$img = Image::make($file)->fit(300)->encode('jpg');
//Provide own name
$name = time() . '.jpg';
//Put file with own name
Storage::put($name, $img);
//Move file to your location
Storage::move($name, 'public/image/' . $name);
确保use Illuminate\Http\File;
此通过文件部分Automatic Streaming添加到文件的顶部工作,和阅读。
这是假设你希望所有的JPEG文件
$path = $request->file('createcommunityavatar');
$resize = Image::make($path)->fit(300)->encode('jpg');
$filePath = $resize->getRealPath() . '.jpg';
$resize->save($filePath);
$store = Storage::putFile('public/image', new File($resize));
$url = Storage::url($store);
这就是我如何与意见,以帮助做这件事在我的应用
// Get the file from the request
$requestImage = request()->file('image');
// Get the filepath of the request file (.tmp) and append .jpg
$requestImagePath = $requestImage->getRealPath() . '.jpg';
// Modify the image using intervention
$interventionImage = Image::make($requestImage)->resize(125, 125)->encode('jpg');
// Save the intervention image over the request image
$interventionImage->save($requestImagePath);
// Send the image to file storage
$url = Storage::putFileAs('photos', new File($requestImagePath), 'thumbnail.jpg');
return response()->json(['url' => $url]);
将干净的解决方案,我可以找到,使用本机Storage
门面,在下面。我可以证实,这部作品在Laravel 5.7,使用intervention/image
版本2.4.2。
$file = $request->file('avatar');
$path = $file->hashName('public/avatars');
$image = Image::make($file)->fit(300);
Storage::put($path, (string) $image->encode());
$url = Storage::url($path);
尝试更新GD扩展当前的PHP版本。
如果没有帮助,请尝试保存在本地磁盘上调整后的图像和使用Storage :: PUTFILE。
一旦它被上传到您的存储路径可以删除该文件。
您PUTFILE方法的第二个参数是图像干预类的一个实例。你需要通过这作为第二个参数PUTFILE方法。
$resize->save($absolutePath . 'small/' . $imageName);
你不能用Laravel 5文件系统直接存储在\干预\图片\ Image对象。你能做的是从你的要求调整图像大小,并将其保存在同一TMP路径下。然后,只需存储上传的(覆盖)文件到文件系统。
码:
$image = $request->file('createcommunityavatar');
//resize and save under same tmp path
$resize = Image::make($image)->fit(300)->save();
// store in the filesystem with a generated filename
$store = $image->store('image', 'public');
// get url from storage
$url = Storage::url($store);