• Đăng ký
  • Đăng nhập

Code chuyển đổi hình ảnh sang chuỗi data base64

📅 — 👀 1298 — 👦

Trang này có hữu ích không?

👍 Có (17)        👎 Không (17)


Khi thiết kế website, chúng ta có thể tối ưu hóa website bằng nhiều cách, một trong cách đó theo mình thấy là chuyển đổi dữ liệu hình ảnh như logo, icon,... (nói chung là các hình ảnh thường dùng trong website) thành chuỗi data base64. Mời các bạn xem và tìm hiểu về tính năng này bên dưới.

Code chuyển đổi hình ảnh sang chuỗi data base64 - Hình 2

 

(Đây là hình ảnh đã được mã hóa)

Đây là data base64 của hình ảnh bên trên:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4AJkFkb2JlAGTAAAAAAQMAFQQDBgoNAAAH4wAAEBYAABmuAAAoVP/bAIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxscHx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f/8IAEQgAVgEvAwERAAIRAQMRAf/EAOoAAQACAwEBAAAAAAAAAAAAAAADBgIEBQcBAQEBAQEBAAAAAAAAAAAAAAAAAgEDBBAAAQQBAwIEBgIDAQAAAAAAAgEDBAUAEDEGIEERIRMUMGASNBU1UDIiJBY2EQACAAQDBQQHBgMGBwEAAAABAgAREgMhMQRBUWEiExBxsTKBkVJyIxQFIKFCgjNzwWKyUPGiwrMkMGDw4ZJTRBUSAAEDAgQGAwAAAAAAAAAAAAEAIIEQkWDw0YJQcBFBUWFAscETAQABAwIFBAIDAQEAAAAAAAERACExQVEQ8GFxgZGhscEg0VDh8TBg/9oADAMBAAIRAxEAAAH1QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAERxDtkoK/O0fclrO5FdHrzs0XXZqfpz7c0OLN8rpxt82BUJ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0dtKLImmOnYxQRsf3PzQxUVuf73381L68UWANMdPOaGioz1V9b1dnt427dKi89I8Z+6QZNKsQUWZ5jbt0qFumPP+fxH//Z

Hình ảnh dạng base64 là hình ảnh đã được mã hóa thành dạng chuỗi data. Với hình ảnh dạng này thì bạn không cần có đường truyền mạng thì vẫn load được hình ảnh.
Ưu điểm :
- Load nhanh vì hình ảnh là dạng chuỗi dữ liệu.
- Vẫn hiển thị hình ảnh khi không kết nối Internet (Bạn hãy ngắt mạng và click vào đây để xem demo)
- Không phụ thuộc vào link hình ảnh (có 1 số plugin dùng cái này, ví dụ JWPlayer....)
Khuyết điểm :
- Do base64 là dạng chuỗi data nên hình ảnh càng lớn thì chuỗi dữ liệu sẽ càng dài -> dẫn tới khó xem trong thiết kế code.

<input id="inputFileToLoad" onchange="encodeImageFileAsURL();" type="file" /> <div id="imgTest"></div> <h3> String data from Image</h3> <textarea id="ket_qua" style="height: 300px; width: 100%;"></textarea> <script type="text/javascript">   function encodeImageFileAsURL() {     var filesSelected = document.getElementById("inputFileToLoad").files;     if (filesSelected.length > 0) {       var fileToLoad = filesSelected[0];       var fileReader = new FileReader();       fileReader.onload = function(fileLoadedEvent) {         var srcData = fileLoadedEvent.target.result; // <--- data: base64         var newImage = document.createElement('img');         newImage.src = srcData;         document.getElementById("imgTest").innerHTML = newImage.outerHTML;         $("#ket_qua").val(newImage.src);       }       fileReader.readAsDataURL(fileToLoad);     }   } </script> 
//Code JAVASCRIPT
Result

Trả lời


📁 Code Demo
🔖