头像裁切

This commit is contained in:
2020-11-22 18:24:02 +08:00
parent 8f9e25408b
commit a31cc2e542
8 changed files with 157 additions and 10 deletions

100
src/components/imghead.vue Normal file
View File

@@ -0,0 +1,100 @@
<template>
<div class="imghead">
<img id="image" />
<div class="submit-btn" @click="sub">{{lan.$t("baocun") }}</div>
</div>
</template>
<style lang="scss" scoped>
.imghead {
padding-top:0.3rem;
width: 550px;
// height: 550px;
// overflow: hidden;
#image{
width: 500px;
height: 500px;
}
.submit-btn {
width: 63px;
height: 23px;
background: #08ae98;
border-radius: 3px;
font-size: 10px;
font-weight: 500;
color: #ffffff;
text-align: center;
line-height: 23px;
cursor: pointer;
user-select: none;
margin: 20px 0 0 auto;
}
}
</style>
<script lang="ts">
import { useI18n } from '@/utils/i18n';
import { uploadflie } from '@/utils/vod';
import { defineComponent, onMounted } from "vue";
export default defineComponent({
props: {
url: {
type: File,
},
},
setup(props, ctx) {
const lan: any = useI18n();
let CROPPER: any;
onMounted(() => {
const reader = new FileReader();
if (props.url) {
reader.readAsDataURL(props.url);
reader.onload = () => {
const dataURL = reader.result;
//将img的src改为刚上传的文件的转换格式
const image: any = document.querySelector("#image");
console.log(image)
if(image){
console.log(110)
image.src = dataURL;
//创建cropper实例-----------------------------------------
CROPPER = new window.Cropper(image, {
aspectRatio: 1,
viewMode: 1,
background: false,
zoomable: false
});
}
};
}
});
function sub(){
if(CROPPER){
CROPPER.getCroppedCanvas({
maxWidth: 4096,
maxHeight: 4096,
fillColor: '#fff',
imageSmoothingEnabled: true,
imageSmoothingQuality: 'high',
}).toBlob(async (blob: any) => {
//之后就可以愉快的将blob数据发送至后端啦可根据自己情况进行发送我这里用的是axios
// 第三个参数为文件名,可选填.
const file = new File([blob], 'name.png',{type: "image/png"});
const res = await uploadflie(file, (info: any) => {
console.log(info);
});
ctx.emit("upload", res)
})
}
}
return {
lan,
sub
};
},
});
</script>

4
src/import-png.d.ts vendored
View File

@@ -28,4 +28,6 @@ declare module 'vue-cropper'{
declare var FB: any;
declare var TCPlayer: any;
declare var TCPlayer: any;
declare var Cropper: any;

View File

@@ -509,6 +509,9 @@
<a-modal v-model:visible="huobi" :footer="null" :title="lan.$t('tishi')" >
<p>{{lan.$t('huobitishi')}}</p>
</a-modal>
<a-modal v-model:visible="touxiang" width="6rem" :footer="null">
<imghead @upload="imgupload" v-if="touxiang" :url="imgfire"></imghead>
</a-modal>
<nav-bottom></nav-bottom>
</div>
</template>
@@ -530,6 +533,7 @@ import { uploadflie } from "@/utils/vod";
import store from "@/store";
import smile from "@/static/images/smile.png";
import smilet from "@/static/images/smilet.png";
import imghead from "@/components/imghead.vue"
import {
changetel,
checksmscode,
@@ -556,10 +560,12 @@ export default defineComponent({
UserOutlined,
PlaySquareOutlined,
NavBottom,
imghead
},
setup() {
let issum = true;
const lan: any = useI18n();
const touxiang = ref(false);
interface SpeakItem {
lang: string;
proficiency: number;
@@ -996,17 +1002,27 @@ export default defineComponent({
userinfo.value.currencyValue = e;
// editsystemsetting({currency:e})
}
const imgfire = ref<any>()
async function uploadspic(file: AntUpload) {
const res = await uploadflie(file.file, (info: any) => {
console.log(info);
// uploadpicprogress.value = info.percent.toFixed(2) * 100;
});
console.log(res);
// const res = await uploadflie(file.file, (info: any) => {
// console.log(info);
// // uploadpicprogress.value = info.percent.toFixed(2) * 100;
// });
// console.log(res);
// picinfo.fileId=res.fileId
// picinfo.url=res.video.url
// // picinfo.fileId=res.fileId
// // picinfo.url=res.video.url
// formData.value.img = res.video.url;
// setheadimg(res.video.url);
console.log(file.file)
imgfire.value = file.file;
touxiang.value = true;
}
function imgupload(res: any){
formData.value.img = res.video.url;
setheadimg(res.video.url);
touxiang.value = false;
}
// function beforeVideoUpload(file: any){
@@ -1081,7 +1097,10 @@ export default defineComponent({
video,
imgs,
mrqh,
huobi
huobi,
touxiang,
imgfire,
imgupload
};
},
});