基本64张照片到回形针导轨图像

时间:2016-07-12 14:19:34

标签: ruby-on-rails ruby image paperclip

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

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

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

我已经开始尝试使用paperclip 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)

2 个答案:

答案 0 :(得分:1)

您可以在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

答案 1 :(得分:0)

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

@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

我赞成以前的答案,因为它帮助我找到了其他资源,但它没有包含我的方案的答案。