Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Google Chrome not show base64 encoded images?

Firefox, Internet Explorer, Opera accept following code:

<html>
<head>
<style type="text/css">
.minus
{
    width: 11px;
    height: 11px;
    background-image: url("data:image/gif;base64,R0lGODlhCwALAIABAAAAAP///yH5BAEAAAEALAAAAAALAAsAAAIUhI8Wy6zdHlxyqnTBdHqHCoERlhQAOw");
}
</style>
</head>
<body>
<div class="minus">
</body>
</html>

Chrome (Version 19.0.1084.56) does not. Why?

like image 481
Jens Haberer Avatar asked Jun 16 '12 22:06

Jens Haberer


3 Answers

This is a bug in Chrome, see bug #105725. The base64-string has to be padded. The following solution works fine: http://jsfiddle.net/TunfH/ (I have added == at the end).

<html>
<head>
<style type="text/css">
.minus
{
    width: 11px;
    height: 11px;
    background-image: url("data:image/gif;base64,R0lGODlhCwALAIABAAAAAP///yH5BAEAAAEALAAAAAALAAsAAAIUhI8Wy6zdHlxyqnTBdHqHCoERlhQAOw==");
}
</style>
</head>
<body>
<div class="minus"></div>
</body>
</html>
like image 88
MMK Avatar answered Sep 21 '22 07:09

MMK


Your base64 data is invalid, I believe you meant

data:image/gif;base64,R0lGODlhCwALAIABAAAAAP///yH5BAEAAAEALAAAAAALAAsAAAIUhI8Wy6zdHlxyqnTBdHqHCoERlhQAOw==

Which seems to work just fine in Chrome and Firefox [ what I have access to ]

I am guessing that Chrome has a slightly more strict base64 implementation and requires the padding.

like image 38
Dre Avatar answered Sep 18 '22 07:09

Dre


Your problem is that you have "//" which you need to "escape" in order to avoid the err_invalid_url warning you see in the console

So take your:

R0lGODlhCwALAIABAAAAAP///yH5BAEAAAEALAAAAAALAAsAAAIUhI8Wy6zdHlxyqnTBdHqHCoERlhQAOw

And convert it to

R0lGODlhCwALAIABAAAAAP/%0a/%0a/yH5BAEAAAEALAAAAAALAAsAAAIUhI8Wy6zdHlxyqnTBdHqHCoERlhQAOw

And the it will show properly

The same solution should be used if the image starts with a "/" replace it with "%0a/"

like image 38
Noam Rathaus Avatar answered Sep 20 '22 07:09

Noam Rathaus