Swedenalessiomaffeis/vue-picture-input
Mobile-friendly picture file input component for Vue.js 3 with image preview, drag and drop, EXIF orientation, and more
vue-picture-input
Mobile-friendly picture file input component for Vue 3 with image preview, drag and drop, EXIF orientation, and more.

Installation
npm
npm install --save vue-picture-input
yarn
yarn add vue-picture-input
Compatibility
Version 5 requires Vue 3.5 or newer within Vue 3. Vue 2 support has been removed; Vue 2 applications should remain on vue-picture-input@4.
The component uses the typed Composition API with `
### Vue 3 (Composition API)
```HTML
<template>
<div class="hello">
<picture-input
ref="pictureInput"
width="600"
height="600"
margin="16"
accept="image/jpeg,image/png"
size="10"
button-class="btn"
:custom-strings="{
upload: '<h1>Bummer!</h1>',
drag: 'Drag a 😺 GIF or GTFO'
}"
@change="onChange">
</picture-input>
</div>
</template>
Prebuilt ESM
To import the precompiled component without an SFC loader:
import PictureInput from 'vue-picture-input/dist/vue-picture-input.mjs'
Browser ESM (CDN)
You can also use it directly in the browser through unpkg's CDN (or jsDelivr). Use a module script and an import map so the component's external vue import resolves to the same Vue instance as your application:
<div id="app">
<p>{{ message }}</p>
<picture-input></picture-input>
</div>
Example project
https://codesandbox.io/s/github/alessiomaffeis/vue-picture-input-example
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 width is not specified, the preview container will expand to full width. If height is not specified, it will be set equal to width.
-
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. Please note that this only affects the image preview; the image file will not be cropped. 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.
-
hideChangeButton: (boolean, optional) set :hideChangeButton="true" if you want to hide the "Change 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, data URI, File, or Blob, optional) use this to provide a default image for the preview. It doesn't change the underlying file input value; this is for image preview only. Default value: empty.
Loading a prefill preserves any existing native file selection, but updates
pictureInput.fileto the prefill source: a generated File for a URL/data URI, or the supplied File/Blob itself. A Blob is not automatically wrapped in a File; when it has no name or modification time, the component'sfileNamemetadata is""andfileModifiedis0. Use the native input orFormDatato inspect the form upload rather than the preview source. A prefill hides the no-preview selection-success message even when an older native selection is retained.File/Blob prefills retain their direct-loading behavior and bypass the selected-file size/type checks; fetched URL/data-URI prefills pass through those checks. Explicit removal (the Remove Photo button or
removeImage()) clears everything: the displayed image, exposedfilereference, and native input, including any earlier selection retained underneath a prefill. It does not restore a previously displayed image. -
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.
- alertOnError: (boolean, optional) Set :alertOnError="false" to disable displaying alerts on attemps to select file with wrong type or too big. Default value: true.
- capture: (string, optional) use this if you want to allow image capture from capture devices (e.g. camera)
- 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
}
Component refs
Parent refs expose live, reactive values through defineExpose, with no extra .value on the component's members:
// Options API parent
this.$refs.pictureInput.file
this.$refs.pictureInput.removeImage()
// Composition API parent (pictureInput is the parent's template ref)
pictureInput.value.image
pictureInput.value.removeImage()
image is initially undefined and becomes "" on removal. file is initially undefined and becomes null on removal. File metadata is available as fileName, fileSize, fileType, and fileModified. The public methods selectImage(), removeImage(), rotateImage(), and preloadImage(source, options) remain callable; existing props, state, computed values, and helper methods are also retained for compatibility. Browser File/Blob objects retain their identity, including direct prefills.
The exposed strings object remains deeply reactive: direct edits update the UI, and updateStrings() applies custom strings in place so retained references stay current.
Unmounting removes the resize listener and cancels pending reads, image decoding callbacks, and abortable prefill fetches without emitting cancellation errors. This does not introduce SSR/hydration support or change overlapping-load ordering.
Events
- change: emitted once on a successful user picture change (prefill excluded). With preview support, the payload is the image as a Base64 Data URI string. Without preview support, no Base64 image is generated: the payload can be
undefinedinitially or""after removal. Use the exposedfileor the native form input in that fallback. Add a ref attribute to picture-input (see the example above) to access this.$refs.pictureInput.image (Base64 Data URI when available) and this.$refs.pictureInput.file (the current image source as a File or Blob, including prefills; not necessarily the native form selection). - prefill: emitted on default image prefill.
- remove: emitted on picture remove.
- click: emitted on picture click.
- aspectratiochange: emitted when the aspect button manually toggles the canvas orientation, with
"landscape"or"portrait"as its payload. Automatic matching throughautoToggleAspectRatiodoes not emit this event. - error: emitted on error, along with an object with type, message, and optional additional parameters.
Rejected selections and dropped files clear the native file input, so a form cannot submit the rejected file. The previous successful preview and exposed file reference are retained, but the native form field stays empty until a file is selected again. Invalid prefills leave the native input untouched.
DeepWiki
You can explore more about this project on DeepWiki too: https://deepwiki.com/alessiomaffeis/vue-picture-input
Tests
npm ci
npm test
npx playwright install chromium
npm run test:browser
npm test runs strict SFC type checking, the Vue 3 source regression suites, and a fresh production build with package-content checks. It does not require a browser. npm run test:browser builds the actual ESM artifact and tests it in Chromium with real image decoding, canvas rendering, file inputs, native FormData, and layout. Vue is served locally through an import map; no CDN is required.
CI runs source/package checks with locked Vue and the Vue 3.5.0 minimum, plus a separate Chromium job using locked Vue. npm run typecheck and npm run test:package run individual checks. The prepack hook rebuilds automatically before npm pack or npm publish. See test/README.md for setup, coverage, debugging, and minimum-version reproduction commands.
Contributions
Contributions are welcome, as long as they are within the scope of the project. Please open a new issue before submitting a pull request.
See CONTRIBUTING.md for the Node.js 24 development workflow and formatting conventions.
Similar Projects
Discover other similar projects and AI models.
✂️ Generates an image from a DOM node using HTML5 canvas and SVG.
🔥一个高效的多媒体支持操作库,可多方面的简单配置操作相册、拍照、录制、录音等功能。也支持配套使用的展示图片、视频、音频的九宫格功能。 (An efficient multimedia support operation library, can be a variety of simple configuration operation album, photo, recording, recording and other functions.Also support supporting the use of the display of pictures, video, audio of the nine grid function.)
🖼 Simple file-upload utility that shows a preview of the uploaded image. Written in TypeScript. No dependencies. Works well with or without a framework.
This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
🙃 A delightful, community-driven framework for managing your zsh configuration. Built by 2,600+ contributors, with hundreds of plugins (git, rails, laravel, node, python, docker, kubectl, aws, and more), themes to spice up your terminal, and automatic updates to keep your command line feeling like home.