VUE:vue3.x使用图片裁剪插件vue-picture-cropper

基于Vue图片裁剪 vue-cropper 今天做项目遇到了这样一个需求:对于要上传的图片进行裁剪,于是发现了一个好的工具----vue-cropper 首先下载vue-cropper npm install vue-cropper 引入vue-cropper import { VueCropper } from 'vue-cropper' export default { components: { VueCropper }, } 封装成组件uploadPattern.vue <template> & 阅读详情

简介

vue项目,当需要裁剪图片时,vue插件中有一个很方便的图片裁剪工具:vue-picture-cropper

在这里插入图片描述

参看文献:Vue 3.0图片裁切插件:vue-picture-cropper

此处是以vue3.x + Ant Design 3.x进行编码示范。

具体实现

第一步,安装
npm i vue-picture-cropper
第二步,组件

在这里插入图片描述

此处举例固定最大选择范围框(默认为所传图片的宽高的最小值为界限)

目录:components/globalCrop/index.vue

<template>
  <a-modal
    class="image-crop-box"
    :visible="displayCropModal"
    :destroyOnClose="true"
    :footer="null"
    :maskClosable="false"
    :width="1200"
    title="图片裁剪"
    @cancel="handleModalCancel"
  >
    <div class="modal-content">
      <!-- 图片裁切插件 -->
      <VuePictureCropper
        :boxStyle="{
          width: '100%',
          height: '100%',
          backgroundColor: '#f8f8f8',
          margin: 'auto'
        }"
        :img="pic"
        :options="{
          viewMode: 1,
          dragMode: 'move',
          aspectRatio: 1,
          cropBoxResizable: false
        }"
        :presetMode="{
          mode: 'fixedSize',
          width: imgSize,
          height: imgSize
        }"
      />
    </div>
    <div class="modal-foot">
      <div class="modal-foot-contain">
        <div class="modal-foot-btn" @click="rorateImg(1)">旋转+</div>
        <div class="modal-foot-btn" @click="rorateImg(-1)">旋转-</div>
      </div>
      <div class="modal-foot-contain">
        <div class="modal-foot-btn" @click="handleModalSure">提交</div>
        <div class="modal-foot-btn" @click="handleModalCancel">取消</div>
      </div>
    </div>
  </a-modal>
</template>

<script>
import VuePictureCropper, { cropper } from 'vue-picture-cropper'
import { reactive, ref, toRefs, onMounted } from 'vue'
export default {
  components: {
    VuePictureCropper
  },
  setup(props, context) {
    // #region 变量
    const state = reactive({
      displayCropModal: false, // 弹框是否可见
      degreeNum: 0, // 旋转度数的次数
      onLoading: false, // 是否加载中
      imgSize: 300 // 图片固定宽高
    })
    // 图片显示
    const result = reactive({
      dataURL: '',
      blobURL: ''
    })
    const pic = ref('')
    // #endregion

    // #region 生命周期、监听、计算 函数
    onMounted(() => {})
    // #endregion

    // #region 页面操作方法
    // 生成:所选图片的裁剪画布
    const initCropInfo = file => {
      // 重置上一次的结果
      result.dataURL = ''
      result.blobURL = ''
      // 如果有多个裁剪框,也需要重置掉裁剪目标的值,避免使用同一张图片无法触发watch
      pic.value = ''
      if (!file) return
      // 转换为base64传给裁切组件
      const reader = new FileReader()
      reader.onload = () => {
        // 更新裁切弹窗的图片源
        pic.value = String(reader.result)
        // 获取加载出来的图片的最小宽或高
        let img = new Image()
        img.src = reader.result
        img.onload = function () {
          if (this.width <= this.height) {
            state.imgSize = this.width
          } else {
            state.imgSize = this.height
          }
        }
        // cropper.reset() // 重置默认的裁切区域
        // 显示裁切弹窗
        state.displayCropModal = true
      }
      reader.readAsDataURL(file)
    }
    // 操作:旋转
    const rorateImg = async num => {
      state.degreeNum += num
      // 设置旋转一次的幅度为 90°
      cropper.rotateTo((state.degreeNum % 4) * 90)
    }
    // 操作:裁剪框-确定
    const handleModalSure = async () => {
      if (state.onLoading) return
      state.onLoading = true
      // 获取生成的base64图片地址
      const base64 = cropper.getDataURL()
      // 获取生成的blob文件信息
      const blob = await cropper.getBlob()
      // 获取生成的file文件信息
      const file = await cropper.getFile({
        // fileName: '测试文件名,可不传'
      })
      // console.log({ base64, blob, file })
      // 把base64赋给结果展示区
      result.dataURL = base64
      try {
        result.blobURL = URL.createObjectURL(blob)
      } catch (e) {
        result.blobURL = ''
      }
      context.emit('handleModalSure', file)
      state.displayCropModal = false
      state.onLoading = false
    }
    // 操作:裁剪框-取消
    const handleModalCancel = () => {
      context.emit('handleModalCancel')
      state.displayCropModal = false
    }
    // #endregion

    return {
      ...toRefs(state),
      initCropInfo,
      result,
      pic,
      rorateImg,
      handleModalSure,
      handleModalCancel
    }
  }
}
</script>

