Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse base64 image repeatedly in HTML file

I have an HTML file that will be portable to distribute to clients. It has an index of documents. I want a PDF or Word icon for each document but I don't want to repeat the base64 string 30 times if I can avoid it. How should I do this? Javascript is okay, but my goals are simplicity and portability.

like image 627
THE JOATMON Avatar asked Jun 24 '16 13:06

THE JOATMON


People also ask

Does browser cache Base64 images?

Browsers can cache downloaded files. If the Base64 string is in a text (or JSON) file then it can be cached.

Does Base64 encoding reduce image quality?

With the introduction of multiplexing that arrived with HTTP/2, web browsers have become incredibly efficient in delivering hundreds of files through a single connection. This works around most limits that the Base64 encoding solved and in fact means Base64 now does more bad than good.

Why Base64 is needed to transfer images?

Base64 is only useful for very small images. This is because when it comes to larger images, the encoded size of a picture in bytes will end up being much larger than JPEGs or PNG files.

How long is Base64 image?

It will however be larger than the source file. A ballpark figure is around 270 000 characters.


4 Answers

You can use a CSS variable.

In the <head> section of the page, name and define the variable and a class to use it:

:root {
    --img-123: url(data:null;base64,iVBORw………); /* your image data here */
}

.img-123 {
    background-blend-mode: normal;
    background-clip: content-box;
    background-position: 50% 50%;
    background-color: rgba(0,0,0,0);
    background-image:var(--img-123);
    background-size: 100% 100%;
    background-origin: content-box;
    background-repeat: no-repeat;
}

In the <body> section of the page, use the image repeatedly via element style section or CSS classes. The SVG data creates a transparent image through which to see the content.

<img 
  class="img-123" 
  src="data:image/svg+xml,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot;&gt;&lt;rect fill-opacity=&quot;0&quot;/&gt;&lt;/svg&gt;"
>

I observed this technique in the output of the open source Firefox / Chrome extension SingleFile. There might be ways to simplify the CSS and to avoid using SVG.

like image 28
tanius Avatar answered Oct 06 '22 19:10

tanius


Try using CSS class

img.word-icon {
  content: url(data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8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B776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==);
  width: 32px;  
  height: 32px;
}
<html>
  <body>  
    <img class="word-icon">foo
    <img class="word-icon">bar
    <img class="word-icon">
  </body>
</html>
like image 199
ryanafrish7 Avatar answered Oct 06 '22 18:10

ryanafrish7


The other answer using CSS gives the right idea, but it is not compatible with all browsers. The content property of the CSS is only allowed in the pseudo-elements ::before and ::after. The img tag with a class with a content property is empty. The browsers have no obligation to display it. Chrome displays it nevertheless (incorrectly).

To make this work for all browsers, use for example the ::before on an empty span:

span.word-icon::before {
  content: url(data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ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CZbEhAAOw==);
}
<html>
  <body>  
    <p><span class="word-icon"></span>Paragraph with icon.</p>
  </body>
</html>

You can also attach it directly to p::before.

like image 22
MartinTeeVarga Avatar answered Oct 06 '22 19:10

MartinTeeVarga


create a script to insert it into the images. I purposely put the script code inline since you're sending a single HTML file:

<img alt="pdf"/>
<img alt="pdf"/>
<img alt="pdf"/>
<img alt="pdf"/>
<img alt="pdf"/>
<img alt="pdf"/>
<script>
  var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++){
  images[i].src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADmklEQVRYhc2VSWgTURjHgycFPVsEtQdRXE96UFDxZHtRXFDRClrxICoIIl7EhWo9eFDEi+jFBRFxqRUFccEusY11abSKjSa2WZqmbZaZzGRt+/d9r5nxzSSpk9iCD34Mb/Le/P7vmy+Jzfafj6l+vx+Ez+fT0e4FAgH09fUhGAyiv7+fEwqFMDAwgMHBQY7T6VzPnjOl3AAVotzr9eoBSG4OQHIxwNDQELq7u4lzRQ3RShsmAwpEAXp6enhQj8dzpmCAMFssr5g5odAzqVIUgK7xeJxXjVXwdF6A0FwbzGRZWZOtLYgcOzI2D/Yh9dmJcG0Nnw+zMqe+OKE8fpS3V4NOHg6HeYBUKgVJkngl2H1jJYJssZm0x43Q5mok3znG5r88CK5cAvX1yz/zAvtEtABut1vvIbrncrnoeldvTP8cG8xkWRMpTW8QOrCfz0lI19TPH2Ofs9ImqSKXL+bt1SBZJBJBNBpFLBbjUBVkWeYw9XQeoJctNpNiFTDMWQDfupVQP37Q54X2GejtLSqnnmDqWTyAe7YNZhLfvhrmadYDSucneGu28Xmy+3veHjP0lS0mVxSFAlTyAC62eDKgd15MbgjwnS2eDOhrV0iuqipHD8DGbMb8Iqxrbm5GU1OTTktLC6e1tRV2ux1tbW1ob2/nOBwOdHR0oKuri/8iamKzPJFIGAJMY8wowmJqJjP0fjUK/UeQnBpQLLkoTyaThgDjjUp6kBmtuQ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