Vue實現(xiàn)裁切圖片功能
本文實例為大家分享了Vue實現(xiàn)裁切圖片的具體代碼,供大家參考,具體內(nèi)容如下
項目需求做一個身份證的裁切功能
原生開發(fā)的話,這種功能挺容易實現(xiàn)的
Web的沒有做過相關(guān)功能,百度了一下 vue-cropper 在 VUE是使用還是蠻方便的
1)、安裝 vue-cropper
npm install vue-cropper
2)、編寫 .VUE 文件 cropper.vue 應(yīng)為項目本身使用 mui
<template> ?? ?<div> ?? ??? ?<div class="mui-fullscreen"> ? ?? ??? ??? ?<div class="top" style="height:1.63rem;background:white;" v-on:click="onBack()"> ?? ??? ??? ??? ?<img src="../assets/img/payMent/fanhui@2x.png" style="width:0.17rem;margin-left: 0.21rem; margin-top: 0.89rem"> ?? ??? ??? ??? ?<p style="position: absolute; margin-left: 2.9rem;top: 0.8rem; font-family:PingFang-SC-Medium; color: black; ?font-size: 0.36rem;">實名認(rèn)證</p> ?? ??? ??? ?</div> ? ?? ??? ??? ?<div style="position: absolute; width: 100%;top:1.63rem;bottom:1.2rem ;background:#F2F2F2 ;"> ?? ??? ??? ??? ?<!-- <img id="image" :src="image" style="width: 100%; width: 100%;"> --> ?? ??? ??? ??? ?<img src="../assets/img/lobby/youxuanzhuan.png" style="width: 0.5rem; position: absolute; top: 0.5rem; left: 0.5rem; z-index: 2;" ?? ??? ??? ??? ? v-on:click="rotateLeft()" /> ?? ??? ??? ??? ?<img src="../assets/img/lobby/zuoxuanzhuan.png" style="width: 0.5rem; position: absolute; top: 0.5rem; right: 0.5rem; z-index: 2;" ?? ??? ??? ??? ? v-on:click="rotateRight()" /> ? ? ? ?? ??? ??? ??? ?<div class="cropperContent"> ? ?? ??? ??? ??? ??? ?<vue-cropper ref="cropper" :img="option.img" :outputSize="option.size" :outputType="option.outputType" :info="true" ?? ??? ??? ??? ??? ? :full="option.full" :canMove="option.canMove" :canMoveBox="option.canMoveBox" :original="option.original" ?? ??? ??? ??? ??? ? :autoCrop="option.autoCrop" :fixed="option.fixed" :fixedNumber="option.fixedNumber" :centerBox="option.centerBox" ?? ??? ??? ??? ??? ? :infoTrue="option.infoTrue" :fixedBox="option.fixedBox" @realTime="realTime"></vue-cropper> ? ? ? ?? ??? ??? ??? ?</div> ? ?? ??? ??? ??? ?<!-- ?? ??? ??? ??? ?<div style="height: 2rem;width: 2rem; background: greenyellow; position: absolute; top: 5rem;"> ?? ??? ??? ??? ??? ?<img :src="preview" style="height: 2rem;width: 2rem;" /> ?? ??? ??? ??? ?</div> --> ? ?? ??? ??? ?</div> ? ? ?? ??? ??? ?<div class="buttom" style="background: #618FF5; height:1.2rem;width:100%; bottom: 0rem; position:absolute;#F2F2F2" ?? ??? ??? ? v-on:click="onSelect()"> ?? ??? ??? ??? ?<p style="position: absolute; margin-left: 3.4rem;top: 0.3rem; font-family:PingFang-SC-Medium; color: white; ?font-size: 0.36rem;">確定</p> ?? ??? ??? ?</div> ? ?? ??? ??? ?<!-- ?? ??? ??? ?<img :src="preview" style="height: 1rem;" /> --> ? ?? ??? ?</div> ?? ?</div> </template> ? <script> ?? ?import { ?? ??? ?VueCropper ?? ?} from 'vue-cropper' ?? ?export default { ?? ??? ?data() { ?? ??? ??? ?return { ?? ??? ??? ??? ?target: 0, ?? ??? ??? ??? ?cropperHelp: null, ?? ??? ??? ??? ?preview: null, ?? ??? ??? ??? ?//裁剪組件的基礎(chǔ)配置option ?? ??? ??? ??? ?option: { ?? ??? ??? ??? ??? ?img: '', // 裁剪圖片的地址 ?? ??? ??? ??? ??? ?info: true, // 裁剪框的大小信息 ?? ??? ??? ??? ??? ?outputSize: 1, // 裁剪生成圖片的質(zhì)量 ?? ??? ??? ??? ??? ?outputType: 'jpeg', // 裁剪生成圖片的格式 ?? ??? ??? ??? ??? ?canScale: false, // 圖片是否允許滾輪縮放 ?? ??? ??? ??? ??? ?autoCrop: true, // 是否默認(rèn)生成截圖框 ?? ??? ??? ??? ??? ?autoCropWidth: 800, // 默認(rèn)生成截圖框?qū)挾? ?? ??? ??? ??? ??? ?autoCropHeight: 500, // 默認(rèn)生成截圖框高度 ?? ??? ??? ??? ??? ?fixedBox: false, // 固定截圖框大小 不允許改變 ?? ??? ??? ??? ??? ?fixed: true, // 是否開啟截圖框?qū)捀吖潭ū壤? ?? ??? ??? ??? ??? ?fixedNumber: [16, 10], // 截圖框的寬高比例 ?? ??? ??? ??? ??? ?full: false, // 是否輸出原圖比例的截圖 ?? ??? ??? ??? ??? ?canMoveBox: true, // 截圖框能否拖動 ?? ??? ??? ??? ??? ?original: false, // 上傳圖片按照原始比例渲染 ?? ??? ??? ??? ??? ?centerBox: true, // 截圖框是否被限制在圖片里面 ?? ??? ??? ??? ??? ?infoTrue: true // true 為展示真實輸出圖片寬高 false 展示看到的截圖框?qū)捀? ?? ??? ??? ??? ?}, ?? ??? ??? ?} ?? ??? ?}, ?? ??? ?components: { ?? ??? ??? ?VueCropper ?? ??? ?}, ?? ??? ?methods: { ?? ??? ??? ?//放大/縮小 ?? ??? ??? ?changeScale(num) { ?? ??? ??? ??? ?console.log('changeScale') ?? ??? ??? ??? ?num = num || 1; ?? ??? ??? ??? ?this.$refs.cropper.changeScale(num); ?? ??? ??? ?}, ?? ??? ??? ?//坐旋轉(zhuǎn) ?? ??? ??? ?rotateLeft() { ?? ??? ??? ??? ?console.log('rotateLeft') ?? ??? ??? ??? ?this.$refs.cropper.rotateLeft(); ?? ??? ??? ?}, ?? ??? ??? ?//右旋轉(zhuǎn) ?? ??? ??? ?rotateRight() { ?? ??? ??? ??? ?console.log('rotateRight') ?? ??? ??? ??? ?this.$refs.cropper.rotateRight(); ?? ??? ??? ?}, ?? ??? ??? ?// 實時預(yù)覽函數(shù) ?? ??? ??? ?realTime(data) { ?? ??? ??? ??? ?//this.previews = data ?? ??? ??? ?}, ?? ??? ??? ?imgLoad(msg) { ?? ??? ??? ??? ?console.log(msg) ?? ??? ??? ?}, ?? ??? ??? ?cropImage() { ? ?? ??? ??? ?}, ? ? ?? ??? ??? ?onSelect() { ?? ??? ??? ??? ? ?? ??? ??? ??? ?this.$refs.cropper.getCropBlob((data) => { ?? ??? ??? ??? ??? ?//console.log("data===>",data) ?? ??? ??? ??? ??? ?var img = window.URL.createObjectURL(data); ?? ??? ??? ??? ??? ?this.$emit("onCutingResoult", { ?? ??? ??? ??? ??? ??? ?img: img, ?? ??? ??? ??? ??? ??? ?target: this.target ?? ??? ??? ??? ??? ?}) ?? ??? ??? ??? ?}) ?? ??? ??? ?}, ? ?? ??? ??? ?onReset(param) { ?? ??? ??? ??? ?this.target = param.target ?? ??? ??? ??? ?this.option.img = param.url ?? ??? ??? ??? ?this.preview = param.url ?? ??? ??? ?}, ?? ??? ??? ?onBack() { ?? ??? ??? ??? ?this.$emit("onCutingBack") ?? ??? ??? ?} ?? ??? ?} ? ?? ?} </script> ? <style scoped> ?? ?.mui-fullscreen { ?? ??? ?background: white; ?? ?} ? ?? ?.cropperContent { ?? ??? ?width: 100%; ?? ??? ?height: 100%; ?? ?} ? ?? ?/* ?? ?.mui-fullscreen { ?? ??? ?background: #F2F2F2; ?? ??? ?top: 0rem; ?? ??? ?bottom: 0rem; ?? ?} */ </style>
基本上,放里面就能使用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實現(xiàn)裁切圖片同時實現(xiàn)放大、縮小、旋轉(zhuǎn)功能
- vue+element實現(xiàn)圖片上傳及裁剪功能
- vue實現(xiàn)移動端圖片裁剪上傳功能
- vue 實現(xiàn)剪裁圖片并上傳服務(wù)器功能
- vue-cli結(jié)合Element-ui基于cropper.js封裝vue實現(xiàn)圖片裁剪組件功能
- Vue-cropper 圖片裁剪的基本原理及思路講解
- vue移動端裁剪圖片結(jié)合插件Cropper的使用實例代碼
- 詳解vue項目中實現(xiàn)圖片裁剪功能
- 基于Vue的移動端圖片裁剪組件功能
- cropper js基于vue的圖片裁剪上傳功能的實現(xiàn)代碼
相關(guān)文章
Vue?2中實現(xiàn)CustomRef方式防抖節(jié)流
這篇文章主要為大家介紹了Vue?2中實現(xiàn)CustomRef方式防抖節(jié)流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02vue中使用gantt-elastic實現(xiàn)可拖拽甘特圖的示例代碼
這篇文章主要介紹了vue中使用gantt-elastic實現(xiàn)可拖拽甘特圖,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07