当前位置:首页 > VUE

vue实现图片裁切

2026-01-18 01:43:55VUE

Vue 实现图片裁切的方法

使用 vue-cropperjs 库

vue-cropperjs 是一个基于 Cropper.js 的 Vue 封装库,专门用于图片裁切功能。

安装依赖:

vue实现图片裁切

npm install vue-cropperjs

基本用法:

vue实现图片裁切

<template>
  <div>
    <input type="file" @change="uploadImage">
    <vue-cropper
      ref="cropper"
      :src="imgSrc"
      :aspect-ratio="16/9"
    ></vue-cropper>
    <button @click="cropImage">裁切图片</button>
  </div>
</template>

<script>
import VueCropper from 'vue-cropperjs';
import 'cropperjs/dist/cropper.css';

export default {
  components: { VueCropper },
  data() {
    return {
      imgSrc: ''
    }
  },
  methods: {
    uploadImage(e) {
      const file = e.target.files[0];
      if (!file.type.includes('image/')) {
        alert('请选择图片文件');
        return;
      }
      const reader = new FileReader();
      reader.onload = (event) => {
        this.imgSrc = event.target.result;
      };
      reader.readAsDataURL(file);
    },
    cropImage() {
      this.$refs.cropper.getCroppedCanvas().toBlob((blob) => {
        // 处理裁切后的图片
      }, 'image/jpeg', 0.9);
    }
  }
}
</script>

使用 cropperjs 直接集成

如果不使用 vue-cropperjs 封装,可以直接集成 Cropper.js:

<template>
  <div>
    <input type="file" @change="uploadImage">
    <img id="image" src="">
    <button @click="crop">裁切</button>
  </div>
</template>

<script>
import Cropper from 'cropperjs';
import 'cropperjs/dist/cropper.css';

export default {
  mounted() {
    this.cropper = new Cropper(document.getElementById('image'), {
      aspectRatio: 1,
      viewMode: 1
    });
  },
  methods: {
    uploadImage(e) {
      const file = e.target.files[0];
      if (!file.type.includes('image/')) return;

      const reader = new FileReader();
      reader.onload = (event) => {
        this.cropper.replace(event.target.result);
      };
      reader.readAsDataURL(file);
    },
    crop() {
      this.cropper.getCroppedCanvas().toBlob((blob) => {
        // 处理裁切结果
      });
    }
  }
}
</script>

自定义简单裁切功能

对于简单的裁切需求,可以完全自己实现:

<template>
  <div>
    <input type="file" @change="loadImage">
    <div class="crop-container" ref="container">
      <img :src="imgSrc" ref="image" @load="initCrop">
      <div class="crop-box" ref="cropBox"></div>
    </div>
    <button @click="performCrop">裁切</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imgSrc: '',
      startX: 0,
      startY: 0,
      isDragging: false
    }
  },
  methods: {
    loadImage(e) {
      const file = e.target.files[0];
      if (!file.type.includes('image/')) return;

      const reader = new FileReader();
      reader.onload = (event) => {
        this.imgSrc = event.target.result;
      };
      reader.readAsDataURL(file);
    },
    initCrop() {
      const img = this.$refs.image;
      const container = this.$refs.container;
      const cropBox = this.$refs.cropBox;

      // 初始化裁切框位置和大小
      cropBox.style.width = '100px';
      cropBox.style.height = '100px';
      cropBox.style.left = '50px';
      cropBox.style.top = '50px';

      // 添加拖拽事件
      cropBox.addEventListener('mousedown', this.startDrag);
      document.addEventListener('mousemove', this.drag);
      document.addEventListener('mouseup', this.stopDrag);
    },
    startDrag(e) {
      this.isDragging = true;
      this.startX = e.clientX;
      this.startY = e.clientY;
    },
    drag(e) {
      if (!this.isDragging) return;
      const cropBox = this.$refs.cropBox;
      const dx = e.clientX - this.startX;
      const dy = e.clientY - this.startY;

      cropBox.style.left = (parseInt(cropBox.style.left) + dx) + 'px';
      cropBox.style.top = (parseInt(cropBox.style.top) + dy) + 'px';

      this.startX = e.clientX;
      this.startY = e.clientY;
    },
    stopDrag() {
      this.isDragging = false;
    },
    performCrop() {
      const img = this.$refs.image;
      const cropBox = this.$refs.cropBox;

      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');

      const x = parseInt(cropBox.style.left);
      const y = parseInt(cropBox.style.top);
      const width = parseInt(cropBox.style.width);
      const height = parseInt(cropBox.style.height);

      canvas.width = width;
      canvas.height = height;

      ctx.drawImage(img, x, y, width, height, 0, 0, width, height);

      // 获取裁切后的图片数据
      const croppedImage = canvas.toDataURL('image/jpeg');
      // 处理裁切结果
    }
  }
}
</script>

<style>
.crop-container {
  position: relative;
  display: inline-block;
}
.crop-box {
  position: absolute;
  border: 2px dashed #fff;
  cursor: move;
}
</style>

注意事项

  1. 移动端适配需要考虑触摸事件
  2. 裁切比例可以根据需求调整
  3. 裁切框的交互可以进一步优化,如调整大小
  4. 性能优化对于大图片很重要,可以考虑压缩或分块处理
  5. 输出格式支持多种选项,如 PNG、JPEG 等

标签: 图片vue
分享给朋友:

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogres…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-mode…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性…