我想在Laravel中设计一个表单,该表单采用用户选择的图像,将其上传到服务器并将图像路径存储在'image'
字段中,并接受'text'
字段描述图像。我不知道如何将文本字段和图像存储为数组以传递给控制器。
表格
<div class="form-group">
{{ Form::label('description', trans('main.poster')) }}
{{ Form::text('description', Input::old('poster'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::file('image','files' => true) }}
</div>
<button type="submit" class="btn btn-success">{{ trans('dash.update') }}</button>
控制器
public function store()
{
$input = array('image' => Input::file('image'));
if ( ! $this->validator->setRules('image')->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors());
}
$this->title->uploadImage($input);
return Redirect::back()->withSuccess( trans('main.uploaded image success') );
上传图片
public function uploadImage(array $input)
{
$name = str_random(25);
$insert = array('image' => asset('assets/images/'.$name.'.jpg'), );
$this->images->saveTitleImage($input, $name);
}
保存图片路径
public function saveTitleImage($input, $name)
{
$encoded = Imagine::make($input['image']->getRealPath())
->encode('jpg');
Imagine::make($encoded)->save(public_path('assets/images/'.$name.'.jpg'));
}