2016-08-12 85 views
11

我有一个关于给定图像调整大小过程的小问题,我试图提交一个包含输入类型的表单 - >文件< - 我能够上传而不调整其大小,在那之后我决定调整该图像的图片,让我安装使用介入图片库:图像源在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()); 

} 
} 

,而不是调整以下异常扔

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

可否请你检查这个【答案】(http://stackoverflow.com/questions/33468437/getting-error-notreadableexception-in-abstractdecoder-php-line-302/33469360#33469360 )? –

+0

此代码中的问题是,如果以前调用$ file-> move() –

+0

,那么$ file-> getRealPath()总是返回false;难道你没有权限(chmod 600)?或者也许php.ini - > php_value post_max_size(也许图像太大)? –

回答

9

它不应该是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()); 
+1

Nop,那不行,我已经试过了。 ''Image :: make($ file-> getRealPath())''或''Image :: make($ file)''给出的错误是''Image source not readable''。它在Laravel 5工作,但不在L5.2。 – Tarunn

+0

你有没有通过手动查看目录来检查文件是否真的存在?此外,请尝试使用调试器来执行代码并查看实际发生的情况。 – borfast

+0

@Tarunn我在这个答案中有解释https://stackoverflow.com/questions/38923863/image-source-not-readable-in-laravel-5-2-intervention-image/45768289#45768289 –

2

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门面采取图像。如果你们中的任何一个人都有直接的解决方案,请在这里展示你的代码,我会奖励你这笔赏金。干杯。

+0

这似乎是原始海报已经在做什么,显然它没有工作。 – borfast

0

上传文件,并调整其大小之前储蓄是这么简单: (未经验证或支票)

可以UploadedFile的实例直接传递给InterventionImage ::使()

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

之后,因为这将调用移动后返回false()

$filename = $file->getClientOriginalName(); 
    $file->move('uploads', $filename); 
    dd($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);