Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to put image binary data into html?

Featured Replies

Hi to all! I need some help with putting image binary data into html.

var xmlhttp = getXmlHttp();
xmlhttp.open('GET', 'http://example.com/image.jpeg', true);
xmlhttp.onreadystatechange = function() {
 if (xmlhttp.readyState == 4) {
   if(xmlhttp.status == 200) {
   //Here, when AJAX request will be completed, i would like to put data conrained in xmlhttp.responseText into html and make browser correctly render it.
   }
 }
};
xmlhttp.send(null);

Any help is appreciated.

Embed image data into the HTML file? You'd have to base64 encode it. Though, I wonder if IE (surprise surprise) might have some issues there. Might have better luck saving it to a temp file.

  • Author

Embed image data into the HTML file? You'd have to base64 encode it. Though, I wonder if IE (surprise surprise) might have some issues there. Might have better luck saving it to a temp file.

Yeah...embed data into html. It seems to me that i can't exaclty find out the way to encode loaded data. Can you post some code example?

In JS - not from the top of my head. Try a google search.

 

Had it been PHP - there is a convenient function available. But first you should check if IE supports base64 images.

  • Author

I found the solution using PHP. Here it is.

Using this javascript we send the AJAX request to the php script that returns base64 encoded image binary data:

var xmlhttp = getXmlHttp();
xmlhttp.open('GET', 'http://example.com/script.php', true);
xmlhttp.onreadystatechange = function() {
 if (xmlhttp.readyState == 4) {
   if(xmlhttp.status == 200) {
   //Here we embed base64 encoded data into html
   document.getElementById('content').innerHTML = '<img src="data:image/jpeg; base64,' + xmlhttp.responseText + '" />';
   }
 }
};
xmlhttp.send(null);

Here script.php is:

<?php
echo base64_encode(file_get_contents('image.jpeg'));
?>

I also tried to use only JS, but it seems that functions that I had found, for example here http://www.webtoolkit.info/javascript-base64.html , don't encode correctly. Although may be I missed something.

  • Author

The JS library produce corrupted images?

Yes, we obtain corrupted images in result, but actually base64 encoded string ,returned from Base64.encode() method (link to js library above), is corrupted. I have compared string, returned from php function base64_encode, and string, returned from Base64.encode() method. They are not the same.

Are the JS method returning a larger string?

 

I wonder if some character encoding errors might occur? Maybe some characters are treated as multibyte...

  • 6 years later...

I load almost all my images via ajax - also known as lazy loading where you load in image as the user scrolls towards their location in the document. it makes a massive difference to server load on high traffic sites, or those with a lot of visual content. It also helps reduce page load for users, saves critical bandwidth for users on fixed data-plans.

 

Although the OP's method seems a very weird way to do things. I just put the image source in a data-attribute, populate the actual src attribute with a blank gif. You can use multiple data-attributes if you want responsive images- i.e. different images served depending on the viewport width.

 

I'd advise using jQuery for Ajax, it saves a lot of headaches, the DOM API is probably the worst API ever created and it's nice to abstract mess away. If you're worried about filesize then I argue that you are likely to get far better performance gains optimising elsewhere. -- This is what Crockford advises in the vast majority of cases.

 

You can simply ajax in an image like this:

el.attr('src', src);

Where el is the image, and src is the value pulled from the data attribute.

 

I wrote about this more extensively here: http://rbrtsmith.com/2015/02/building-a-high-performance-lazy-load-module/

Edited by rbrtsmith

  • 1 month later...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.