问题 Laravel 5.2中无法读取的图像源 - 干预图像


我有一个关于给定图像的大小调整过程的小问题,我正在尝试提交一个包含输入类型的表单 - >文件< - 我能够在不调整大小的情况下上传图片,之后我决定调整大小图像所以我使用以下方法安装了干预图像库:

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

}
}

但不是调整pic的大小,而是抛出以下异常

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

4747
2017-08-12 17:55


起源

你能检查一下吗? 回答? - AddWeb Solution Pvt Ltd
这段代码中的问题是,如果以前调用$ file-> move(),$ file-> getRealPath()总是返回false - shock_gone_wild
可能是你没有权限(chmod 600)?或者也许php.ini - > php_value post_max_size(也许图像太大了)? - Denis Solakovic


答案:


不应该 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());

11
2017-10-04 14:56



不,我没有工作,我已经尝试过了。 Image::make($file->getRealPath()) 要么 Image::make($file) 给出错误 Image source not readable。它在Laravel 5中工作,但在L5.2中没有。 - Tarunn
您是否通过手动查看目录来检查文件是否确实存在?此外,尝试使用调试器来执行代码并查看实际发生的情况。 - borfast
@Tarunn我在这个答案中有解释 stackoverflow.com/questions/38923863/... - abdalla arbab


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 正面。如果你们中的任何人有直接解决方案,请在此处显示您的代码,我将奖励您这笔赏金。干杯。


2
2017-10-10 14:46



这似乎是原始海报已经在做的事情,显然它没有用。 - borfast


在保存之前上传文件并调整其大小就像这样简单: (没有验证或检查)

您可以直接将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));

这只是在目前最新的5.2版本上进行了测试,版本为5.2.45

[编辑:]

如果你打电话

$file->move();

不要用

$file->getRealPath() 

之后,因为这会在调用move()后返回false

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

0
2017-10-11 05:32





移动图像后调整图像大小时会出现此问题

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

0
2017-08-19 06:07