1
votes

I have api based image upload feature. I uploaded the project in a subdomain. enter image description here

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

My Controller:

 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
   }

}

But when i try to upload picture, it returns 500 server error! I want to store image in images/Gallery path. When i return $destinationPath to test in console, it returns : enter image description here If i delete this line :$img = Image::make($image->getRealPath());$img->encode('jpg')->save($destinationPath . '/' . $imageName);

, it is ok but image in not stored in the folder. I think Image package problem. I tried base_path instead of public_path as my folder is not in public folder. NOTE:Everything is ok in my local host

3

3 Answers

0
votes

The $destinationPath should be images/gallary, In your directory structure, there is no public folder. Make a public directory and move your images/gallary folder inside it or else change your destination path as

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

Please let me know for any queries!

0
votes

Heyy! Try this!

@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
votes

$img = Image::make($image->getRealPath());

This function make image from the actual path, So that you have to first save it to temperory folder and then resize it with intervention.

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