2

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 2

5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

the image is not base64 encoded, so i tried to leave the ";base64" out, but nevertheless the image doesnt show...
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?
its the contents of the image file starting with ?PNG IHDR (...)
How exactly do you get the raw data?
i get it as response from a REST-Server using ajax
|
0

Have you tried (with jQuery):

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

From Switching a DIV background image with jQuery

1 Comment

put the URL of the server in the css if that server is accessible by the client of course

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.