【发布时间】: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