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