当前位置:首页 > VUE

vue实现图片裁切

2026-02-18 18:11:12VUE

Vue 实现图片裁切的方法

使用第三方库 vue-cropperjs

安装 vue-cropperjs 库:

npm install vue-cropperjs --save

引入并使用组件:

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

export default {
  components: {
    VueCropper
  },
  data() {
    return {
      imgSrc: '',
      cropperOptions: {
        aspectRatio: 1,
        viewMode: 1,
        autoCropArea: 0.8
      }
    };
  },
  methods: {
    handleFileChange(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);
    },
    getCroppedImage() {
      this.$refs.cropper.getCroppedCanvas().toBlob((blob) => {
        // 处理裁切后的图片
      });
    }
  }
};

模板部分:

<template>
  <div>
    <input type="file" accept="image/*" @change="handleFileChange">
    <vue-cropper
      ref="cropper"
      :src="imgSrc"
      :options="cropperOptions"
    />
    <button @click="getCroppedImage">裁切图片</button>
  </div>
</template>

使用原生 Canvas API 实现

创建图片裁切组件:

export default {
  data() {
    return {
      image: null,
      canvas: null,
      ctx: null,
      startX: 0,
      startY: 0,
      isDragging: false
    };
  },
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
  },
  methods: {
    handleImageUpload(e) {
      const file = e.target.files[0];
      const reader = new FileReader();
      reader.onload = (event) => {
        this.image = new Image();
        this.image.onload = () => {
          this.canvas.width = this.image.width;
          this.canvas.height = this.image.height;
          this.ctx.drawImage(this.image, 0, 0);
        };
        this.image.src = event.target.result;
      };
      reader.readAsDataURL(file);
    },
    startCrop(e) {
      this.startX = e.offsetX;
      this.startY = e.offsetY;
      this.isDragging = true;
    },
    cropImage(e) {
      if (!this.isDragging) return;
      const width = e.offsetX - this.startX;
      const height = e.offsetY - this.startY;

      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
      this.ctx.drawImage(this.image, 0, 0);
      this.ctx.strokeStyle = 'red';
      this.ctx.strokeRect(this.startX, this.startY, width, height);
    },
    endCrop() {
      this.isDragging = false;
      // 获取裁切区域数据
    }
  }
};

模板部分:

<template>
  <div>
    <input type="file" @change="handleImageUpload">
    <canvas
      ref="canvas"
      @mousedown="startCrop"
      @mousemove="cropImage"
      @mouseup="endCrop"
    ></canvas>
  </div>
</template>

使用 Element UI 的 Upload 组件结合裁切功能

安装 Element UI:

npm install element-ui -S

实现代码:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

export default {
  data() {
    return {
      dialogVisible: false,
      cropImg: '',
      cropper: null
    };
  },
  methods: {
    handlePreview(file) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.cropImg = URL.createObjectURL(file.raw);
        this.initCropper();
      });
    },
    initCropper() {
      const image = this.$refs.cropImage;
      this.cropper = new Cropper(image, {
        aspectRatio: 1,
        viewMode: 1
      });
    },
    confirmCrop() {
      this.cropper.getCroppedCanvas().toBlob((blob) => {
        // 处理裁切后的图片
        this.dialogVisible = false;
      });
    }
  }
};

模板部分:

<template>
  <div>
    <el-upload
      action="#"
      :on-change="handlePreview"
      :auto-upload="false"
    >
      <el-button type="primary">上传图片</el-button>
    </el-upload>

    <el-dialog :visible.sync="dialogVisible">
      <img ref="cropImage" :src="cropImg" style="max-width: 100%">
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmCrop">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

注意事项

  • 移动端适配需要考虑触摸事件替代鼠标事件
  • 裁切比例可以通过设置 aspectRatio 参数控制
  • 大图片处理需要考虑性能优化,可以限制最大尺寸
  • 裁切结果可以导出为多种格式:Blob、Base64 或 FormData

vue实现图片裁切

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

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

实现 vue ssr

实现 vue ssr

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

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕包…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…