图像的干预/ Laravel 5.4寄存

问题描述 投票:18回答:8

我使用的存储门面存储的正常工作一个化身,但我想调整图片的大小就像我在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).
php laravel laravel-5.3 laravel-5.4
8个回答
21
投票

你想传递到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());

4
投票

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


3
投票

我做这种方式:

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

3
投票

使用Laravel 5.8

当试图这样一个保存和加载Image读取图像文件与Storage当我有类似的问题。 除了所有的答案,我不知道为什么它不工作。


异常时Image试图读取该文件

干预\图片\异常\ NotReadableException:无法从给定的二进制数据来初始化。

Short answer

加入->encode()solved问题

http://image.intervention.io/api/encode

Scenario

基本上,我有这样一个测试

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

Solution

调查后,我意识到,通过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);

在廷克可测试,这将创建一个黑色图像


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

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

1
投票

将干净的解决方案,我可以找到,使用本机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);

0
投票

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

如果没有帮助,请尝试保存在本地磁盘上调整后的图像和使用Storage :: PUTFILE。

一旦它被上传到您的存储路径可以删除该文件。

您PUTFILE方法的第二个参数是图像干预类的一个实例。你需要通过这作为第二个参数PUTFILE方法。

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

0
投票

你不能用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);
© www.soinside.com 2019 - 2024. All rights reserved.