【问题标题】:Picture upload returns 500 in laravel 5.4图片上传在 laravel 5.4 中返回 500
【发布时间】:2017-10-28 01:29:00
【问题描述】:

我有基于 api 的图片上传功能。我在子域中上传了项目。

我的 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 服务器错误!我想将图像存储在图像/画廊路径中。当我返回 $destinationPath 在控制台中进行测试时,它返回: 如果我删除这一行:$img = Image::make($image->getRealPath());$img->encode('jpg')->save($destinationPath . '/' . $imageName);

,没关系,但图像没有存储在文件夹中。我认为图像包问题。 我尝试了 base_path 而不是 public_path,因为我的文件夹不在公用文件夹中。 注意:我的本地主机一切正常

【问题讨论】:

    标签: php angularjs image laravel-5


    【解决方案1】:

    $destinationPath 应该是 images/gallery,在您的目录结构中,没有公共文件夹。创建一个公共目录并将您的图像/图库文件夹移动到其中,否则将您的目标路径更改为

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

    如有任何疑问,请告诉我!

    【讨论】:

    • 我提到我使用了 base_path() 但失败了。如果我使用 base_path() 并删除: $img = Image::make($image->getRealPath());$img->encode('jpg')->save($destinationPath . '/' . $imageName );并写 move($destinationPath , $imageName);并使用 base_path(),然后将图像保存在根图像/画廊文件夹中。但我需要使用名为干预的包来调整图像大小。
    【解决方案2】:

    嘿嘿!试试这个!

    @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

    【讨论】:

      【解决方案3】:

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

      此功能根据实际路径制作图像,因此您必须先将其保存到临时文件夹,然后再通过干预调整其大小。

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

      【讨论】:

        猜你喜欢
        • 2017-08-02
        • 2017-08-01
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 2017-12-13
        • 2019-09-21
        • 2017-09-20
        • 2017-11-19
        相关资源
        最近更新 更多