使用Laravel 5.4存储的图像干预

wiz*_*zeb 22 php laravel laravel-5.3 laravel-5.4

我正在使用存储外观存储一个工作正常的头像,但我想调整我的图像大小,就像我之前版本的laravel一样.我该怎么做呢?这是我到目前为止(不起作用)

  $path   = $request->file('createcommunityavatar');
  $resize = Image::make($path)->fit(300);
  $store  = Storage::putFile('public/image', $resize);
  $url    = Storage::url($store);
Run Code Online (Sandbox Code Playgroud)

错误信息:

  Command (hashName) is not available for driver (Gd).
Run Code Online (Sandbox Code Playgroud)

Leo*_*kov 25

你试图将putFile传递给错误的对象.该方法需要File对象(而不是Image).

$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);
Run Code Online (Sandbox Code Playgroud)

好的,现在当我们了解主要原因时,让我们修复代码

// 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;
Run Code Online (Sandbox Code Playgroud)

让我们假设你想要使用存储外观:

// does not work - Storage::putFile('public/image', $resize);

// Storage::put($path, $contents, $visibility = null)
Storage::put('public/image/myUniqueFileNameHere.jpg', $resize->__toString());
Run Code Online (Sandbox Code Playgroud)

  • 上面的代码工作得很好,有一点需要注意:`md5($resize->__toString());` 对于同一个文件总是返回相同的哈希值,这在某些情况下可能会出现问题。在我的解决方案中,我已将其替换为 `$now = Carbon::now()->toDateTimeString(); $hash = md5($image->__toString().$now);` (2认同)

Jef*_*rey 17

方法适用于图像输出周期。该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);
Run Code Online (Sandbox Code Playgroud)

上面的代码在保持纵横比的同时将上传的文件调整为 400px 宽度。然后以 80% 的质量编码为 jpg。然后将该文件存储到公共光盘。请注意,您必须提供文件名,而不仅仅是目录。


Jes*_*ijm 6

我能找到的最干净的解决方案——使用原生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);
Run Code Online (Sandbox Code Playgroud)


Md.*_*med 5

我这样做:

  1. 调整图像大小并将其保存在某处(例如在公共文件夹中)。
  2. 创建一个新文件并将其传递给 Laravel 文件系统函数(例如 putFileAs)。
  3. 删除临时干预文件

注:当然您可以根据需要进行修改。

$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);
Run Code Online (Sandbox Code Playgroud)


cba*_*ier 5

使用Laravel 5.8

试图当我有一个类似的问题,阅读与图像文件Image时,这个被保存装载Storage
除了所有答案,我不确定为什么它不起作用。


Image尝试读取文件时发生异常

Intervention \ Image \ Exception \ NotReadableException:无法从给定的二进制数据初始化。

简短答案

添加->encode()解决了这个问题

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
    );
Run Code Online (Sandbox Code Playgroud)

在控制器中,我有这样的东西

return \Image::make(
    Storage::disk($photo->disk)
        ->get(
            $photo->fullPath()
        )
)->response();
Run Code Online (Sandbox Code Playgroud)

经过调查,我意识到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);
Run Code Online (Sandbox Code Playgroud)

可在Tinker中测试,它将创建黑色图像