头像上传裁剪

This commit is contained in:
haomingming
2023-05-11 11:13:50 +08:00
parent 7ef82dab29
commit c3ef53d336
18 changed files with 1429 additions and 60 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,3 @@
{
"component": true
}
@@ -0,0 +1,32 @@
<view class='image-cropper' catchtouchmove='_preventTouchMove' wx:if="{{show_cropper}}">
<view class='main' id="main" bindtouchend="_cutTouchEnd" bindtouchstart="_cutTouchStart" bindtouchmove="_cutTouchMove">
<view class='content'>
<view class='content_top bg_gray {{_flag_bright?"":"bg_black"}}' style="height:{{cut_top}}px;transition-property:{{_cut_animation?'':'background'}}"></view>
<view class='content_middle' style="height:{{height}}px;">
<view class='content_middle_left bg_gray {{_flag_bright?"":"bg_black"}}' style="width:{{cut_left}}px;transition-property:{{_cut_animation?'':'background'}}"></view>
<view class='content_middle_middle' style="width:{{width}}px;height:{{height}}px;transition-duration: .3s;transition-property:{{_cut_animation?'':'background'}};">
<view class="border border-top-left"></view>
<view class="border border-top-right"></view>
<view class="border border-right-top"></view>
<view class="border border-right-bottom"></view>
<view class="border border-bottom-right"></view>
<view class="border border-bottom-left"></view>
<view class="border border-left-bottom"></view>
<view class="border border-left-top"></view>
</view>
<view class='content_middle_right bg_gray {{_flag_bright?"":"bg_black"}}' style="transition-property:{{_cut_animation?'':'background'}}"></view>
</view>
<view class='content_bottom bg_gray {{_flag_bright?"":"bg_black"}}' style="transition-property:{{_cut_animation?'':'background'}}"></view>
</view>
<image bindload="imageLoad" bindtouchstart="_start" bindtouchmove="_move" bindtouchend="_end" style="width:{{img_width ? img_width + 'px' : 'auto'}};height:{{img_height ? img_height + 'px' : 'auto'}};transform:translate3d({{_img_left-img_width/2}}px,{{_img_top-img_height/2}}px,0) scale({{scale}}) rotate({{angle}}deg);transition-duration:{{_cut_animation?.4:0}}s;" class='img' src='{{imgSrc}}'></image>
</view>
<canvas canvas-id='image-cropper' disable-scroll="true" style="width:{{_canvas_width * export_scale}}px;height:{{_canvas_height * export_scale}}px;left:{{canvas_left}}px;top:{{canvas_top}}px" class='image-cropper-canvas'></canvas>
<view class="btn_box">
<view class="btn">
<button bindtap="cnacle" class="btn_cancle">取消</button>
</view>
<view class="btn">
<button bindtap="_click" class="btn_confirm">确定</button>
</view>
</view>
</view>
+144
View File
@@ -0,0 +1,144 @@
.image-cropper{
background:rgba(14, 13, 13,.8);
position: fixed;
top:0;
left:0;
width:100vw;
height:100vh;
z-index: 2;
}
.image-cropper .main{
position: absolute;
width:100vw;
height:100vh;
overflow: hidden;
}
.image-cropper .content{
z-index: 9;
position: absolute;
width:100vw;
height:100vh;
display: flex;
flex-direction:column;
pointer-events:none;
}
.image-cropper .bg_black{
background: rgba(0, 0, 0, 0.8)!important;
}
.image-cropper .bg_gray{
background: rgba(0, 0, 0, 0.45);
transition-duration: .35s;
}
.image-cropper .content>.content_top{
pointer-events:none;
}
.image-cropper .content>.content_middle{
display: flex;
height: 200px;
width:100%;
}
.image-cropper .content_middle_middle{
width:200px;
box-sizing:border-box;
position: relative;
transition-duration: .3s;
}
.image-cropper .content_middle_right{
flex: auto;
}
.image-cropper .content>.content_bottom{
flex: auto;
}
.image-cropper .img{
z-index: 2;
top:0;
left:0;
position: absolute;
border:none;
width:100%;
backface-visibility: hidden;
transform-origin:center;
}
.image-cropper .image-cropper-canvas{
position: fixed;
background: white;
width:150px;
height:150px;
z-index: 10;
top:-200%;
pointer-events:none;
}
.image-cropper .border{
background: white;
pointer-events:auto;
position:absolute;
}
.image-cropper .border-top-left{
left:-2.5px;
top:-2.5px;
height:2.5px;
width:33rpx;
}
.image-cropper .border-top-right{
right:-2.5px;
top:-2.5px;
height:2.5px;
width:33rpx;
}
.image-cropper .border-right-top{
top:-1px;
width:2.5px;
height:30rpx;
right:-2.5px;
}
.image-cropper .border-right-bottom{
width:2.5px;
height:30rpx;
right:-2.5px;
bottom:-1px;
}
.image-cropper .border-bottom-left{
height:2.5px;
width:33rpx;
bottom:-2.5px;
left:-2.5px;
}
.image-cropper .border-bottom-right{
height:2.5px;
width:33rpx;
bottom:-2.5px;
right:-2.5px;
}
.image-cropper .border-left-top{
top:-1px;
width:2.5px;
height:30rpx;
left:-2.5px;
}
.image-cropper .border-left-bottom{
width:2.5px;
height:30rpx;
left:-2.5px;
bottom:-1px;
}
.btn_box{
z-index: 9;
position: absolute;
display: flex;
bottom: 0rpx;
justify-content: center;
width: 100vw;
}
.btn{
font-weight: 600;
text-align: center;
margin: 60rpx 20rpx;
flex: 1;
}
.btn_cancle{
/* width: 100rpx; */
}
.btn_confirm{
background-color: #3CC7C0;
color: #fff;
}
+39 -3
View File
@@ -85,8 +85,11 @@ Component({
zhuanchang_note: '请选择专长',
jianjie_show: false,
shanchang_show: false,
show_cropper: false,
img_cropper_src: '',
img_cropper_width: 250, //宽度
img_cropper_height: 250, //高度
},
options: {
addGlobalClass: true,
styleIsolation: 'apply-shared'
@@ -901,6 +904,16 @@ Component({
})
},
doUploadPhoto(event) {
const { file } = event.detail;
this.setData({
photoEvent: event,
img_cropper_src: file.url,
show_cropper: true
})
this.loadimage()
},
OSSUpload(fileurl){
let event = this.data.photoEvent
console.log("index douploadFIle: ", event);
const scene = event.currentTarget.dataset.scene;
const field_name = event.currentTarget.dataset.field_name;
@@ -919,7 +932,7 @@ Component({
const ossAccessKeyId = response.data.accessid;
const policy = response.data.policy;
const key = response.data.dir+filename;
const filePath = file.url; // 待上传文件的文件路径。
const filePath = fileurl; // 待上传文件的文件路径。
wx.uploadFile({
url: host, // 开发者服务器的URL。
filePath: filePath,
@@ -982,6 +995,29 @@ Component({
},
moveZhuanChang(){
}
},
cropperload(e) {
console.log("cropper初始化完成");
},
loadimage(e) {
this.cropper = this.selectComponent("#image-cropper");
//重置图片角度、缩放、位置
this.cropper.imgReset();
this.triggerEvent('showPageMeta');
},
clickcut(e) {
console.log(e);
// //点击裁剪框阅览图片
// wx.previewImage({
// current: e.detail.url, // 当前显示图片的http链接
// urls: [e.detail.url] // 需要预览的图片http链接列表
// })
this.OSSUpload(e.detail.url)
this.setData({
show_cropper: false
})
this.triggerEvent('hidePageMeta');
},
}
})
+2 -1
View File
@@ -21,6 +21,7 @@
"van-icon": "@vant/weapp/icon/index",
"van-search": "@vant/weapp/search/index",
"van-empty": "@vant/weapp/empty/index",
"van-image": "@vant/weapp/image/index"
"van-image": "@vant/weapp/image/index",
"image-cropper": "/commpents/image-cropper/image-cropper"
}
}
+3 -1
View File
@@ -365,4 +365,6 @@
</view>
<van-toast id="van-toast" />
<van-toast id="van-toast" />
<image-cropper show_cropper="{{show_cropper}}" id="image-cropper" limit_move="{{true}}" disable_rotate="{{true}}" width="{{img_cropper_width}}" height="{{img_cropper_height}}" imgSrc="{{img_cropper_src}}" disable_height="{{true}}" disable_width="{{true}}" disable_ratio="{{true}}" min_scale="0.1" max_scale="5" bindload="cropperload" bindimageload="loadimage" bindtapcut="clickcut"></image-cropper>