<style lang="less">
.image-crop-box {
  position: relative;
  top: calc(50% - 40vh);
  .ant-modal-content {
    .ant-modal-body {
      width: 100%;
      height: calc(80vh - 55px);
      overflow: hidden;
      .modal-title {
        width: 100%;
        margin-top: 18px; // 42 - 24
        margin-bottom: 40px;
        text-align: center;
        font-size: 36px;
        font-weight: bold;
        color: #333;
      }
      .modal-content {
        height: calc(100% - 40px - 30px);
        width: 100%;
      }
      .modal-foot {
        margin-top: 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .modal-foot-contain {
        display: flex;
        align-items: center;
        }
        .modal-foot-btn {
          width: 112px;
          height: 40px;
          margin: 0 8px;
          border-radius: 6px;
          line-height: 40px;
          text-align: center;
          font-size: 16px;
          font-weight: bold;
          color: #333;
          box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
          cursor: pointer;
          &:hover {
            box-shadow: 0px 1px 2px 1px rgba(0, 0, 0, 0.16);
          }
        }
      }
    }
  }
}
</style>
第三步,使用

将目标图片文件,通过proxy.$refs.globalCropRef.initCropInfo(file)进行初始化调用。在调用裁剪组件中的initCropInfo方法时,需要传递file。

// html
<template>
  <div>
    <GlobalCrop
      ref="globalCropRef"
      @handleModalSure="handleCropModalSure"
      @handleModalCancel="handleCropModalCancel" />
  </div>
</template>
// js
import GlobalCrop from '@/components/globalCrop'
import { getCurrentInstance } from 'vue'
export default {
  setup() {
    const { proxy } = getCurrentInstance()
    // 准备上传文件的选择结果
    const handleUpload = file => {
      console.log(file)
      proxy.$refs.globalCropRef.initCropInfo(file)
    }
    // 操作:裁剪框-确定
    const handleCropModalSure = file => {
      console.log(file)
      // 进行图片上传到服务器
      // TODO。。。
      // 注意上传组件的显示内容的更新
      // TODO。。。
    }
    // 操作:裁剪框-取消
    const handleCropModalCancel = () => {
      // 取消裁剪后需要做的操作,TODO。。。
    }
    return {
      handleUpload,
      handleCropModalSure,
      handleCropModalCancel
    }
  }
}

最后

觉得有用的朋友请用你的金手指点一下赞,或者评论留言一起探讨技术!

VUEvue3.x自定义上传文件的组件 当ui框架的上传组件不能满足我们的使用需求,就需要自定义一个上传的组件 阅读详情

相关推荐

APP开发-使用Vue3+vant+html5+ 实现相机拍照,选取相册图片裁剪图片以及提取图片中的文字等功能(二)

一、实现图片裁剪

不觉晓晓的博客 2062

vue3-cropperjs:vue-next 的一个简单的图片剪辑插件

vue3-cropperjs Table of contents Features 支持触摸(移动) 支持缩放 支持旋转 支持缩放(翻转) 支持多种作物 支持通过画布在浏览器端裁剪图像 支持翻译 Exif 方向信息 跨浏览器支持 Getting started Installation npm install vue3-cropperjs yarn add vue3-cropperjs //组件内使用 import v3Cropper from &#39;vue3-cropperjs&#39; import &#39;vue3-cropperjs/dist/v3cropper.css&#39; components: { v3Cropper } main.js里面使用 import v3Cropper from &#39;vue3-cropperjs&#39; import &#39;vue3-cropperjs/dist/v3cropp

vue3 - 超详细 cropper.js 实现图片(头像)裁剪效果功能,在vue3中上传图像后按比例进行图片裁剪及实时预览,Element / Ant Desi(详细示例源码,一键复制开箱即用!)

