Create image file from base64 data
I have base64 encoded image data. I am inserting the first few characters
string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD /2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopG R8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgo......'
I do next to him in ruby
decoded_string=Base64.decode64 string output_file = Tempfile.new(['image','.jpeg']) output_file.binmode output_file.write image
After that, when I open 'image.jpeg' it gives error
Error interpreting JPEG image file (Not a JPEG file: starts with 0x75 0xab)
I have also tried
File.open('a.jpeg', 'wb') do|f|
f.write decoded_string
end
In this case also I got the same error.
What am I doing wrong?
+3
source to share
1 answer
File.open('shipping_label.gif', 'wb') do|f|
f.write(Base64.decode64(base_64_encoded_data))
end
This answer from: How to store a base64 string as an image using ruby
+1
source to share