Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render base64 image in vue.js

Tags:

Can someone help me display a Base 64 encoded image using vue.js?

Basically I have an image object:

img = {   encodedImage: '/9x/4AFQSkZJRgABAXAASABIAAD...' } 

I know that in pure HTML i can do something like:

<div>     <p>Taken from wikpedia</p>     <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO     9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" /> </div>  

In vue, i have tried the following:

<img :src="img.encodedImage" /> <img v-bind:src="img.encodedImage" /> <img :src="{{img.encodedImage}}" /> <img v-bind:src="{{img.encodedImage}}" /> 

Here's my full vue component:

<template>   <div>     <img :src="img.encodedImage">   </div> </template> <script>  export default {   props: [ 'img' ] } </script> 

Can someone help?

Thanks in advance!

like image 335
Trung Tran Avatar asked Sep 29 '17 15:09

Trung Tran


People also ask

How to show base64 in image tag?

Images encoded with Base64 can be embedded in HTML by using the <img> tag. This can help to increase the page load time for smaller images by saving the browser from making additional HTTP requests.

How to open base64 image in HTML?

To get this to work, once your bytes are loaded into an Image class, Create a BytesIO object and then use this to save the image with the format to the BytesIO, i.e img. save(bt_ob, format="png"). This code will now work in the html in the "data:image/png;base64,<>" format.

What is Data Image PNG base64?

data:image/png;base64 tells the browser that the data is inline, is a png image and is in this case base64 encoded. The encoding is needed because png images can contain bytes that are invalid inside a HTML document (or within the HTTP protocol even).


1 Answers

In vue.js, it'll look like:

<img v-bind:src="'data:image/jpeg;base64,'+imageBytes" /> 
like image 187
ejdrian313 Avatar answered Sep 18 '22 18:09

ejdrian313