Mobile-friendly picture file input Vue.js component with image preview, drag and drop, EXIF orientation, and more.
###npm
npm install --save vue-picture-input
<template>
<div class="hello">
<picture-input ref="pictureInput" @change="onChange" width="600" height="600" margin="16" accept="image/jpeg,image/png" size="10" buttonClass="btn"></picture-input>
</div>
</template>
<script>
import PictureInput from 'vue-picture-input'
export default {
name: 'app',
data () {
return {
}
},
components: {
PictureInput
},
methods: {
onChange () {
console.log('New picture selected!')
if (this.$refs.pictureInput.image) {
console.log('Picture loaded.')
} else {
console.log('FileReader API not supported: use the <form>, Luke!')
}
}
}
}
</script>
You can find an example project here: https://github.com/alessiomaffeis/vue-picture-input-example
- width, height: (pixels, optional) the maximum width and height of the preview container. The picture will be resized and centered to cover this area. If not specified, the preview container will expand to full width, 1:1 square ratio.
- margin: (pixels, optional) the margin around the preview container. Default value: 0.
- accept: (media type, optional) the accepted image type(s), e.g. image/jpeg, image/gif, etc. Default value: 'image/*'.
- size: (MB, optional) the maximum accepted file size in megabytes.
- id, name: (string, optional) the id and name attributes of the HTML input element.
- buttonClass: (string, optional) the class which will be applied to the 'Change Photo' button.
- change: emitted on (successful) picture change. If you need to access the underlying image from the parent component, add a ref attribute to picture-input (see the example above).
- Add support for multiple files (?)
- Demo GitHub pages
All contributions are welcome.