Vue Picture Cropper

1.0.0 · active · verified Sun Apr 19

Vue Picture Cropper is a lightweight and easy-to-use image cropping component specifically designed for Vue 3 applications. It leverages the popular Cropper.js library internally, providing a robust set of cropping functionalities. The current stable version is `v1.0.0`, which introduced significant breaking changes and new features, most notably the `useCropper` composable for enhanced programmatic control. The project maintains an active release cadence, with a focus on Vue 3 Composition API, TypeScript support, and features like multiple cropper instances on a single page, preset cropping modes (fixed size, circular), and direct output as Blob, File, or Base64. It differentiates itself through its tight integration with the Vue 3 ecosystem and a streamlined API for common cropping tasks.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `vue-picture-cropper` and `cropperjs`, select an image, display the cropper component, and then use the `useCropper` composable to retrieve the cropped image as a Blob for preview.

<!-- App.vue -->
<template>
  <div class="container">
    <h1>Image Cropper</h1>
    <input type="file" accept="image/*" @change="selectFile" />
    <div v-if="imageSrc">
      <PictureCropper
        :boxStyle="{ width: '100%', height: '300px', backgroundColor: '#f8f8f8', margin: 'auto' }"
        :img="imageSrc"
        :options="{ 
          viewMode: 1, 
          dragMode: 'move', 
          aspectRatio: 1 / 1,
          cropBoxResizable: true
        }"
        @ready="cropperReady"
      />
      <button @click="cropImage">Crop Image</button>
      <div v-if="croppedImageBlob">
        <h2>Cropped Image Preview</h2>
        <img :src="croppedImageBlob" alt="Cropped" style="max-width: 100%;" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PictureCropper, { useCropper } from 'vue-picture-cropper';

const imageSrc = ref<string | null>(null);
const croppedImageBlob = ref<string | null>(null);
const { getCropperRef, getCropper, setCropper } = useCropper();

const selectFile = (e: Event) => {
  const target = e.target as HTMLInputElement;
  if (target.files && target.files.length) {
    const file = target.files[0];
    imageSrc.value = URL.createObjectURL(file);
  }
};

const cropperReady = () => {
  console.log('Cropper is ready!');
  // You can set options or perform actions when cropper is ready
  // For example, setCropper(getCropperRef().value.cropperInstance); // If you need direct Cropper.js instance
};

const cropImage = async () => {
  const cropper = getCropper(); // Get the current cropper instance via composable
  if (cropper) {
    const blob = await cropper.getBlob();
    if (blob) {
      croppedImageBlob.value = URL.createObjectURL(blob);
      console.log('Cropped image blob:', blob);
    } else {
      console.error('Failed to get cropped image blob.');
    }
  }
};
</script>

<style>
.container {
  max-width: 600px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
  font-family: sans-serif;
}
button {
  margin-top: 20px;
  padding: 10px 15px;
  background-color: #3fb984;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>

view raw JSON →