views:

39

answers:

2

In a my project in Ruby On Rails (but this is not important), when I load a html page, I need to decrypt an image (jpg) and show it in the web page: after the page request, the image is decrypted an the file is write on the server disk, so the browser can show the image.

I don't want to write the image on the server, but I want to encrypt the image on the fly !

I think that ajax (jquery) could be helpfull, but I don't know how use it to the image on the fly (without write the image on the disk).

Do you have any Idea ?

thank you, Alessandro

+2  A: 

The only way to do this is the data URI scheme.

It allows you to embed the image data directly as the src property of the <img> element .

However, it has its limitations: It doesn't work at all in IE < 8, and has a 32k size limit in IE 8.

Pekka
This is an interesting solutions, but I am working win images > 32k, thank you, alexdesi
+2  A: 

You can use send_data instead of rendering a page. Take a look here. With this you can generate your file and send it without saveing to disk. However normal page won't be rendered. But you can use it like this:

1.Create normal html view, on example for index action:

# controller
def index
  @some_resources = SomeResources.all # just whatever you need
end

# somewhere in index.html.erb view
<img src="rendered_images/my_rendered_image.jpg" />

2.Add route for rendered_images

# routes
match "rendered_images/:filename" => "application#render_image"

3.Add render image action in application controller

# application controller
def render_image
  send_data my_function_to_generate_file, :filename => params[:filename], :type => 'image/jpeg', :disposition => :inline
end

I haven't tested it. I hope it will give you something to start with!

klew
+1, but would it not be "better" to create an image controller and use it's show method.
Noel Walters
@Noel, probably yes, I just wanted to give an idea how to do it. Probably he also need to somehow tell which image was 'decrypted' (whatever it means) and also there may be some access restrictions for different users and so on.
klew
@klew, Thank you for your indications, It work ! some little corrections: "map.connect" instead of "match", and :disposition => 'inline' (in ROR 2.3.8 is required string instead of symbol, strange but true),Thank You, Alexdesi
`match` is in Rails 3, of course routes for earlier versions of Rails are a little bit different.
klew