Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is possible to read image from remote server using in binary mode using javascript or phonegap?

Actually in one of my project i need to read images from remote server and store in local database as binary to use in application later. so is there any simple way that i can do this? it's only thing in which i stuck and it is important to complete application. please help !! Thanks in advance.

like image 497
Shailesh Thanki Avatar asked Mar 23 '12 14:03

Shailesh Thanki


1 Answers

It is rather straightforward in HTML5/ES5 environment (practically everything except Internet Explorer 9-);

First you need to download the binary content of the image into an arraybuffer, then convert it to base64 datauri, that is actually a string. This can be stored in the browsers localStorage, indexedDb or webSQL, or even a cookie (not too efficient though); Later on you just set this datauri as the image src to display.

<script>
    function showImage(imgAddress) {
        var img = document.createElement("img");
        document.body.appendChild(img);
        getImageAsBase64(imgAddress, function (base64data) { img.src = base64data; });
    };

    function getImageAsBase64(imgAddress, onready) {
        //get from online or from whatever string store
        var req = new XMLHttpRequest();
        req.open("GET", imgAddress, true);
        req.responseType = 'arraybuffer'; //this won't work with sync requests in FF
        req.onload = function () { onready(arrayBufferToDataUri(req.response)); };
        req.send(null);
    };

    function arrayBufferToDataUri(arrayBuffer) {
        var base64 = '',
            encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
            bytes = new Uint8Array(arrayBuffer), byteLength = bytes.byteLength,
            byteRemainder = byteLength % 3, mainLength = byteLength - byteRemainder,
            a, b, c, d, chunk;

        for (var i = 0; i < mainLength; i = i + 3) {
            chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
            a = (chunk & 16515072) >> 18; b = (chunk & 258048) >> 12;
            c = (chunk & 4032) >> 6; d = chunk & 63;
            base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d];
        }

        if (byteRemainder == 1) {
            chunk = bytes[mainLength];
            a = (chunk & 252) >> 2;
            b = (chunk & 3) << 4;
            base64 += encodings[a] + encodings[b] + '==';
        } else if (byteRemainder == 2) {
            chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
            a = (chunk & 16128) >> 8;
            b = (chunk & 1008) >> 4;
            c = (chunk & 15) << 2;
            base64 += encodings[a] + encodings[b] + encodings[c] + '=';
        }
        return "data:image/jpeg;base64," + base64;
    }

 </script>

I borrowed the base64 conversion routine from this fine post: http://jsperf.com/encoding-xhr-image-data/5

like image 138
Peter Aron Zentai Avatar answered Oct 05 '22 17:10

Peter Aron Zentai