HTML/CSS: Background Image from raw image data, not url

孤者浪人 提交于 2020-01-05 12:10:33

问题


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?


回答1:


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.




回答2:


Have you tried (with jQuery):

$('#divID').css("background-image", "url(/myimage.png)");

From Switching a DIV background image with jQuery



来源:https://stackoverflow.com/questions/11027186/html-css-background-image-from-raw-image-data-not-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!