I would like to design a form in Laravel which takes an image the User has selected, uploads it to the server and stores the image path in a 'image'
field and also accepts a 'text'
field which describes the image. I do not know how to store both the text field and image as an array to pass to the controller.
The Form
<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>
Controller
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') );
Upload image
public function uploadImage(array $input)
{
$name = str_random(25);
$insert = array('image' => asset('assets/images/'.$name.'.jpg'), );
$this->images->saveTitleImage($input, $name);
}
Save image path
public function saveTitleImage($input, $name)
{
$encoded = Imagine::make($input['image']->getRealPath())
->encode('jpg');
Imagine::make($encoded)->save(public_path('assets/images/'.$name.'.jpg'));
}