Render base64 image in vue.js
Asked Answered
S

6

28

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!

Smudge answered 29/9, 2017 at 15:36 Comment(6)
Did you set img to be a property of the vue instance? Or are you just trying to reference a plain javascript object?Proverbial
it's part of my vue instance @ProverbialSmudge
Can you show how you've set that in your vue instance?Proverbial
sure - just updated my question. note - the img object comes in as a propSmudge
And how are you passing img to the component? BTW this is working for me jsfiddle.net/1eddx72kProverbial
ah - my encodedImage is in the raw format w/o the data:image/png;base64, ... i'll just add that in manually. Thanks for your help!!Smudge
F
48

In vue.js, it'll look like:

<img v-bind:src="'data:image/jpeg;base64,'+imageBytes" />
Fucus answered 11/1, 2018 at 22:32 Comment(1)
This worked for me with slight change - <img v-bind:src="imageBytes" />Avignon
A
18

To display any data URI in a vue component you can do something like this:

Data URI way

<template>
  <img :src="`data:image/png;base64,${img.encodedImage}`" />
</template>
export default {
  props: ['img'],
};

Recommended way

<template>
  <img :src="image" />
</template>
import image from '@/assets/image.png';

export default {
  data() {
    return {
      image,
    };
  },
};
Amaya answered 18/6, 2019 at 14:28 Comment(1)
Even though it's recommended, it's not practical in every situation. I created a Vue app, integrated it with a CRM and my images did not show (status 404) because of incorrect paths.The data-uri way solved my problem.Finagle
D
3

Have had the same issue.

Change

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

to

img = {  encodedImage: 'data:image/jpg;base64,/9x/4AFQSkZJRgABAXAASABIAAD...'}

should solve the problem.

Dandle answered 10/1, 2018 at 23:27 Comment(0)
P
1

<img :src="'data:image/png;base64,' + item.ItemImageData ">

Here, item.ItemImageData contains base64 image string. As my image is png hence I used "data:image/png". Change it accordingly.

Polyzoic answered 30/11, 2022 at 9:36 Comment(0)
F
0

Here's a reusable image component:

<template>
  <img v-bind:src="'data:image/gif;base64,'+ imageAsBase64" />
</template>

<script>
import fs from "fs";

export default {
  data() {
    return {
      imageAsBase64: ""
    };
  },
  mounted() {
    this.imageAsBase64 = fs.readFileSync("./assets/progress-bar.gif", "base64");
  }
};
</script>

Then use in your components:

<template>
  <div>
    <DataImage></DataImage>
  </div>
</template>

<script>
import DataImage from "./DataImage.vue";

export default {
  components: {
    DataImage
  }
}
</script>
Finagle answered 14/2, 2020 at 3:2 Comment(0)
C
0

For large images, putting the image content into different js files works for me.

--- myImage.js content ---

export default {
  getContent() {
    return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABCkAAAKkCAY ...";
  }
}

--- index.vue content ---

import myImage from "../assets/base64/myImage"

export default {
  name: "IndexPage",
  data() {
    return {
      myImageBase64: myImage.getContent(),
  }
}

--- template content ---

<template>
<div class="home-dashboard">
   <img :src="myImageBase64" alt="" class="img-fluid rounded-md shadow-md">
</div>
</template>
Coracle answered 8/6 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.