i get the raw image data of a png-image from a webserver, and want to use it as the background image of a div-element. is this possible without saving the data as an image-file first?
2 Answers
Use a data URI:
document.getElementById('e').src = 'data:image/png;base64,ton_of_crap';
In that Wikipedia article, there is code for PHP and Python that outputs base-64 encoded images. You can just shove that into the url() of a CSS file (or the src attribute of a <img /> tag) and it should work.
8 Comments
user1448982
the image is not base64 encoded, so i tried to leave the ";base64" out, but nevertheless the image doesnt show...
Blender
It needs to be base64-encoded. By "raw-data" what do you mean? Like the contents of the image file, or a special URL that shows you a generated image?
user1448982
its the contents of the image file starting with ?PNG IHDR (...)
Blender
How exactly do you get the raw data?
user1448982
i get it as response from a REST-Server using ajax
|
Have you tried (with jQuery):
$('#divID').css("background-image", "url(/myimage.png)");
1 Comment
Memos Electron
put the URL of the server in the css if that server is accessible by the client of course