Is there a way to set background-image as a base64 encoded image?

后端 未结 9 2229
执念已碎
执念已碎 2020-12-02 16:09

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 =         


        
相关标签:
9条回答
  • 2020-12-02 16:53

    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>
    
    0 讨论(0)
  • 2020-12-02 16:55

    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; 
    
    0 讨论(0)
  • 2020-12-02 16:57

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

    0 讨论(0)
  • 2020-12-02 16:58

    I tried to do the same as you, but apparently the backgroundImage doesn't work with encoded data. As an alternative, I suggest to use CSS classes and the change between those classes.

    If you are generating the data "on the fly" you can load the CSS files dynamically.

    CSS:

    .backgroundA {
        background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2V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WFhYWFhYWFhYWFhYWFhYWFhYWFhYW1qsrwABYuwNkimqm3gAAAABJRU5ErkJggg==");
    }
    
    .backgroundB {
        background-image:url("data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPbWLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVrApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KTkpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxMAF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=");
    }
    

    HTML:

    <div id="test" height="20px" class="backgroundA"> 
      div test 1
    </div>
    <div id="test2" name="test2" height="20px" class="backgroundB">
      div test2
    </div>
    <input type="button" id="btn" />
    

    Javascript:

    function change() {
        if (document.getElementById("test").className =="backgroundA") {
            document.getElementById("test").className="backgroundB";
            document.getElementById("test2").className="backgroundA";
        } else {
            document.getElementById("test").className="backgroundA";
            document.getElementById("test2").className="backgroundB";
        }
    }
    
    btn.onclick= change;
    

    I fiddled it here, press the button and it will switch the divs' backgrounds: http://jsfiddle.net/egorbatik/fFQC6/

    0 讨论(0)
  • 2020-12-02 16:58

    Try this, I have got success response ..it's working

    $("#divId").css("background-image", "url('data:image/png;base64," + base64String + "')");
    
    0 讨论(0)
  • 2020-12-02 16:59

    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, "");
    $("body").css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");

    0 讨论(0)
提交回复
热议问题