Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Convert Image to Data URI with JavaScript
JavaScript provides several methods to convert images to Data URI (Base64) format. A Data URI is a string representation of an image that can be embedded directly in HTML or CSS. This tutorial covers two main approaches: using Canvas API for image URLs and FileReader API for local files.
Method 1: Converting Image URL to Data URI Using Canvas
This method uses the HTML5 Canvas API to convert an image from a URL to a Base64 Data URI string.
<!DOCTYPE html>
<html>
<head>
<title>Convert Image URL to Data URI</title>
</head>
<body>
<img src="https://via.placeholder.com/150" id="myImg" alt="Sample Image">
<button onclick="convertImage()">Convert to Data URI</button>
<div id="output"></div>
<script>
function toDataURL(src, callback) {
var image = new Image();
image.crossOrigin = 'Anonymous';
image.onload = function() {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.height = this.naturalHeight;
canvas.width = this.naturalWidth;
context.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL('image/png');
callback(dataURL);
};
image.src = src;
}
function convertImage() {
toDataURL('https://via.placeholder.com/150', function(dataURL) {
document.getElementById('output').innerHTML =
'<p>Data URI: <code>' + dataURL.substring(0, 50) + '...</code></p>';
console.log('Full Data URI:', dataURL);
});
}
</script>
</body>
</html>
Method 2: Converting Local File to Data URI Using FileReader
For local files selected by users, use the FileReader API to convert them to Data URI format.
<!DOCTYPE html>
<html>
<head>
<title>Convert Local File to Data URI</title>
</head>
<body>
<input type="file" id="fileInput" accept="image/*">
<div id="preview"></div>
<script>
document.getElementById('fileInput').addEventListener('change', function(event) {
var file = event.target.files[0];
if (file && file.type.startsWith('image/')) {
var reader = new FileReader();
reader.onload = function(e) {
var dataURL = e.target.result;
document.getElementById('preview').innerHTML =
'<img src="' + dataURL + '" style="max-width: 200px;">' +
'<p>Data URI length: ' + dataURL.length + ' characters</p>';
};
reader.readAsDataURL(file);
}
});
</script>
</body>
</html>
Key Points
-
CORS Issues: Set
crossOrigin = 'Anonymous'for external images -
Format Options: Use
toDataURL('image/png')ortoDataURL('image/jpeg') - Size Limitation: Data URIs can become very large for high-resolution images
- Browser Support: Both methods work in all modern browsers
Comparison
| Method | Use Case | API | CORS Considerations |
|---|---|---|---|
| Canvas + toDataURL() | Image URLs | Canvas API | Required for external images |
| FileReader | Local files | File API | Not applicable |
Conclusion
Use Canvas API with toDataURL() for converting image URLs to Data URI, and FileReader API for local files. Both methods produce Base64-encoded strings that can be used directly in HTML or stored for later use.
Advertisements
