【问题标题】:base 64 photo to paperclip rails imagebase 64 照片转回形针导轨图像
【发布时间】:2016-07-12 14:19:33
【问题描述】:

我有一个 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)

如何将 base64 字符串转换为 rails 中的正确图像文件?我有从这个宝石运行的回形针:

gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git"

备注

发送过来的图像可以是来自 ios 手机的任何图像,因此类型可以更改。我想支持 .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)

【问题讨论】:

    标签: ruby-on-rails ruby image paperclip


    【解决方案1】:

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

    @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
    

    我赞成上一个答案,因为它帮助我找到了其他资源,但它不包含我的方案的答案。

    【讨论】:

      【解决方案2】:

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

      3.5.0:

      • 功能:将 Base64 编码的数据 URI 处理为上传

      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

      【讨论】:

      • 现在我正在尝试将您的代码放入我已有的代码中。我有一个函数,但你有一个类。
      • 您的版本可以处理多种可能的图像类型吗?
      • 不,它不处理多种类型。你可以使用str.content_type 来做到这一点
      • @Rorschach Stand 是什么,是模特吗?
      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2016-06-23
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多