在laravel框架中上传配置文件图像

时间:2015-03-05 12:14:45

标签: php html laravel

您好我是laravel框架的新手,我正在开发一个仪表板,当用户在仪表板中更新他的详细信息以及个人资料照片时我没有收到任何错误,但是当用户仅更新细节时保持图像相同(没有更新图像),我收到一个错误:“在非对象上调用成员函数getClientOriginalName()”

我的blade.php是:

{{ Form::open(array('route' => 'account.profile-update', 'class' => 'form-horizontal', 'files'=>true)) }}

<div class="uploader" id="uniform-file">
                    {{Form::file('fileinput')}}
                    <span class="filename" style="-webkit-user-select: none;">Upload Image</span>
                    <span class="action" style="-webkit-user-select: none;">Browse</span>
                </div>
                <div>Please upload image of size 150x150 pixels</div>
            </div>

我的UserController.php:

public function updateProfile()
{
     $input = Input::all();        
      if ($this->user->updateUser($input)) {
          return Redirect::back()->withMessage('Data saved successfully');
      } else {
         return View::make('error.generic-errors')->withErrors($this->user->getMessages());
      }
 }

我的UserRepository.php:

   public function updateUser(array $attributes)
{
    $user = $this->getLoggedUser();
    if (strcmp($user->first_name, $attributes ['first_name']) !== 0)
        $user->first_name = $attributes ['first_name'];
    if (strcmp($user->last_name, $attributes ['last_name']) !== 0)
        $user->last_name = $attributes ['last_name'];
    if (strcmp($user->email, $attributes ['email']) !== 0)
        $user->email = $attributes ['email'];
    $this->updateUserInfo($attributes);
    if ($user->save()) {
        return true;
    } else {
        $this->messageBag->add('update_profile', 'Error while saving user details in profile page');
        return false;
    }
}

 private function updateUserInfo($attributes)
{
    $user_detail = UserDetail::where('user_id', '=', $this->getLoggedUserID())->firstOrFail();
    if (array_key_exists('fileinput', $attributes)) {
        if (!is_null($user_detail->picture)) {
            File::delete(public_path() . $user_detail->picture);
        }
        $user_detail->picture = $this->getImageName($attributes);
    }
    $user_detail->updateIfKeyExists($attributes);
}

   private function getImageName(array $attributes)
{
    if (!array_key_exists('fileinput', $attributes)) {
        return null;
    }
    $file = Input::file('fileinput');
    $destinationPath = public_path().Config::get('far.profile_pic_folder');                                    
    $temp_filename = 't_' . str_random(6) . '_' . substr($file->getClientOriginalName(), -40);
    $new_filename = substr($temp_filename, 2);
    $uploadSuccess = $file->move($destinationPath, $temp_filename);
    //TODO - check and handle for large filesize
    $img = Image::make($destinationPath . $temp_filename)->resize(150, 143)->save($destinationPath . $new_filename);
    File::delete($destinationPath . $temp_filename);
    return Config::get('far.profile_pic_folder') . $new_filename;
}

请帮我解决这个问题。 提前致谢

1 个答案:

答案 0 :(得分:0)

您需要安装在composer.json文件中添加的软件包&#34;干预/图像&#34;:&#34; ~2.0&#34;用于上传图片。

请参阅此处http://image.intervention.io/getting_started/installation#laravel

你的json文件看起来像这样。

"require": {
        "laravel/framework": "4.2.*",
        "robclancy/presenter": "1.3.*",
        "intervention/image": "~2.0"
    },

在json文件中添加此内容后。 运行composer update