Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a transparent gif pixel in Phoenix?

Coming from a Rails background I was hoping I could render a transparent pixel gif. To do this on Rails I simply

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

Would appreciate some cleaner way to do it than my own.

like image 511
owyongsk Avatar asked Oct 18 '15 06:10

owyongsk


1 Answers

After some googling and learning string encodings. I converted the Hex code of the actual transparent gif

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

using Hexate package into this

<<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>>

so to use this in a Phoenix Controller I can

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
like image 99
owyongsk Avatar answered Nov 21 '22 12:11

owyongsk