Upload pictures and Gifs with this mobile friendly Vue.js component

vue-picture-input

A Vue.js component which serves as a picture file input with image preview, drag and drop feature, mobile-friendly, EXIF orientation, custom settings, and more.

Picture Input Demo

There is an example project making use of the vue-picture-input, available here.

Example

To start working with the component using the following command to install it:

npm

npm install --save vue-picture-input

yarn

yarn add vue-picture-input

There are props and events below you can use to make it fit to your needs.

Props

  • 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.
  • crop: (boolean, optional) set :crop="false" if you wish to disable cropping. The image will be resized and centered in order to be fully contained in the preview container. Default value: true.
  • margin: (pixels, optional) the margin around the preview container. Default value: 0.
  • radius: (percentage, optional) The border-radius value for the container. Set radius="50" to get a circular container. Default value: 0.
  • plain: (boolean, optional) Set :plain="true" to remove the inner border and text. Default value: false.
  • 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.
  • removable: (boolean, optional) set :removable="true" if you want to display a "Remove Photo" button. Default value: false.
  • 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. Default value: 'btn btn-primary button'.
  • removeButtonClass: (string, optional) the class which will be applied to the 'Remove Photo' button. Default value: 'btn btn-secondary button secondary'.
  • prefill: (image url or File object, optional) use this to specify the path to a default image (or a File object) to prefill the input with. Default value: empty.
  • prefillOptions: (object, optional) use this if you prefill with a data uri scheme to specify a file name and a media or file type:
    fileName: (string, optional) the file name
    fileType: (string, optional) the file type of the image, i.e. "png", or
    mediaType: (string, optional) the media type of the image, i.e. "image/png"
  • toggleAspectRatio: (boolean, optional) set :toggleAspectRatio="true" to show a button for toggling the canvas aspect ratio (Landscape/Portrait) on a non-square canvas. Default value: false.
  • autoToggleAspectRatio: (boolean, optional) set :autoToggleAspectRatio="true" to enable automatic canvas aspect ratio change to match the selected picture's. Default value: false.
  • changeOnClick: (boolean, optional) set :changeOnClick="true" to open image selector when user click on the image. Default value: true.
  • aspectButtonClass: (string, optional) the class which will be applied to the 'Landscape/Portrait' button. Default value: 'btn btn-secondary button secondary'.
  • zIndex: (number, optional) The base z-index value. In case of issues with your layout, change :zIndex="..." to a value that suits you better. Default value: 10000.
  • customStrings: (object, optional) use this to provide one or more custom strings (see the example above). Here are the available strings and their default values:
{
  upload: '<p>Your device does not support file uploading.</p>', // HTML allowed
  drag: 'Drag an image or <br>click here to select a file', // HTML allowed
  tap: 'Tap here to select a photo <br>from your gallery', // HTML allowed
  change: 'Change Photo', // Text only
  remove: 'Remove Photo', // Text only
  select: 'Select a Photo', // Text only
  selected: '<p>Photo successfully selected!</p>', // HTML allowed
  fileSize: 'The file size exceeds the limit', // Text only
  fileType: 'This file type is not supported.', // Text only
  aspect: 'Landscape/Portrait' // Text only
}

Events

  • 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). You may want to use this.$refs.pictureInput.image (Base64 Data URI string) or this.$refs.pictureInput.file (File Object)
  • remove: emitted on picture remove.
  • click: emitted on picture click.

Usage

Using some of the options above we can create the following example:

<picture-input
ref="pictureInput"
@change="onChange"
width="500"
height="500"
:removable="true"
:autoToggleAspectRatio="true"
margin="12"
radius="50"
accept="image/jpeg,image/png,image/gif"
size="10"
buttonClass="btn-info"
:customStrings="{
    upload: '<h1>Failure!</h1>',
    drag: 'Drag a photo here',
    selected: '<p>Photo successfully selected!</p>', // HTML allowed
    fileSize: 'The file size exceeds the limit', // Text only
    fileType: 'This file type is not supported.',
}">
</picture-input>
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('Not supported!')
        }
    }
}
}

And there it is, a customized input which accepts images & gifs for uploading, easy and fast.

This project is open source available on GitHub.