2016-08-12 3 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()); 

} 
} 

대신 다음과 같은 예외가 Image::make($file->getRealPath()) 대신 Image::make('public/uploads/', $file->getRealPath()) 있어야하지

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-> getRealPath()가 $ file-> move()에 대한 이전 호출이 있으면 항상 false를 반환한다는 것입니다. –

+0

권한 (chmod 600)을 가질 수 없습니까? 아니면 php.ini -> php_value post_max_size (아마 이미지가 너무 큽니다)? –

답변

9

을 throwed 된 그림의 크기를 조정의 다음?

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

을 아니면 먼저 파일을 이동하지 않고 그것을 수행하려는 경우,이 시도 :

이 시도 L5.2에서

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

Nop, 작동하지 않는 나는 이미 시도했다. '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

그것의 직접적 가능 Input에서 이미지를 얻을 정면. 이를 위해서는 먼저 이미지를 서버에 저장 한 다음 이미지에 대한 작업을 수행하기 위해 정면에 Image의 경로를 지정해야합니다. 이 Input 외관에서 직접 이미지를 촬영하기 이전에 가능했던 것처럼 내가 직접 솔루션 즉 찾고 있었다

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

:

코드가 좋아하는이 간다. 누구든지 직접적인 해결책을 가지고 있다면 여기에 코드를 보여주십시오.이 현상금에 대해 보상 해 드리겠습니다. 건배.

+0

원래의 포스터가 이미하고 있던 것이고 분명히 작동하지 않았습니다. – borfast

0

파일을 업로드하고 절약만큼 쉽게하기 전에 크기를 조절 : 직접 InterventionImage에 UploadedFile의 인스턴스를 전달할 수 있습니다

(검증이나 확인없이) :: 수 있도록()

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

[편집 :]이 움직임을 호출 한 후 false를 반환하기 때문에

전화 할 경우

$file->move(); 

가, 나중에

$file->getRealPath() 

사용하지 마십시오()

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