Vue Image Crop Upload Component
vue-image-crop-upload is a Vue component designed for client-side image cropping and uploading, currently stable at version 3.0.3. It provides a rich UI for users to select, crop, and then upload images to a specified server endpoint. The component prioritizes a desktop user experience and explicitly advises against mobile use due to its design. Key differentiators include its compatibility with Vue 3 (since v3.0.0), support for older browsers like IE10+, and extensive customization options for the cropping interface, such as disabling circular or square previews and image rotation. While a specific release cadence isn't published, major version bumps appear tied to Vue framework updates, indicating stability rather than rapid iteration. It integrates easily into Vue applications, handling image selection, client-side manipulation, and direct HTTP upload with configurable parameters and headers.
Common errors
-
Failed to resolve component: vue-image-crop-upload
cause The component was not properly imported or registered in your Vue application.fixEnsure you have `import VueImageCropUpload from 'vue-image-crop-upload';` at the top of your component/main file and that it's listed in the `components` option (for local use) or globally registered with `app.component()` (for global use). -
TypeError: Cannot read properties of undefined (reading 'length') in vue-image-crop-upload.umd.js
cause This often occurs when the component's internal methods expect certain props or data to be defined, but they are missing or incorrectly typed, possibly due to an invalid `imgFormat` or `imgBgc` value.fixVerify that `imgFormat` is 'jpg' or 'png' and `imgBgc` is a valid color string (e.g., '#fff') if `imgFormat` is 'jpg'. Check all props for correct types as specified in the documentation. -
ERR_NAME_NOT_RESOLVED or 404 Not Found in console network tab for upload request
cause The `url` prop provided to the component points to an invalid or unreachable backend endpoint, or the path is incorrect.fixDouble-check the `url` prop's value to ensure it's a correct and accessible API endpoint. Verify your backend server is running and configured to handle the POST request at that specific path.
Warnings
- breaking Version 3.0.0 introduced full compatibility with Vue 3. This means projects built on Vue 2 will be incompatible and should use an older version (e.g., ^2.x.x) of the component.
- gotcha The component is explicitly 'not recommended for use on the mobile side'. Developers should be aware of this limitation and implement alternative solutions or responsive adjustments for mobile users.
- gotcha If the `url` prop is not provided or is an empty string, the component will not perform an upload, leading to silent failure of the upload functionality.
Install
-
npm install vue-image-crop-upload -
yarn add vue-image-crop-upload -
pnpm add vue-image-crop-upload
Imports
- VueImageCropUpload
const VueImageCropUpload = require('vue-image-crop-upload');import VueImageCropUpload from 'vue-image-crop-upload';
- Component Registration (Local)
import VueImageCropUpload from 'vue-image-crop-upload'; export default { components: { VueImageCropUpload }, // ... } - Component Registration (Global)
Vue.component('vue-image-crop-upload', VueImageCropUpload);import VueImageCropUpload from 'vue-image-crop-upload'; createApp(App).component('vue-image-crop-upload', VueImageCropUpload).mount('#app');
Quickstart
import { createApp, ref } from 'vue';
import VueImageCropUpload from 'vue-image-crop-upload';
const app = createApp({
components: {
'vue-image-crop-upload': VueImageCropUpload
},
setup() {
const show = ref(false);
const params = ref({
token: process.env.VUE_APP_UPLOAD_TOKEN ?? 'dummy_token',
name: 'avatar'
});
const headers = ref({
authorization: `Bearer ${process.env.VUE_APP_AUTH_KEY ?? ''}`
});
const imgDataUrl = ref('');
const toggleShow = () => {
show.value = !show.value;
};
const cropSuccess = (imgDataUrlLocal: string, field: string) => {
console.log('Cropping successful. Field:', field);
imgDataUrl.value = imgDataUrlLocal; // Update the preview
};
const cropUploadSuccess = (jsonData: any, field: string) => {
console.log('Upload successful. JSON Data:', jsonData, 'Field:', field);
alert('Upload success!');
show.value = false; // Close modal on success
};
const cropUploadFail = (status: number, field: string) => {
console.log('Upload failed. Status:', status, 'Field:', field);
alert(`Upload failed with status: ${status}`);
};
return {
show,
params,
headers,
imgDataUrl,
toggleShow,
cropSuccess,
cropUploadSuccess,
cropUploadFail
};
},
template: `
<div id="app-container">
<h1>Vue Image Crop Upload Demo</h1>
<img v-if="imgDataUrl" :src="imgDataUrl" alt="Cropped Image Preview" style="max-width: 200px; max-height: 200px; border: 1px solid #ccc; margin-bottom: 20px;" />
<button @click="toggleShow">Upload New Avatar</button>
<vue-image-crop-upload
v-model:show="show"
:width="300"
:height="300"
url="/api/upload-avatar" <!-- Replace with your actual backend upload endpoint -->
field="upload"
img-format="png"
:params="params"
:headers="headers"
@crop-success="cropSuccess"
@crop-upload-success="cropUploadSuccess"
@crop-upload-fail="cropUploadFail"
></vue-image-crop-upload>
<p>This demo illustrates how to integrate and use the <code>vue-image-crop-upload</code> component in a Vue 3 application. It sets up basic visibility control, defines upload parameters and headers, and handles success and failure events. The <code>url</code> prop should point to a valid backend API endpoint capable of handling image uploads.</p>
</div>
`
});
app.mount('#app-container');