开发者

Rails3: Render an in-memory image in the view

I have a rails app, which has a png file in memory in the controller which was generated by spark_pr.

@pngfile << Spark.plot( [47, 43, 24, 47, 16, 28, 38, 57, 50, 76, 42, 20, 98, 34, 53, 1, 55, 74, 63, 38, 31, 98, 89], :has_min => true, :has_max => true, 'has_last' => 'true', 'height' => '40', :step => 10, :normalize => 'logarithmic' )
@user=User.all.first

I would like to render this png file in my view, which is app/views/users/show.html.erb, and it has the standard stuff generated by the Rails scaffold generator where it displays various attributes of @user.

How can I render this in-memory file (@pngfile) in my view? I do not want to save the file, nor do I want to even provide a temporary URL for that file. I have seen this done on HTML pages with inline icons rendered from a binary blob, so I know it's possible. But I couldn't figure out the magic incantations with Rails view helpers that makes this possible.

----------- EDIT: More Information -------------------------

This site gives more information on how to embed URLs. The code for a folder icon looks like this:

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" 
width="16" height="14" alt="embedded folder icon">

I embedded this exact code in my view and it worked for Chrome, Firefox, and IE9.

So I tried doing this with my spark plot.

In my Rails controller, I did this:

    @pngfile = Base64.encode64 Spark.plot( [47, 43, 24, 47, 16, 28, 38, 57, 50, 76, 42, 20, 98, 34, 53, 1, 55, 74, 63, 38, 31, 98, 89], :has_min => true, :has_max => true, 'has_last' => 'true', 'height' => '40', :step => 10, :normalize => 'logarithmic' )

In my view I tried a few different tricks

<%=image开发者_如何学编程_tag "data:image/png;base64,"+@pngfile %>

<%=image_tag "data://image/png;base64,"+@pngfile %>

<img src=<%="data:image/png;base64,"+@pngfile %>  ></img>

<img src=<%=raw("data:image/png;base64,"+@pngfile) %>  ></img>

They all failed except the second one, and it worked in Chrome and Firefox, but not IE9. The first one should have worked, but the code of the image_tag helper prepends "/images/" to my URL by default even if it is an immediate data URL. I know this because I tried to view the image in a different tab, and it failed. But when I stripped off the "/images/" part, it worked fine. I have no idea why the second one worked. I just tried it on a whim. The third one failed because it looked like parts of the string were getting broken up because of special characters. I tried using a raw string in the fourth one, but it didn't work either.

At this point, I'm stumped. Do I try to monkey-patch image_tag so that it doesn't prepend stuff for data: URLs for embeddded images, or do I do something funky with the string so that it renders properly as with view examples 3 or 4?


I've just faced a similar problem. In my case, I defined base64_image in application_helper:

def base64_image image_data
  "<img src='data:image/png;base64,#{image_data}' />".html_safe
end

so in your case you'd just use this instead of image_tag:

base64_image(@png_file)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