vue-cropper

repository·main·Indexed 26 days ago

https://github.com/xyxiao001/vue-cropper

An image cropping plugin for Vue.js applications (version 0.6.5) that allows users to crop, rotate, and scale images with real-time previews. It supports both Vue 2 and Vue 3, providing a set of props for configuration, event callbacks for movement and loading, and methods for extracting crop data as Base64 or Blob. For Vue 3 projects, the author recommends using cropper-next-vue.

Tokens
2.6K
Snippets
9
Records
18
Agent score
87%

What's inside vue-cropper

  1. Import and use vue-cropper in Vue 2

    main

    For Vue 2 projects, you can import the component locally or register it globally.

    Local Import:

    import { VueCropper } from 'vue-cropper'
    // In component options:
    components: {
      VueCropper
    }

    Global Registration:

    import VueCropper from 'vue-cropper'
    Vue.use(VueCropper)
    import { VueCropper } from 'vue-cropper'
    components: {
      VueCropper
    }
  2. Register vue-cropper in a Vue application

    main

    Depending on your setup, you can register vue-cropper globally in main.js or locally within a specific component.

    Global Registration (main.js):

    import VueCropper from "vue-cropper"
    Vue.use(VueCropper)

    Local Registration (Component):

    import { VueCropper } from "vue-cropper"
    
    export default {
      components: {
        VueCropper,
      },
    }

    CDN Usage:

    <script src="vuecropper.js"></script>
    <script>
      Vue.use(window['vue-cropper'])
    </script>
    import { VueCropper }  from "vue-cropper"
    components: {
      VueCropper,
    },
    
    main.js
    
    import VueCropper from "vue-cropper" 
    
    Vue.use(VueCropper)
  3. Configure vue-cropper for Nuxt.js (SSR)

    main

    Because vue-cropper relies on browser APIs, you must disable Server-Side Rendering (SSR) for the plugin in Nuxt.js.

    In your nuxt.config.js:

    module.exports = {
      build: {
        vendor: [
          'vue-cropper'
        ],
        plugins: [
          { src: '~/plugins/vue-cropper', ssr: false }
        ]
      }
    }
    module.exports = {
      ...
      build: {
        vendor: [
          'vue-cropper
        ...
        plugins: [
          { src: '~/plugins/vue-cropper', ssr: false }
        ]
      }
    }
  4. Import and use vue-cropper in Vue 3

    main

    For Vue 3 projects, you can import the component locally or register it globally. Ensure you also import the required CSS.

    Local Import:

    import 'vue-cropper/dist/index.css'
    import { VueCropper } from "vue-cropper";

    Global Registration:

    import VueCropper from 'vue-cropper'; 
    import 'vue-cropper/dist/index.css'
    
    const app = createApp(App)
    app.use(VueCropper)
    app.mount('#app')
  5. Configure vue-cropper via props

    main

    The vue-cropper component accepts several props to control its behavior.

    Important Requirements:

    • You must wrap the component in an outer container and set its width and height.
    • Disable local mock services, otherwise image conversion may fail.
    NameFunctionDefaultOptions
    imgImage source URLemptyurl, base64, blob
    outputSizeOutput image quality10.1 ~ 1
    outputTypeOutput image formatjpgjpeg, png, webp
    infoShow crop box infotruetrue, false
    canScaleAllow mouse wheel scalingtruetrue, false
    autoCropDefault to generating crop boxfalsetrue, false
    autoCropWidthDefault crop box width80% of container0 ~ max
    autoCropHeightDefault crop box height80% of container0 ~ max
    fixedFixed aspect ratiofalsetrue, false
    fixedNumberAspect ratio (if fixed is true)[1, 1][width, height]
    fullOutput original aspect ratiofalsetrue, false
    fixedBoxLock crop box sizefalsetrue, false
    canMoveAllow moving imagetruetrue, false
    canMoveBoxAllow moving crop boxtruetrue, false
    originalRender image at original scalefalsetrue, false
    centerBoxRestrict crop box to image areafalsetrue, false
    highOutput image based on device DPRtruetrue, false
    infoTrueShow real output size vs visible sizefalsetrue, false
    maxImgSizeMax image width/height limit20000 ~ max
    enlargeOutput scale multiplier10 ~ max
    modeDefault rendering modecontaincontain, cover, 100px, 100% auto
    limitMinSizeMinimum crop area limit10Number, Array, String
    fillColorBackground color for exportempty#ffffff, white
  6. Implement real-time preview

    main

    Use the @realTime event to get real-time preview data. The callback provides an object containing the preview URL, dimensions, and CSS styles for the preview container and image.

    Example Implementation:

    <vue-cropper
      @realTime="realTime"
      :img="option.img"
    ></vue-cropper>
    
    <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px', 'overflow': 'hidden', 'margin': '5px'}">
      <div :style="previews.div">
        <img :src="previews.url" :style="previews.img">
      </div>
    </div>
    realTime (data) {
      this.previews = data
    }
    ```html
    @realTime="realTime"
    // Real time preview function
    realTime (data) {
      this.previews = data
    }
    <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden',
        'margin': '5px'}">
      <div :style="previews.div">
        <img :src="option.img" :style="previews.img">
      </div>
    </div>
  7. Handle vue-cropper events

    main

    The component provides several event callbacks:

    • @realTime: Emitted during real-time preview. Returns an object containing the width (w) and height (h) of the crop area.
    • @imgMoving: Emitted when the image is being moved. Returns an object with moving: boolean and axis coordinates (x1, x2, y1, y2).
    • @cropMoving: Emitted when the crop box is being moved. Returns an object with moving: boolean and axis coordinates (x1, x2, y1, y2).
    • @imgLoad: Emitted when the image finishes loading. Returns success or error.