Restore jpeg file from its encoded64 code in Rails/Ruby - ruby

Our question is that with Base64 encoded jpeg image file in uploaded_io, how to restore jpeg file out of it?
The encoded uploaded_io is generated by canvas.toDataURL("image/jpeg"). Here is the uploaded_io looks like:
uploaded_io = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2....."
In ruby/rails 4, a base64 encoded file could be decoded with:
require 'base64'
decoded = Base64.decode64(uploaded_io.sub(/.+,/, '')) #removed file header 'data:image/jpeg;base64,' as suggested
We added the gem mini_magick (v3.5.0) and installed the image magick library on our computer. Did the following:
image = MiniMagick::Image.new(decoded)
However the image is not a jpeg image file and does not respond well to .type and .size. There is no need to manipulate the image file and we are not sure that weather mini_magick/image magick are really needed here.

One issue that stands out is your decoding the image then removing the header which will cause problems.
image = MiniMagick::Image(decoded.sub(/.+,/, ''))
I did a simple test encoding / decoding an image using Ruby Base64 and everything worked as expected.
irb example:
require 'base64'
e = Base64.encode64(IO.read('/path/to/jpeg'))
d = Base64.decode64(e)
File.open("test.jpg", "w") { |f| f.write(d) }
test.jpg should be a valid file. Confirm by executing file test.jpg.

Related

Creating image file from base64 data [duplicate]

This question already has answers here:
Open and save base64 encoded image data URI in Ruby
(4 answers)
Closed 5 years ago.
I have a base64 encoded image data . I am pasting the first few characters
string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD /2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopG R8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgo......'
I am doing following to it in ruby
decoded_string=Base64.decode64 string
output_file = Tempfile.new(['image','.jpeg'])
output_file.binmode
output_file.write image
After this when I am opening 'image.jpeg', It is giving error
Error interpreting JPEG image file (Not a JPEG file: starts with 0x75 0xab)
I 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?
File.open('shipping_label.gif', 'wb') do|f|
f.write(Base64.decode64(base_64_encoded_data))
end
This answer is from: How to save a base64 string as an image using ruby

Grim pdf to png in ruby using grim

I run the below code for pdf to png conversion using grim
pdf = Grim.reap(File.dirname(__FILE__)<<"/pdf.pdf")
count = pdf.count
pdf[3].save('like.png')
text = pdf[3].text
pdf.each do |page|
puts page.text
end
It shows following error:
No such file or directory - gs -dNODISPLAY -q -sFile=./pdf.pdf
C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/pdf_info.ps (Errno::ENOENT)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/grim/image_magic
k_processor.rb:21:in count'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/grim-1.3.0/lib/grim/pdf.rb:35:i
ncount'
from pdfpng.rb:18:in `'"
It looks like the Ghostscript dependency is missing as that provides the gs binary, or you do have that installed but it's not present in your PATH.

Saving a GIF image with RMagick failure

I'm using ruby 2.1.0, rmagick 2.15.4, ImageMagick 6.7.7-10
I'd like to load a JPEG file and then save in the GIF format.
x = Magick::Image.read("a.jpg").first
puts "Start write..."
x.format = "GIF"
x.write("a.gif")
puts "Done."
Gives me this:
Start write...
cli.rb:104:in `exit': no implicit conversion from nil to integer (TypeError)
The stack trace includes foreman and thor gems, but no step in my code.
The filesystem has a.gif defined, but the filesize is zero.
UPDATE
I think I have a problem with ImageMagick itself. Here's what happens on the command line:
$ convert -debug a.jpg a.gif
convert.im6: unrecognized event type `a.jpg' #error/convert.c/ConvertImageCommand/1135.
It looks like this question was answered here:
JPG to PNG using RMagick
Just changing the file name will not convert he file format.
Yes it will. Something else is going on here.
"This makes it easy to convert an image file to another format. Simply write the image file using a name that has either a prefix or a suffix corresponding to the format you want."
https://rmagick.github.io/imusage.html

Uploading Images through Sinatra

I'm using the example code from this page:
http://www.wooptoot.com/file-upload-with-sinatra
When I try to upload an image file (png or jpg), it uploads successfully and I can see the file in the proper directory, but it gets corrupted in the process. I cannot open the image. Doing a diff with the original files, I see several newlines that are missing in the uploaded version.
I'm running Ruby 1.9.3p392 on Windows.
Edit:
I tried a test outside the context of Sinatra
File.open('57-new.jpg', "wb") do |f|
f.write(File.open('57.jpg', 'rb').read)
end
That works. The only difference is the addition of the binary flags. When using Sinatra I can set the binary flag on the write operation, but I'm not sure how I can set it on the read since I seem to be passed a file object by the request.
File.open('uploads/' + params['myfile'][:filename], "wb") do |f|
f.write(params['myfile'][:tempfile].read)
end
Okay, so it looks like all I needed was the binary flag when opening the new file.
File.open('uploads/' + params['myfile'][:filename], "wb") do |f|
f.write(params['myfile'][:tempfile].read)
end

How to read image metadata from a URL?

I want to read metadata of already uploaded JPEGs on S3. Is there a way to do that in Ruby without downloading the file locally?
The problem I am facing is that Image(Mini)Magick doesn't take a URL as a source (or at least I didn't find the right command).
Update:
This is working:
>> image = MiniMagick::Image.from_file -path_to_file-
>> image["EXIF:datetime"]
=> "2010:07:19 23:07:54"
But I didn't find a good substitude for "from_file", for URLs so something like:
>> image = MiniMagick::Image.from_url http://image_adress.com/image.jpg
doesn't work.
What about using open-uri?
require 'open-uri'
image = nil
open('http://image_adress.com/image.jpg') do |file|
image = MiniMagick::Image.from_blob(file.read) rescue nil
end
image["EXIF:datetime"] if image

Resources