1

if I have something like

<img src="x.jpg" id=i>

is there away to access the binary using something like getElementById("i").something ?

6
  • 2
    You can do it with the canvas API, but only if you're loading the image from the same domain as the enclosing page. Commented Aug 29, 2015 at 19:28
  • 1
    If it's same-origin you can paint it in a canvas and get the pixel data, but I'm not sure about binary data. Commented Aug 29, 2015 at 19:29
  • 2
    stackoverflow.com/questions/934012/get-image-data-in-javascript Commented Aug 29, 2015 at 19:32
  • 1
    As Oriol said. If you have pixel data (in canvas) these are 8bit numbers, you can access them as binary. Why? Commented Aug 29, 2015 at 19:33
  • You might be able to do it by sending an additional XMLHTTPRequest. You SHOULD AFAIK get the "binary" back. Commented Aug 29, 2015 at 19:39

1 Answer 1

1

From an MDN demo:

They show in the below example how to fetch an image using an XHR request wrapped in a Promise. (you don't really need the promise part, but it's cool).

Just make sure the images are on the same domain which this code runs on, or wrap with JSONP.

And to your question, you could iterate on all the images on the page, and run the imgLoad function with their src, to get the data. I can't think of a way to access the data of an image that was already downloaded through the browser's own mechanism (src='...')

function imgLoad(url) {
    // Create new promise with the Promise() constructor;
    // This has as its argument a function
    // with two parameters, resolve and reject
    return new Promise(function(resolve, reject) {
      // Standard XHR to load an image
      var request = new XMLHttpRequest();
      request.open('GET', url);
      request.responseType = 'blob';

      // When the request loads, check whether it was successful
      request.onload = function() {
        if (request.status === 200) {
        // If successful, resolve the promise by passing back the request response
          resolve(request.response);
        } else {
        // If it fails, reject the promise with a error message
          reject(Error('Error code:' + request.statusText));
        }
      };

      request.onerror = function() {
      // Also deal with the case when the entire request fails to begin with
      // This is probably a network error, so reject the promise with a message
          reject(Error('There was a network error.'));
      };

      // Send the request
      request.send();
    });
}

// Get a reference to the body element, and create a new image object
var body = document.querySelector('body');
var myImage = new Image();

// Call the function with the URL we want to load, but then chain the
// promise then() method on to the end of it. This contains two callbacks
imgLoad('myLittleVader.jpg')
    .then(function(response) {
        // The first runs when the promise resolves, with the request.reponse
        // specified within the resolve() method.
        var imageURL = window.URL.createObjectURL(response);
        myImage.src = imageURL;
        body.appendChild(myImage);
        // The second runs when the promise
        // is rejected, and logs the Error specified with the reject() method.
    }, 
    function(Error) {
        console.log(Error);
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.