base 64 照片到回形针 rails 图片

base 64 photo to paperclip rails image

我有一个 rails 应用程序,我正在接收一个 base64 编码的字符串,它是来自 ios 应用程序的图像。

我正在尝试将其转换为回形针图像。

到目前为止我有:

module Api
    module V1
        class MyController < BaseController
            def image_build 
                begin
                   token = request.headers["HTTP_TOKEN"]
                   @user = User.find_by(user_token: token)
                   @decoded_image = Base64.decode64(params[:image_data])
                   puts @decoded_image[0, 50]
                   @new_stand = Stand.create(:user_id => @user.id, :avatar => @decoded_image)
                   ...

输出:

iVBORw0KGgoAAAANSUhEUgAAAPsAAACxCAYAAAACscusAAAAAX
�PNG

IHDR��ˬsRGB���  
Completed 500 Internal Server Error in 90ms (Views: 3.1ms | ActiveRecord: 1.1ms)

如何在 rails 中将 base64 字符串转换为正确的图像文件?我从这个 gem:

得到回形针 运行
gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"

备注

发送的图像可以是来自 ios phone 的任何图像,因此类型可以更改。我想支持 .gif、.png、.jpg 和 jpeg。

在IOS我发送的信息是这样的:

@IBOutlet var image: UIImage
@IBAction func Build(sender: AnyObject) {
    let image_data = UIImagePNGRepresentation(image.image!)
    self.service.createNewImage(notifier: notifier, image: image_data!)
}

then


let strBase64:String = image.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let dataDictionary = ["image_data": strBase64]
self.post("image/build", data: dataDictionary).responseJSON

编辑 1

我已经开始尝试使用回形针 io 适配器,但它仍然出错。这是我目前所拥有的:

                @new_image = Stand.create(:user_id => @user.id)
                encoded_picture = params[:image_data]
                content_type = "image/jpg"
                puts "HERE 1"
                image = Paperclip.io_adapters.for("data:#{content_type};base64,#{encoded_picture}")
                puts "HERE 2"
                image.original_filename = "new_image.jpg"
                puts "HERE 3"
                @new_image.avatar = image
                puts "HERE 4"

                @new_image.cover_image_url = @new_image.avatar.url
                puts "HERE 5"
                @new_image.save

这段代码给出了这些错误:

HERE 1
HERE 2
HERE 3
Command :: file -b --mime '/tmp/082995477845923f853c5a48cc6e3cae20160712-26217-s1dngb.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' 2>/dev/null
Command :: identify -format %m '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]'
Command :: convert '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' -auto-orient -resize "160x160>" '/tmp/4a98b13d8ce55141b173bb0ed5cb181220160712-26217-17sazsk'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' 2>/dev/null
Command :: identify -format %m '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]'
Command :: convert '/tmp/8d777f385d3dfec8815d20f7496026dc20160712-26217-1ht7ijm[0]' -auto-orient -resize "40x40>" '/tmp/4a98b13d8ce55141b173bb0ed5cb181220160712-26217-fhdb0w'
HERE 4
Completed 500 Internal Server Error in 654ms (Views: 2.5ms | Searchkick: 15.6ms | ActiveRecord: 7.3ms)

您可以在 paperclip's 存储库中找到很多有用的信息,对于您的问题,我发现了这个:

3.5.0:

  • Feature: Handle Base64-encoded data URIs as uploads

Paperclip::DataUriAdapter 处理 base64-encoded 数据。

有效:

class Photo < ActiveRecord::Base

  before_validation :set_image

  has_attached_file :image

  def set_image
    StringIO.open(Base64.decode64(image_json)) do |str|
      decorate_str(str)
      str.original_filename = "THE_NAME_OF_FILE.jpg"
      str.content_type = "image/jpeg"
      self.image = data
    end
  end

  def decorate_str(str)
    str.class.class_eval { attr_accessor :original_filename, :content_type }
  end

end

解决您的问题的类似方法是 here

Paperclip's changelog

这似乎可行,但我仍在努力测试不同的图像类型。

@new_image = Stand.create(:user_id => @user.id)
encoded_picture = params[:image_data]
content_type = "image/jpg"
image = Paperclip.io_adapters.for("data:#{content_type};base64,#{encoded_picture}")
image.original_filename = "new_image.jpg"
@new_image.avatar = image
@new_image.save
@new_image.cover_image_url = @new_image.avatar.url
@new_image.save

我对之前的答案投了赞成票,因为它帮助我找到了其他资源,但它不包含我的场景的答案。