title: Upload base64 encoded image with paperclip - Rails
body: <p>Using <a href="https://github.com/scottcheng/cropit" rel="nofollow">cropit</a> I get the image bas64 encode on rails through params.</p>
  
  <pre><code>  image = params['image'].gsub('data:image/jpeg;base64,', '')
    decoded_file = Base64.decode64(image)
  </code></pre>
  
  <p>and then I save to amazon s3 with paperclip</p>
  
  <pre><code>   begin
      file = Tempfile.new(['image', '.jpg'])
      file.binmode
      file.write decoded_file
      unless params['image_id']
        media_img = Media::Image.new()
        media_img.image = file
        if media_img.save
          render json: {status: 'success'}
        else
          render json: {status: 'error'}
        end
      else
        img = Media::Image.find(params['image_id'])
        img.update_attribute(:image, file)
        img.update_attribute(:name, params['image_name'])
        render json: {status: 'success'}
      end
      file.close
    ensure
      file.unlink
    end
  </code></pre>
  
  <p>The main problem is that the code is working only for jpeg images because I use gsub only for <code>data:image/jpeg;base64,</code> and when creating the Tempfile I created jpg <code>Tempfile.new(['image', '.jpg'])</code> . So how can I handle with best practice jpg, jpeg and png?</p>