2017-05-27 2 views
1

私はAPIベースの画像アップロード機能を持っています。サブドメインにプロジェクトをアップロードしました。 enter image description here写真のアップロードでは、ラーベル5.4で500が返されます。

私のJS(angularjs):

var data = new FormData(); 

     data.append('picture', $scope.picture); 
     data.append('title', $scope.title); 
     data.append('description', $scope.description); 
     data.append('captured_by', $scope.captured_by); 

     $http.post('api/SaveGallery',data,{ 
      withCredentials: true, 
      headers: {'Content-Type': undefined }, 
      transformRequest: angular.identity 

     }).then(function (data) { 
      console.log(data) 

マイコントローラー:

use Illuminate\Http\Request; 

use Session; 
use DB; 
use Illuminate\Database\Eloquent; 
use Illuminate\Support\Facades\Auth; 
use Exception; 
use Response; 

use Image; 
use File; 

    public function SaveGallery(Request $r) 
{ 
    if ($r->hasFile('picture')) { 
     $image = $r->file('picture'); 
     $imageName = $image->getClientOriginalName(); 
     $destinationPath = public_path('images/Gallery'); 


     $img = Image::make($image->getRealPath()); 
     $img->encode('jpg')->save($destinationPath . '/' . $imageName); 

     DB::table('gallerys') 
      ->insert([ 
       'title' => $r->title, 
       'picture'=> $imageName, 
       'description' => $r->description, 
       'captured_by' => $r->captured_by=='undefined'?'Famous Bag':$r->captured_by, 
       'create_by' => Auth::user()->username, 
       'create_date' => date('Y-m-d H:i:s') 
      ]); 

     return Response::json(['succ' => 'added'], 200); // Status code here 

} else { 
     // $imageName = null; 
     return Response::json(['picnotfound' => 'picnotfound'], 201); // Status code here 
    } 

} 

しかし、私は写真をアップロードしようとすると、それは500サーバーエラーを返します!画像/ギャラリーパスに画像を保存したいenter image description here この行を削除した場合:$ img = Image :: getRealPath()); $ img-> encode( 'jpg') - コンソールでテストする$ destinationPathを返すと、 > save($ destinationPath。 '/'。$ imageName);

でもOKですが、フォルダに保存されていない画像です。イメージパッケージの問題だと思う。 私のフォルダがパブリックフォルダにないので、私はpublic_pathの代わりにbase_pathを試しました。 注:ローカルホストですべてOKです。

答えて

0

$ destinationPathはimages/gallaryにする必要があります。ディレクトリ構造にはパブリックフォルダがありません。パブリックディレクトリを作成し、その中に画像/ギャラリーフォルダを移動するか、宛先パスを次のように変更します。

$ destinationPath = base_path( 'images/galary');

質問があれば教えてください。

+0

私はbase_path()を使用したが、失敗したと述べました。もし私がbase_path()を使って、$ img = Image :: make($ image-> getRealPath()); $ img-> encode( 'jpg') - > save($ destinationPath。 '/'。$ imageName ); write move($ destinationPath、$ imageName);と書いてください。 base_path()を使用し、ルートイメージ/ギャラリーフォルダに保存されたイメージを使用します。しかし、私は、介入という名前のパッケージを使用してイメージのサイズを変更する必要があります。 – Fawel

0

Heyy!これを試して!

@switch ($friend['reqs_status'])    
 
@case 0:    
 
//show button already request sent     
 
@breakcase;    
 
@case 1:    
 
    //show accept and reject request button    
 
    @breakcase;   
 
    @case 2:     
 
    //meaning they are friends already    
 
    @default:    
 
    # code...     
 
    @breakcase;  
 
     @endswitch

0

の$ IMG =画像::($イメージ - > getRealPath())します。

この関数は、イメージを実際のパスから作成するので、まずそれをtemperoryフォルダに保存してから、それを介入してサイズを変更する必要があります。

$ img = Image :: make( "images/temp/filename.ext");

関連する問題