Laravel 5.2中的图像来源无法读取 - Intervention Image

14

我有一个小问题涉及给定图像的调整大小过程,我正在尝试提交一个包含的表单。我能够上传一张未经过缩放的图片,之后我决定调整该图片的大小,因此我使用 Intervention Image Library 进行了安装:

composer require intervention/image

然后我将这个库集成到我的Laravel框架中。

Intervention\Image\ImageServiceProvider::class
'Image' => Intervention\Image\Facades\Image::class

最后我按照以下方式进行了配置。

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

我的控制器如下所示

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Image; 

class ProjectController extends Controller{

public function project(Request $request){  


    $file = Input::file('file');
    $fileName = time().'-'.$file->getClientOriginalName();

    $file -> move('uploads', $fileName);
    $img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());

}
}

但是,不是调整图片大小,而是抛出以下异常

NotReadableException in AbstractDecoder.php line 302:
Image source not readable

请您帮忙查看一下这个答案好吗? - AddWeb Solution Pvt Ltd
这段代码的问题在于,如果之前有调用 $file->move(),$file->getRealPath() 总是返回 false。 - shock_gone_wild
可能是因为您没有权限(chmod 600)?或者是php.ini -> php_value post_max_size(图片太大了)? - Denis Solakovic
7个回答

14

应该使用Image::make($file->getRealPath())而不是Image::make('public/uploads/', $file->getRealPath()),因为Image::make()似乎不接受两个参数,这可能是你的问题。

尝试这样做:

$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();

$file->move('uploads', $fileName);

$img = Image::make($file->getRealPath())
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());

或者,如果你想在不先移动文件的情况下完成操作,请尝试以下方法:

(译注:保留原文中的代码标签)

$file = Input::file('file');
$img = Image::make($file)
    ->resize(320, 240)
    ->save('public/uploads/', $file->getClientOriginalName());

2
不行,我已经尝试过了。Image::make($file->getRealPath()) 或者 Image::make($file) 会报错,显示图像源不可读取。在Laravel5中可以工作,但在L5.2中无法工作。 - Tarunn
你确定文件是否实际存在于目录中,通过手动查看目录吗?同时,尝试使用调试器来跟踪代码执行并查看实际发生了什么。 - borfast
@Tarunn 我在这个答案中有解释 https://dev59.com/TlkT5IYBdhLWcg3wNs3-#45768289 - Abdalla Arbab

3
L5.2 中,无法直接从 Input 门面获取图像。为此,我们需要先将图像存储在服务器上,然后将路径提供给 Image 门面以对图像进行操作。
代码如下:
if ($request->hasFile('picture') ) {

        $destinationPath = public_path('uploads/user');
        $photoname = date("YmdHis");
        $file_extention = '.'.$request->file('picture')->getClientOriginalExtension();
        $photo = $photoname.$file_extention;
        $file_check = $request->file('picture')->move($destinationPath, $photo);

        $thumb_path = $destinationPath.'/thumbnail/'.$photo;

        $new_filePath =  $destinationPath.'/'.$photo;

        $assets_path = url('uploads/user/');

        $img = Image::make($assets_path.'/'.$photo)->fit(100)->save($thumb_path,40);

        $data['picture'] = $photo;           
    }

我正在寻找直接解决方案,即以前可以直接从Input门面获取图像。如果你们中有人有直接解决方案,请在这里展示你的代码,我会给你奖励的。干杯。


这似乎是原帖作者已经在做的事情,但显然它没有起作用。 - borfast

1
上传文件并在保存前调整大小就像这样简单: (没有验证或检查)
您可以直接将UploadedFile的实例传递给InterventionImage :: make()
public function upload(Request $request)
{
    $file = $request->file('file');

    $filename = $file->getClientOriginalName();

    $img = \Image::make($file);
    $img->resize(320, 240)->save(public_path('uploads/'.$filename))

}

如果您想保存原始大小和调整大小后的图像:
    $img->save(public_path('uploads/'.$filename))
        ->resize(320, 240)
        ->save(public_path('uploads/thumb_'.$filename));

This was just tested on currently latest 5.2 version with is 5.2.45.
[编辑:]
如果您调用
$file->move();

不要使用


$file->getRealPath() 

之后,因为在调用move()之后将返回false。

    $filename = $file->getClientOriginalName();
    $file->move('uploads', $filename);
    dd($file->getRealPath());

1
这对我很有效。
$file->move($location,$file_name);  
$img = Image::make($location.'/'.$file_name)->fit(100)->save(public_path('thumbnails'.'/'.$file_name),50);

1
这个问题也可能发生在您不想使用 $filename = $file->getClientOriginalName(); 方法命名上传文件时。如果您想要创建自己的上传文件名称,可以使用下面的方法。
// $filename_without_ext = $request->input('name');
$filename_without_ext = Str::slug($request->input('name'));
$file_extension = pathinfo($logo->getClientOriginalName(), PATHINFO_EXTENSION);
$filename =   time() . '-' . $filename_without_ext . '.' . $file_extension;

如果您使用的是Laravel 5.8版本,且您正在尝试创建自己的文件名,则可能会遇到此错误。您应该检查名称字段是否作为表单输入出现。例如,对于上面的代码,如果您不使用Str::slug函数(这是一个Laravel助手函数),就像有注释的代码一样,由于表单字段可能具有的空格,您可能会遇到问题。

0
我刚解决了这个问题。
修改这一行:
$file -> move('uploads', $fileName);

$file = $file -> move('uploads', $fileName);

现在$file->getRealPath()已经有一个有效的值。

希望这对你有用。


0

当您移动图像后调整其大小时,可能会出现此问题。

$file->move('uploads', $fileName);

在移动图像之后,$file->getRealPath()将返回false。您需要在移动过程之前调整图像的大小。就是这样 ;)

$img=Image::make('public/uploads/', $file->getRealPath())->resize(320, 240)->save('public/uploads/',$file->getClientOriginalName());
$file->move('uploads', $fileName);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接