I want to change background dynamically in JS and my set of images is in base64 encoded. I try:
document.getElementById("bg_image").style.backgroundImage =
"url('http://amigo.com/300107-2853.jpg')";
with perfect result,
yet I fail to do the same with:
document.getElementById("bg_image").style.backgroundImage =
"url('data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...')";
nor
document.getElementById("bg_image").style.backgroundImage =
"data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...";
Is there any way to do it?
This question is related to
javascript
base64
background-image
Adding this trick to gabriel garcia's following solution -
var img = 'data:image/png;base64, ...'; //place ur base64 encoded img here
document.body.style.backgroundImage = 'url(' + img + ')';
However, in my case this wasn't working. Moving url into the img variable did the trick. SO the final code looked like this
var img = "url('data:image/png;base64, "+base64Data + "')";
document.body.style.backgroundImage = img;
This is the correct way to make a pure call. No CSS.
<div style='background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAFCAYAAABW1IzHAAAAHklEQVQokWNgGPaAkZHxPyMj439sYrSQo51PBgsAALa0ECF30JSdAAAAAElFTkSuQmCC)repeat-x center;'></div>
Had the same problem with base64. For anyone in the future with the same problem:
url = "data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...";
This would work executed from console, but not from within a script:
$img.css("background-image", "url('" + url + "')");
But after playing with it a bit, I came up with this:
var img = new Image();
img.src = url;
$img.css("background-image", "url('" + img.src + "')");
No idea why it works with a proxy image, but it does. Tested on Firefox Dev 37 and Chrome 40.
Hope it helps someone.
EDIT
Investigated a little bit further. It appears that sometimes base64 encoding (at least in my case) breaks with CSS because of line breaks present in the encoded value (in my case value was generated dynamically by ActionScript).
Simply using e.g.:
$img.css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");
works too, and even seems to be faster by a few ms than using a proxy image.
What I usually do, is to store the full string into a variable first, like so:
<?php
$img_id = 'data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAY...';
?>
Then, where I want either JS to do something with that variable:
<script type="text/javascript">
document.getElementById("img_id").backgroundImage="url('<?php echo $img_id; ?>')";
</script>
You could reference the same variable via PHP directly using something like:
<img src="<?php echo $img_id; ?>">
Works for me ;)
In my case, it was just because I didn't set the height
and width
.
But there is another issue.
The background image could be removed using
element.style.backgroundImage=""
but couldn't be set using
element.style.backgroundImage="some base64 data"
Jquery works fine.
I think this will help, Base64 is addressed by CSS in a different way, you should set the data type of the image to base64, this will help the CSS to change the base64 to image and display it to the user. and you can use this from javascript by assigning the background image using the jquery script, it will automatically change the base64 to mage and display it
url = "data:image;base64,"+data.replace(/(\r\n|\n|\r)/gm, "");_x000D_
$("body").css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");
_x000D_
Try this, I have got success response ..it's working
$("#divId").css("background-image", "url('data:image/png;base64," + base64String + "')");
I tried this (and worked for me):
var img = 'data:image/png;base64, ...'; //place ur base64 encoded img here
document.body.style.backgroundImage = 'url(\'' + img + '\')';
ES6 syntax:
let img = 'data:image/png;base64, ...'
document.body.style.backgroundImage = `url('${img}')`
A bit better:
let setBackground = src => {
this.style.backgroundImage = `url('${src}')`
};
let node = nodeIGotFromDOM, img = imageBase64EncodedFromMyGF;
setBackground.call(node, img);
Source: Stackoverflow.com