I am trying to upload a image with my form in my Laravel project. I have a Image cropper that saves the image as data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/..
the cropper sends a JSON string which contains a base64 encoded string of the file, i need to parse the JSON string to an object and then extract the base64 string and turn that into a file object.
I am using Image Intervention for the upload process
Controller function
public function store(Request $request) { // save post in db $post = new Post; $post->title = $request->title; $post->body = $request->body; $post->user_id = auth()->id(); if ($request->hasFile('featimage')) { $image = $request->file('featimage'); $filename = time() . '.' . $image->getCLientOriginalExtension(); $image = trim($image); $image = str_replace('data:image/png;base64,', '', $image); $image = str_replace('data:image/jpg;base64,', '', $image); $image = str_replace('data:image/jpeg;base64,', '', $image); $image = str_replace('data:image/gif;base64,', '', $image); $image = str_replace(' ', '+', $image); $imagedata = base64_decode($image); //Set image whole path here $location = public_path('images/uploads/' . $filename); Image::make($image)->save($location); $post->image = $filename; } $post->save(); $post->tags()->sync($request->tags, false); Session::flash('success', 'The blog post was saved successfully!'); return redirect()->route('posts.show', $post->id); } View
<form class="form-horizontal" action="{{route('posts.store')}}" method="POST" enctype="multipart/form-data">{{ csrf_field() }} <fieldset class="form-group"> <label for="featimage">Upload Image</label> <input type="file" class="form-control slim" data-ratio="4:3" name="featimage"> <label class="col-md-2 col-form-label">Post Body</label> <textarea type="textarea" class="form-control" rows="10" name="body"></textarea> </fieldset> <button class="btn btn-primary btn-block" name="submit">Save</button> <button class="btn btn-danger btn-block" name="submit">Cancel</button> </form> I have read other posts like this and i'm clearly missing something but i just don't know what.
$request->hasFile('featimage')definitely evaluating totrue??