fix: Improve the picture cropping component (#463)
* fix: Improve the picture cropping component * fix: component /verify/rotate text show problem
This commit is contained in:
parent
ee1c349858
commit
700306bb45
|
|
@ -69,9 +69,9 @@
|
||||||
default: {},
|
default: {},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
setup(props) {
|
setup(props, ctx) {
|
||||||
const imgElRef = ref<ElRef<HTMLImageElement>>(null);
|
const imgElRef = ref<ElRef<HTMLImageElement>>(null);
|
||||||
const cropper = ref<Nullable<Cropper>>(null);
|
const cropper: any = ref<Nullable<Cropper>>(null);
|
||||||
|
|
||||||
const isReady = ref(false);
|
const isReady = ref(false);
|
||||||
|
|
||||||
|
|
@ -106,9 +106,24 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// event: return base64 and width and height information after cropping
|
||||||
|
const croppered = (): void => {
|
||||||
|
let imgInfo = cropper.value.getData();
|
||||||
|
cropper.value.getCroppedCanvas().toBlob(blob => {
|
||||||
|
let fileReader: FileReader = new FileReader()
|
||||||
|
fileReader.onloadend = (e: any) => {
|
||||||
|
ctx.emit("cropperedInfo", {
|
||||||
|
imgBase64: e.target.result,
|
||||||
|
imgInfo
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fileReader.readAsDataURL(blob)
|
||||||
|
}, 'image/jpeg')
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(init);
|
onMounted(init);
|
||||||
|
|
||||||
return { imgElRef, getWrapperStyle, getImageStyle, isReady };
|
return { imgElRef, getWrapperStyle, getImageStyle, isReady, croppered };
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -152,7 +152,7 @@ export default defineComponent({
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
{!state.showTip && !state.draged && (
|
{!state.showTip && !state.draged && (
|
||||||
<span class={[`ir-dv-img__tip`, 'normal']}>t('redoTip')</span>
|
<span class={[`ir-dv-img__tip`, 'normal']}>{t('component.verify.redoTip')}</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<BasicDragVerify
|
<BasicDragVerify
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,17 @@
|
||||||
<template>
|
<template>
|
||||||
<PageWrapper title="图片裁剪示例" contentBackground>
|
<PageWrapper title="图片裁剪示例" contentBackground>
|
||||||
<CropperImage src="https://fengyuanchen.github.io/cropperjs/images/picture.jpg" />
|
<div class="cropper-container">
|
||||||
|
<CropperImage ref="refCropper" src="https://fengyuanchen.github.io/cropperjs/images/picture.jpg" @cropperedInfo="cropperedInfo" style="width:40%" />
|
||||||
|
<a-button type="primary" @click="onCropper"> 裁剪 </a-button>
|
||||||
|
<img :src="cropperImg" class="croppered" v-if="cropperImg" />
|
||||||
|
</div>
|
||||||
|
<p v-if="cropperImg">裁剪后图片信息:{{ info }}</p>
|
||||||
</PageWrapper>
|
</PageWrapper>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent } from 'vue';
|
import { defineComponent, ref, onBeforeMount, getCurrentInstance } from 'vue';
|
||||||
import { PageWrapper } from '/@/components/Page';
|
import { PageWrapper } from '/@/components/Page';
|
||||||
|
|
||||||
import { CropperImage } from '/@/components/Cropper';
|
import { CropperImage } from '/@/components/Cropper';
|
||||||
|
|
||||||
import img from '/@/assets/images/header.jpg';
|
import img from '/@/assets/images/header.jpg';
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: {
|
components: {
|
||||||
|
|
@ -16,7 +19,42 @@
|
||||||
CropperImage,
|
CropperImage,
|
||||||
},
|
},
|
||||||
setup() {
|
setup() {
|
||||||
return { img };
|
let vm: any;
|
||||||
|
let info = ref("");
|
||||||
|
let cropperImg = ref("");
|
||||||
|
|
||||||
|
const onCropper = (): void => {
|
||||||
|
vm.refs.refCropper.croppered();
|
||||||
|
}
|
||||||
|
|
||||||
|
onBeforeMount(()=>{
|
||||||
|
vm = getCurrentInstance();
|
||||||
|
})
|
||||||
|
|
||||||
|
function cropperedInfo({ imgBase64, imgInfo }) {
|
||||||
|
info.value = imgInfo
|
||||||
|
cropperImg.value = imgBase64
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
img,
|
||||||
|
info,
|
||||||
|
cropperImg,
|
||||||
|
onCropper,
|
||||||
|
cropperedInfo
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.cropper-container {
|
||||||
|
display:flex;
|
||||||
|
justify-content: space-around;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.croppered {
|
||||||
|
width: 50%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue