使用Laravel 5.4存储的图像干预

时间:2016-10-13 15:42:23

标签: 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);

错误讯息:

  Command (hashName) is not available for driver (Gd).

10 个答案:

答案 0 :(得分:19)

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

<input v-model-blur="myVar" />

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

$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;

答案 1 :(得分:4)

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。 然后将该文件存储到公共光盘。 请注意,您必须提供文件名,而不仅仅是目录。

答案 2 :(得分:3)

我是这样做的:

  1. 调整大小并将图像保存在某处(例如公共文件夹中)。
  2. 创建一个新文件并将其传递给Laravel文件系统函数(例如putFileAs)。
  3. 删除临时干预文件
  4. 注意:当然您可以根据需要进行修改。

    $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);
    

答案 3 :(得分:2)

使用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个八位字节。 在查看了这篇文章以及之后几个小时的所有解决方案之后,我注意到每个人都在使用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);

修补程序中可测试,它将创建黑色图像

答案 4 :(得分: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]);

答案 5 :(得分:1)

我已经通过以下方式做到了这一点,它简单且没有任何路径混乱:

//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);

答案 6 :(得分:0)

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

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

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

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

$resize->save($absolutePath . 'small/' . $imageName);

答案 7 :(得分: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);

答案 8 :(得分:0)

使用本机Storage外立面,我能找到的最干净的解决方案如下。我可以使用intervention/image版本2.4.2在Laravel 5.7中确认这一点。

$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);

答案 9 :(得分: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();