Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display mysql blob image in html using Vuejs?

I have a vue file like this,

export default {
	data(){
		return{
            info: {
                name: '',
                image: '',
              
            },
            errors: []
		}
	},
  
  created: function(){
        this.getInfo();
  },
  
  methods: {
        getInfo: function(){
              this.info.name = response.data.results[0].name;
              this.info.image = response.data.results[0].image;
        }
  }
}

I am passing data from this file into a child Vue component. The child component is as follows,

<template>
    <div class="ui items">
        <div class="item">
            <div class="ui small image">
                {{info.image}}
            </div>
        </div>
    </div>
  
</template>

<script>
export default{

    props:['info']

}
</script>

My image is stored as a blob in a MySQL database. When I run my application, the image appears as binary data on the UI. The object appears like this,

JSON response of the image

Can anybody here help me in displaying the image? Thank you very much!

like image 504
CoderPJ Avatar asked Nov 24 '17 07:11

CoderPJ


2 Answers

What you want is a data url. You will need to convert the byte array to base64. There is no way to use the raw bytes. Perhaps do this in a computed property, using one of the byte array to base64 functions.

Markup

<img :src="dataUrl">

Behaviour (untested!)

computed : {
    dataUrl(){
        return 'data:image/jpeg;base64,' + btoa(
            new Uint8Array(this.info.image)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        );
    }
}

Search your conscience. This is really not a good idea :-) Sending images as a JSON encoded byte array is something I've never seen, and will be about, guessing, 10x larger on the wire than the binary image. Images in the DB are an antipattern. Images in JSON work, but they should be encoded as base64 strings in the JSON. Even then, they reduce the readability of the JSON, and can bury tools like Postman. Data urls are much slower to load than regular urls. Even with images in the DB, if you control your api, you can gain a lot by making image apis that return just the byte array, with an application/jpeg mime type.

like image 109
bbsimonbb Avatar answered Oct 22 '22 14:10

bbsimonbb


You can load image data using Base64 format, like this,

<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0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FeEGPIKfYU8AETkCYI6wANkZQwIKRNDB1hA0Jrf0rk6WXZ8BvWkb4kv99vn89kDrfVexBSYgVNwDA7AN+jAK3gEd+AlGMGIBFDgFvzouK3JV/lihQTOwLtOtw9wIRG5pJn91Tbgqk9kSk7GViADrTD4HCyZ0NQnomi51sb0fUyCMQEbp2WpU67IjfNjwcYyoUDhjJVcZBjYBy40j4wXgaobWoe8Z6Y80CJBwFpunepIzt2AUgFjtXXshNXjVmMh+K+zzp/CMs0CqeuzrxSRpbOKfdCkiMTS1VBQ41uxMyQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=">

For your problem,

export default {
    data(){
        return{
            info: {
                name: '',
                image: '',

            },
            errors: []
        }
    },

  created: function(){
        this.getInfo();
  },

  methods: {
        getInfo: function(){
              this.info.name = response.data.results[0].name;
              this.info.image = 'data:image/jpeg;base64,' + btoa(response.data.results[0].image.data);
        }
  }
}

In template,

<template>
    <div class="ui items">
        <div class="item">
            <div class="ui small image">
                <img :src="info.image">
            </div>
        </div>
    </div>
</template>
like image 3
Casper Avatar answered Oct 22 '22 14:10

Casper