I would like to display an image (PNG) generated in controller, which is not saved in a file. Is it possible to pass such generated image data to view? I did a bit of googling, but not found anything on this idea. A similar solution is provided here: Invalid encoding with rqrcode, saving image in temporary file and assigning it back, but I am trying to avoid this extra step.
I tried to use send_data disposition: 'inline' but I am stuck how to use this object in my view code.
Take a look at data URIs. That might solve what you're looking for.
The wikipedia article even has an example for PNGs.
It's possible to load an image's data inline using Data URIs and Base 64 encoding:
<img src="data:image/png;base64,/<%= #image_data_b64 %>" />
You can then use ActiveSupport::Base64.encode64 to encode the image data:
class ImageController < ApplicationController
def index
raw_data = # Code to get image data as string
#image_data_b64 = ActiveSupport::Base64.encode64(raw_data)
end
end
Related
So I was checking on how to display PDF thumbnails in Rails since for some reason creating a thumbnail version of my file in my uploader doesn't work, and it lead me to this:
Convert a .doc or .pdf to an image and display a thumbnail in Ruby?
So I got up to this:
def show_thumbnail
require 'rmagick'
pdf = Magick::ImageList.new(self.pdf_file.file.path)
first_page = pdf.first
scaled_page = first_page.scale(300, 450)
end
But how do I display scaled_page to a webpage?
I added this function in the decorator so I can do something like this:
= image_tag(pdf.pdf_file.show_thumbnail)
But it results in this error:
Can't resolve image into URL: undefined method `to_model' for #<Magick::Image:0x0000000122c4b300>
To display the image, the browser only need a URL to the image. If you don't want to store the image on your HDD, you can encode the image into a data URL.
...
scaled_page = first_page.scale(300, 450)
# Set the image format to png so that we can quantize it to reduce its size
scaled_page.format('png')
# Quantize the image
scaled_page = scaled_page.quantize
# A blob is just a binary string
blob = scaled_page.to_blob
# Base64 encode the blob and remove all line feeds
base64 = Base64.encode64(blob).tr("\n", "")
data_url = "data:image/png;base64,#{base64}"
# You need to find a way to send the data URL to the browser,
# e.g. `<%= image_tag data_url %>`
But I highly recommend that you persist the thumbnails on your HDD or better on a CDN because such images are hard to generate but are frequently accessed by the browsers. If you decided to do so, you need a strategy to generate unique URLs to those thumbnails and a way to associate the URLs to your PDF files.
The actual content of this site is an image (http://cf.ccdn.es/e_1/CB34A36F787E8E77C4F29B7B8366CA975623AADB85F9FFBD74144AAF871C0DDCEA11C7A4C38903CDA63D620BB6190F68), but since it doesn't seem to be formatted like one I have problems handling it in my Rails application; I interprets the page as html instead of an image because of its MIME type. How could I change this so it will get recognized correctly?
I know that I can do something like this:
MIME::Types.type_for('jpg').first
which will give me the correct type, but how do I implement it in a way that it will overwrite the existing one when I request it with Mechanize?
If this is a little unclear: How can the above "image" be converted to something that gets recognized as an image?
In your controller:
send_file [image location], type: "image/jpeg", disposition: "inline"
I gonna upload files to rackspace(video, audio and images) in rails with paperclip or carrierwave, I need to Know the kind of file, to show in the view with image_tag, or video_tag or audio_tag, rackspace tell me the kind of file? or I have to store in my database? thanks
You can query/set the file type by using the 'content_type' function located in the 'ruby-cloudfiles' library.
See here: https://github.com/rackerlabs/ruby-cloudfiles/blob/master/lib/cloudfiles/storage_object.rb#L80-L82
Something like this should work for creating the object:
container = conn.create_container('new_container')
obj = container.create_object('new_obj.txt')
obj.load_from_filename('./obj.txt')
obj.content_type = 'text/plain'
And to retrieve the object:
obj = container.object('new_obj.txt')
puts obj.content_type # text/plain
Even if rackspace would tell you the file type, you don't want it to, since it would take so long to run roundtrips from your server to theirs.
My code examples below assume carrierwave, but I'm sure paperclip has similar options. Two options:
Interpret the file extension
Something like: File.extname(user.avatar), which you then have to interpret however you like.
Record & interpret the mime type.
The carrierwave readme explains how to get carrierwave to calculate it in the first place, and then you should probably store it to your database manually or using carrierwave-meta. Then user.avatar.content_type would be something like image/jpeg which you could easily interpret as a particular file type.
I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.
I have the binary data for an image in my controller action. I need to display the image in the view. How can I do this? I'm using Ruby 1.9.2 and Rails 3.0.1.
As coreyward above notes, it's not ideal to be doing this at all. But if you don't have an alternative (really, you probably do), you should look into data URIs.
It depends on what format your binary data is in, but the short version is that you will convert the data to a Base64-encoded string and then build a special URI that starts with data:, followed by the appropriate MIME type, and then the base64 string, and use that as the src attribute in a normal <img /> tag.