当前位置:首页 > VUE

vue实现图片裁剪

2026-03-09 22:37:16VUE

使用 vue-cropperjs 实现图片裁剪

安装 vue-cropperjs 依赖包

npm install vue-cropperjs --save

引入组件并注册

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

export default {
  components: {
    VueCropper
  }
}

模板中使用组件

<vue-cropper
  ref="cropper"
  :src="imgSrc"
  :aspectRatio="16/9"
  :viewMode="1"
  :autoCropArea="0.8"
  :guides="true"
  :background="false"
></vue-cropper>

实现基本裁剪功能

设置图片源

data() {
  return {
    imgSrc: '',
    croppedImage: null
  }
}

处理文件上传

handleImageUpload(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) => {
    const reader = new FileReader();
    reader.onload = () => {
      this.croppedImage = reader.result;
    };
    reader.readAsDataURL(blob);
  });
}

添加自定义裁剪选项

设置裁剪比例

setAspectRatio(ratio) {
  this.$refs.cropper.setAspectRatio(ratio);
}

旋转图片

rotate(degrees) {
  this.$refs.cropper.rotate(degrees);
}

缩放图片

zoom(percent) {
  this.$refs.cropper.relativeZoom(percent);
}

响应式处理

监听窗口变化

mounted() {
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.$refs.cropper && this.$refs.cropper.replace();
  }
}

保存裁剪结果

下载裁剪后的图片

downloadCroppedImage() {
  const link = document.createElement('a');
  link.download = 'cropped-image.png';
  link.href = this.croppedImage;
  link.click();
}

上传到服务器

vue实现图片裁剪

uploadCroppedImage() {
  const formData = new FormData();
  formData.append('image', this.dataURLtoBlob(this.croppedImage));

  axios.post('/upload', formData)
    .then(response => {
      console.log('上传成功', response);
    })
    .catch(error => {
      console.error('上传失败', error);
    });
},
dataURLtoBlob(dataurl) {
  const arr = dataurl.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
}

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

相关文章

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…