Monthly Archives: December 2017

How to stream an image using IDLnetURL

The IDnetURL object provides an interesting capability to stream data from a server into a buffer that can be loaded into a client application. I’ll present a simple example of how to stream a JPEG image from a remote website and embed it another web page without actually downloading the image into local storage. The key steps are as follows:

o=obj_new('IDLnetURL')                               ;-- initialize IDLnetURL
buffer = o->get(/buffer,url=url)                     ;-- read URL data into buffer stream (byte) array 
imgdata=idl_base64(buffer)                           ;-- encode stream to BASE64 
src='<img src="data:image/jpg;base64,'+imgdata+'"\>' ;-- insert as a data URI into HTML source tag

After initializing the IDLnetURL object, I call its get method with the /buffer keyword which reads the url (passed as a keyword) of the image into local memory as a byte array. The trick to getting a client to recognize the byte array is to use the idl_base4 function to convert the array into a MIME Base64 encoded scalar string. Once converted, I insert the encoded string as a data URI in a HTML source tag. The data URI syntax informs the client that the encoded string has a MIME type of image/jpg so that the client (e.g. browser) will render it correctly. For example, I can embed the URI into a simple HTML web page as follows:


As an exercise, try testing the above commands using the image URL:


The commands will create an HTML file called test_image.html. Open it in your preferred browser and you should see the image below:


I can get a performance boost in streaming speed if the remote image is compressed (e.g. gzip format). In this case, I use the zlib_uncompress function to uncompress the buffer before encoding as follows:

buffer = o->get(/buffer) 
src='<img src="data:image/jpg;base64,'+imgdata+'"\>'