vue3图片裁剪及实时预览,vue3 vue-croppervue3头像裁剪vue3 el-upload+vue-cropper 图像图片头像裁剪上传,vue3怎么进行图片裁剪vue3使用vue-cropper图片裁剪vue3项目如何实现头像图片裁剪上传功能,vue3网站pc怎么将上传的图片按比例进行裁剪vue3+element plus实现图片上传按尺寸大小裁剪vue3图片上传使用cropperjs 头像裁剪vue3图片上传使用cropperjs 头像裁剪vue3好用的图片裁剪插件

✅ 精选推荐文章 4485

【亲测免费】 vue-picture-cropper:简单易用的 Vue 3 图片裁剪组件

vue-picture-cropper:简单易用的 Vue 3 图片裁剪组件 在现代网页应用中,图片处理是不可或缺的一部分,尤其是在用户需要上传头像、封面图片等场景下,图片裁剪功能变得尤为重要。vue-picture-cropper 正是这样一款为 Vue 3 设计的简单易用的图片裁剪组件。 项目介绍 vue-picture-cropper 是一个基于 Vue 3图片裁剪组件,它提供了一套简...

gitblog_00314的博客 1221

Vue2 vue-cropper裁剪图片-使用方法及注意事项

记录vue-cropper使用及过程中遇到的问题

MyOxygen的博客 3914

vue3图片裁剪】上传、裁剪、压缩图片尺寸

vue3图片裁剪】【代码】上传、裁剪、压缩图片尺寸。

qq_32768761的博客 3274

vue3+vue-cropper图片裁剪个人使用记录

这是本人在使用vue-cropper裁剪图片时的过程,这里大概记录一下

m0_62317155的博客 1万+

Vue3 图片裁剪插件 vue-cropper

Vue 3 图片裁剪插件 vue-cropper setup 语法糖模式用法

XLucas 934

vue3项目中使用在线图片裁剪功能

如何在前后端分离项目中使用在线的图片裁剪功能, 今天我来给大家讲一下。

zihan0321的博客 1645

Vue3项目中使用VueCropper裁剪组件(裁剪及预览效果)

Vue3封装裁剪组件

weixin_45331887的博客 1万+

vue-cropper(图片裁剪插件)

描述:vue-cropper是一个非常好用的图片裁剪插件,可以拿到裁剪图片的base64地址或者blob地址。

weixin_52797317的博客 3208

vue3图片剪裁:vue-img-cutter

vue-img-cutter实现当前已有的图片剪裁

Gunba1的博客 2223

使用vue3实现一个图片裁剪的功能

使用vue3实现一个图片裁剪的功能

jsmeng626的博客 1448

vue3图片裁剪

【代码】vue3图片裁剪

m0_50884068的博客 960

Vue 图片裁剪插件cropperJs

【代码】Vue 图片裁剪插件cropperJs

weixin_53035812的博客 2426

vue实现图片剪裁(vue-cropper组件)并上传到服务器

vue-cropper官网:vue-cropper 安装 npm install vue-cropper --save 使用 main.js文件 import VueCropper from 'vue-cropper' Vue.use(VueCropper) 展示页面 <template> <div class="info-img"> <upload-img ref="headImg" :imageUrl.sync="ruleForm.headImg" /&g.

elephant_my的博客 750

史上最全基于vue图片裁剪vue-cropper使用

史上最全基于vue图片裁剪vue-cropper使用 基于vue图片裁剪vue-cropper 新的需求 vue-cropper官网 代码拷贝 最后 基于vue图片裁剪vue-cropper 最近小编好久没有写博客了,今天发现突然涨了好几个粉,特别的开心,为了回馈大家,我今天准备了一点点小小的心意,最近工作实在太忙,难得抽空更博了,不过最近小编会快马加鞭努力更博的。 新的需求 最近小编遇到一个图片裁剪的需求,对于一个前端小白的我来说,搞这些花里胡哨的东西,走了很多弯路,各种百度,改了又改,

m0_67401935的博客 1万+

Vue 2 / Vue 3】一个简单易用、功能强大的图片裁剪插件,开源且免费!

大家好,我是CodeQi!一位热衷于技术分享的码仔。图片裁剪是许多现代网页应用不可或缺的功能,无论是社交媒体平台、电子商务网站,还是后台管理系统,都需要高效的图片处理工具。vue-img-cutter是一款基于 Vue3图片裁剪插件,拥有旋转、缩放、平移、固定比例裁剪、远程图片裁剪等强大功能,且使用简单、兼容性好,特别适合那些需要快速集成图片裁剪功能的项目。今天,我将介绍如何在 Vue3 ...

frontend_frank的博客 1090
上一篇: VUE:vue3.x自定义上传文件的组件
下一篇: CSS:关于搭建项目,全局初始化CSS默认样式的配置
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值