How to Store an Image using Local Storage of HTML5?

html5-web

To improve performance of a web application we need to reduce server & client communication as much as we can. This technique helps to improve performance of a web application. Always it’s wise for a programmer to store static data in client browser. Except for the first time but every time while user will request the same page from the serve we need to check whether the similar data exists in the client memory or not. If the similar data exist in the client memory we need to load this data to reduce server & client communication. In this way to achieve this we need to store our static data in the client browser memory. To make this process easier HTML5 introduced Local Storage. Which stores data in client browser for the unlimited time we want. Compare to session storage local storage stored data after user interaction. I can say Local Storage is like a off-line data store.

During web application development we do integrate many images to our application to make our application more user attractive. Here images are static data. If the user is requesting the similar page 10 times from his/her browser we need to avoid server interaction for nine times rather then for the first one. So in this session let us know how to store an image in Local storage.

In the below example I am storing an image to HTML5 local storage. In HTML body here I have 2 images imageToLoadInLocalStorage & imageToShowFromLocalStorage. The first image has a src attribute which loads a png image (global-img.png) from server. Second image is having blank src attribute. Using client script here I am storing the first image to local storage & fetching the similar image from local storage to my second image. The mechanism I used here is I am fetching the first image using DOM object method getElementById. Converting the image to Base 64 data url format & storing it to local storage using localStorage.setItem() method. Then fetching the image from local storage using localStorage.getItem() method & assign the image to my second image control.

To run the below example copy the codes into a notepad file. Save it as HTML. Then open the file using any browser which supports HTML5 storage. Before run the below app make sure you are with internet connectivity.

Example to Store an Image using HTML5 Local Storage

<!DOCTYPE html>
<html>
<head>
<title>How to Store an Image using Local Storage of HTML5?</title>
<!--CDN link of Jquery-->
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
/* Function to Convert image into Base64 */
function ConvertImageToBase64(imageToConvertBase64) {
/* Creating a Canvas in HTML5 */
var drawCanvas = document.createElement("canvas");
/* Setting the Canvas width & height as refer to the Image */
drawCanvas.width = imageToConvertBase64.width;
drawCanvas.height = imageToConvertBase64.height;
/* Copy the image to Canvas */
var copyImageToCanvas = drawCanvas.getContext("2d");
copyImageToCanvas.drawImage(imageToConvertBase64, 0, 0);
/* Converting Image to Data URL */
var dataUrlOfImage = drawCanvas.toDataURL("image/png");
/* Returning Data URL of the Image */
return dataUrlOfImage.replace(/^data:image\/(png|jpg);base64,/, "");
}

$(document).ready(function() {
/* Fetching the image using JavaScript getElementById */
var imageToLoadInLocalStorage = document.getElementById('imageToLoadInLocalStorage');
/* Calling the function ConvertImageToBase64 to convert the Image to Base 64 */
var imageDataUrl = ConvertImageToBase64(imageToLoadInLocalStorage);
/* Storing the image in Local Storage using setItem() method */
localStorage.setItem("imageData", imageDataUrl);

/* Using getItem() fetching the image from Local Storage */
var dataImageUrl = localStorage.getItem('imageData');
/* Using getElementById to detect the image control for loading local storage image */
var imageToShow = document.getElementById('imageToShowFromLocalStorage');
/* Assigning source to a blank image with id imageToShowFromLocalStorage */
imageToShow.src = "data:image/png;base64," + dataImageUrl;
});
</script>
</head>
<body>
<!--Image with Source-->
<img src="global-img.png" id="imageToLoadInLocalStorage" />
<!--Image with no Source to grab from Local Storage-->
<img src="" id="imageToShowFromLocalStorage" />
</body>
</html>

Source of Image: http://www.namasteui.com