如何在凤凰城渲染透明的gif像素?

owy*_*gsk 7 elixir phoenix-framework

来自Rails背景,我希望我能渲染一个透明的像素gif.要简单地在Rails上执行此操作

gif_data = "GIF89a\x01\x00\x01\x00\x80\xFF\x00\xC0\xC0\xC0\x00\x00\x00!\xF9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x01\x012\x00;"
respond_to do |format|
  format.gif { render text: gif_data, content_type: "image/gif" }
end
Run Code Online (Sandbox Code Playgroud)

会比我自己更清楚地做到这一点.

owy*_*gsk 6

经过一些谷歌搜索和学习字符串编码.我转换了实际透明gif的Hex代码

47 49 46 38 39 61 01 00 01 00 80 00 00 00 00 00 FF FF FF 21 F9 04 01 00 00 00 00 2C 00 00 00 00 01 00 01 00 00 02 01 44 00 3B
Run Code Online (Sandbox Code Playgroud)

使用Hexate包进入此

<<71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 0, 0, 0, 0, 0, 255, 255, 255, 33, 249, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 1, 68, 0, 59>>
Run Code Online (Sandbox Code Playgroud)

所以我可以在凤凰控制器中使用它

gif_data = <<71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 0, 0, 0, 0, 0, 255, 255, 255, 33, 249, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 1, 68, 0, 59>>
conn = put_resp_content_type(conn, "image/gif")
text conn, gif_data
Run Code Online (Sandbox Code Playgroud)

  • 而不是`text/2`,调用`send_resp(conn,200,git_data)`.最后的结果相同,但后者在语义上是正确的.:) (8认同)