当前位置: 首页 > news >正文

vue-img-cutter 实现图片裁剪[vue 组件库]

借助 vue-img-cutter 可以在网页端实现图片裁剪功能,最终功能效果如下:
在这里插入图片描述
在这里插入图片描述
组件 npm 安装

npm install vue-img-cutter@2 --save-dev # for vue2
npm install vue-img-cutter@3 --save-dev # for vue3
vue-img-cutter使用

template模板标签模块,定义了两个 div 标签,用 flex 做了水平布局,左侧区域为裁剪主区域,右侧为显示裁剪后的效果

<template> <div><div style="display: flex;flex-direction: row;"><ImgCutter v-on:cutDown="cutDownImg" :WatermarkText="''" :is-modal="false" :tool="false"@onPrintImg="cutterPrintImg" style="padding: 10px;"><template #cancel><a-button @click="() => {isShowable = false}">取消</a-button></template></ImgCutter><div style="margin-left: 10px ; padding: 10px;"><div style="margin-left: 10px; padding-bottom: 20px; font-size: 18px;font-weight: bold;">裁剪后图像</div><div style="padding: 2px; background-color: rgba(0,0,0,0.1);"><img :src="temImgPath" style="width: 100%;" /></div></div></div>
</template>

在 demo 中,主要用到了 vue-img-cutter 组件中两个回调函数

  • onPrintImg 事件函数 接受实时裁剪图片;
  • cutDown 裁剪成功后会触发的回调函数,用来处理裁剪后的图片,例如,本案例中将裁剪后图片上传至 文件服务器上

组件除了提供以上函数外,还提拱了一些其他属性以及信号槽,使用时可以根据功能需求对其进行重写

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

详情请参考:https://github.com/acccccccb/vue-img-cutter

<script setup>
import { Client } from '@/utils/Client'
import { message } from 'ant-design-vue'
import ImgCutter from 'vue-img-cutter'
import { ref, defineProps, watch, onMounted, defineEmits } from 'vue'
const emit = defineEmits(['update:value'])
const Props = defineProps({value: String,type: {type: String,default: 'IMAGE'}
})const loading = ref(false)
const isShowable = ref(false)
const currentImgPath = ref('')
const temImgPath = ref('')
const imgForm = ref(null)
watch(() => Props.value,(val, o) => {currentImgPath.value = val}
)onMounted(() => {currentImgPath.value = Props.valuetemImgPath.value = currentImgPath.value
})
// 图片裁剪确认后触发
function cutDownImg(option) {// console.log(fileName)handleHttpRequest(option)
}function cutterPrintImg(obj) {console.log('obj is ', obj)temImgPath.value = obj.dataURL}function onCancel() {isShowable.value = false
}// 触发裁剪功能
function touchCutDown() {isShowable.value = true
}function randomString(len) {const len1 = len || 32const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz12345678'const maxPos = chars.lengthlet pwd = ''for (let i = 0; i < len1; i++) {pwd += chars.charAt(Math.floor(Math.random() * maxPos))}return pwd
}
async function handleHttpRequest(option) {// 上传OSSconst client = Client()// 随机命名const randomName = `${randomString(6)}_${new Date().getTime()}.${option.file.name.split('.').pop()}`try {// 分片上传文件const result = await client.put(randomName, option.file)if (result) {if (result.url.startsWith('http://')) {result.url = result.url.replace('http://', 'https://')}currentImgPath.value = result.urlemit('update:value', currentImgPath.value)isShowable.value = false}} catch (error) {message.error('上传失败')}
}
</script>

最终style 样式模块如下

  
<style lang="less" scoped>
.remove-btn-wrap {position: absolute;left: 0;bottom: 0;right: 0;top: 0;text-align: center;display: none;align-items: center;justify-content: center;background: rgba(0, 0, 0, 0.5);
}.ant-upload:hover .remove-btn-wrap {display: flex;
}/deep/ .ant-upload {img {width: 100%;height: 100%;object-fit: cover;border-radius: 2px;}
}
</style>
http://www.lryc.cn/news/183585.html

相关文章:

  • 手把手教你从零开始腾讯云服务器部署(连接建站教程)
  • 微信开放平台第三方开发,实现代小程序备案申请
  • 设计模式——11. 享元模式
  • 【LLM】主流大模型体验(文心一言 科大讯飞 字节豆包 百川 阿里通义千问 商汤商量)
  • CSS小计
  • 机器学习:决策树
  • xxl-job的原理(2)—调度中心管理注册信息
  • 小白入门pytorch(二)----神经网络
  • 【进阶C语言】排序函数(qsort)与模拟实现(回调函数的实例)
  • CentOS 7 上编译和安装 SQLite 3.9.0
  • [GXYCTF2019]禁止套娃 无回显 RCE 过滤__FILE__ dirname等
  • Springboot使用Aop保存接口请求日志到mysql
  • 网络安全面试题汇总(附答案)
  • Centos7安装kvm,配置虚拟机网络
  • Javascript文件上传
  • golang gin——文件上传(单文件,多文件)
  • 面试题:Redis和MySQL的事务区别是什么?
  • Canvas绘图
  • 逻辑回归评分卡
  • DPDK系列之三十三DPDK并行机制的底层支持
  • LVGL_基础控件滚轮roller
  • 王道考研操作系统——文件管理
  • 商业智能系统的主要功能包括数据仓库、数据ETL、数据统计输出、分析功能
  • 基于帝国主义竞争优化的BP神经网络(分类应用) - 附代码
  • 将python项目部署在一台服务器上
  • 【C语言】善于利用指针(二)
  • Python调用C++
  • 自己实现扫描全盘文件的函数。
  • JSON文件读写
  • VisualStudio2022环境下Release模式编译dll无法使用TLS函数问题