2

I currently save image strings (this is how they are provided through API) as a binary in my database but I need to (after creation), change this to a file structure, probably using Paperclip/Carrierwave and S3.

What is the best way to convert binary to image file e.g. jpg?

amaseuk
  • 2,147
  • 4
  • 24
  • 43
  • You are talking about just saving your BLOB data to a filesystem? You could make a ruby application that goes to each DB record and moves it to the System. As far as the two Paperclip is slightly more difficult to use but I find it gives more compatibility to Windows machines while Carrierwave is easier to use but if you ever use Windows it will give you major headaches with its image processors. – Travis Pessetto Aug 15 '11 at 14:33
  • I'm not going to use Windows but it will be on Heroku – amaseuk Aug 15 '11 at 14:37
  • Then I am not sure, both require a back-end of ImageMagick which has to be installed. My guess is you will be fine on Heroku because it is probably *Nix based. – Travis Pessetto Aug 15 '11 at 14:43

2 Answers2

3

This did the trick:

sio = StringIO.new(Base64.decode64(string))

[ source: base64 photo and paperclip -Rails ]

Community
  • 1
  • 1
amaseuk
  • 2,147
  • 4
  • 24
  • 43
  • I guess what do you do with the sio object after you've decoded it? Like if User has_attachment :file, then do you say User.create(:avatar => sio) ? – ajbraus Feb 21 '13 at 15:17
  • I can't remember how I did it and can't find the source code but this link might help: http://stackoverflow.com/questions/6718841/restful-file-uploads-with-carrierwave – amaseuk Feb 22 '13 at 10:17
1
file_arr = Model.find(:all)
file_arr.each do |file|
  File.open(file.name,'w'){|f| f.write(file.blob)}
end

would be my guess of how to do it. Where Model is your model .name is the name stored in the Database and .blob is the blob field...You may be able to do this via the Rails console.

This may not be the best answer but it may give you a start.

Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55