34

我正在使用存储外观来存储一个工作正常的头像,但我想像在以前版本的 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).
4

10 回答 10

38

您正在尝试将错误的对象传递给 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;

假设你想使用 Storage 门面:

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

// Storage::put($path, $contents, $visibility = null)
Storage::put('public/image/myUniqueFileNameHere.jpg', $resize->__toString());
于 2017-01-08T10:51:29.630 回答
23

put方法适用于图像干预输出。putFile方法接受 Illuminate\Http\File 或 Illuminate\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);

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

于 2017-04-29T21:34:12.457 回答
13

使用 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
    );

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

return \Image::make(
    Storage::disk($photo->disk)
        ->get(
            $photo->fullPath()
        )
)->response();

解决方案

经过调查,我意识到由创建Image和保存的任何文件Storage的大小都是0 octets。在查看了这篇文章中的所有解决方案以及几个小时后,我注意到每个人都在使用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);

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

于 2019-06-28T09:59:06.870 回答
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);
于 2019-05-13T13:35:02.023 回答
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);
于 2017-05-05T11:29:21.967 回答
2

我已经通过以下方式完成了它,它简单且没有任何路径混淆:

//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);
于 2019-02-06T17:40:02.757 回答
1

确保添加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]);
于 2016-12-13T06:36:58.527 回答
0

在我的情况下,错误是由在图像实例上调用 hashName 方法引起的。

    //initially
    $image = Image::make(request('image')->getRealPath());
    $filename = $image->hashName();

    //to fix the error
    $image = Image::make(request('image')->getRealPath());
    $filename = request('image')->hashName();
于 2020-10-26T13:45:04.387 回答
0

你不能用 Laravel 5 文件系统直接存储 \Intervention\Image\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);
于 2017-04-11T04:07:09.960 回答
0

尝试更新当前 php 版本的 GD 扩展。

如果这没有帮助,请尝试将调整大小的图像保存在本地磁盘上并使用 Storage::putFile。

文件上传到您的存储路径后,您可以删除该文件。

putFile 方法的第二个参数是 Image Intervention 类的实例。您需要将此作为第二个参数传递给 putFile 方法。

$resize->save($absolutePath . 'small/' . $imageName);
于 2016-11-15T07:14:44.170 回答