|
@@ -1,157 +1,74 @@
|
|
|
<template>
|
|
|
- <div>
|
|
|
- <van-uploader v-model="fileList"
|
|
|
- multiple
|
|
|
- :after-read="afterRead" />
|
|
|
- <van-image width="100"
|
|
|
- height="100"
|
|
|
- :src="src" />
|
|
|
+ <div class="dome">
|
|
|
+ <div class="cropper">
|
|
|
+ <img :src="img" class="img" />
|
|
|
+ <!-- option是配置,格式是对象,getbase64Data是组件的一个方法获取裁剪完的头像 2.14新增一个获取getblobData的方法 -->
|
|
|
+ <h5-cropper :option="option" @getbase64Data="getbase64Data" @getblobData="getblobData" @getFile="getFile"></h5-cropper>
|
|
|
+ </div>
|
|
|
+ <div class="info">
|
|
|
+ <div>作者:居里栈栈</div>
|
|
|
+ <div>Wechat:812936565</div>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
</template>
|
|
|
+
|
|
|
<script>
|
|
|
-import Exif from 'exif-js'
|
|
|
+import H5Cropper from "vue-cropper-h5";
|
|
|
export default {
|
|
|
- name: 'PolicyContent',
|
|
|
- components: {
|
|
|
-
|
|
|
- },
|
|
|
- data () {
|
|
|
+ components: { H5Cropper },
|
|
|
+ data() {
|
|
|
return {
|
|
|
- fileList: [],
|
|
|
- src: ""
|
|
|
- }
|
|
|
+ option: {
|
|
|
+ canScale: true,
|
|
|
+ outputSize: 0.6
|
|
|
+ }, //配置
|
|
|
+ img:
|
|
|
+ "http://geren.yi-school.top/images/logo.png"
|
|
|
+ };
|
|
|
},
|
|
|
methods: {
|
|
|
- afterRead (file) {
|
|
|
- let Orientation = null;
|
|
|
- //获取照片方向角属性,用户旋转控制
|
|
|
- var self = this;
|
|
|
- Exif.getData(file.file, function () {
|
|
|
- Exif.getAllTags(this);
|
|
|
- Orientation = Exif.getTag(this, 'Orientation');
|
|
|
- self.resetOrientation(file.content, Orientation, self.callbackBase64Img);
|
|
|
- });
|
|
|
- },
|
|
|
- callbackBase64Img (callbackImg) {
|
|
|
- var img = new Image();
|
|
|
- var self = this
|
|
|
- img.onload = function () {
|
|
|
- self.src = self.compress(img)
|
|
|
- }
|
|
|
- img.src = callbackImg;
|
|
|
+ getbase64Data(data) {
|
|
|
+ this.img = data;
|
|
|
},
|
|
|
- resetOrientation (srcBase64, srcOrientation, callback) {
|
|
|
- alert(srcOrientation)
|
|
|
- var img = new Image();
|
|
|
-
|
|
|
- img.onload = function () {
|
|
|
- var width = img.width,
|
|
|
- height = img.height,
|
|
|
- canvas = document.createElement('canvas'),
|
|
|
- ctx = canvas.getContext("2d");
|
|
|
-
|
|
|
- // 可以设置最大大小
|
|
|
- var MAX_WIDTH = 1024;
|
|
|
- var MAX_HEIGHT = 768;
|
|
|
- if (width / MAX_WIDTH > height / MAX_HEIGHT) {
|
|
|
- if (width > MAX_WIDTH) {
|
|
|
- height *= MAX_WIDTH / width;
|
|
|
- width = MAX_WIDTH;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (height > MAX_HEIGHT) {
|
|
|
- width *= MAX_HEIGHT / height;
|
|
|
- height = MAX_HEIGHT;
|
|
|
- }
|
|
|
- }
|
|
|
- // set proper canvas dimensions before transform & export
|
|
|
- if ([5, 6, 7, 8].indexOf(srcOrientation) > -1) {
|
|
|
- canvas.width = height;
|
|
|
- canvas.height = width;
|
|
|
- } else {
|
|
|
- canvas.width = width;
|
|
|
- canvas.height = height;
|
|
|
- }
|
|
|
-
|
|
|
- // transform context before drawing image
|
|
|
- switch (srcOrientation) {
|
|
|
- case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;
|
|
|
- case 3: ctx.transform(-1, 0, 0, -1, width, height); break;
|
|
|
- case 4: ctx.transform(1, 0, 0, -1, 0, height); break;
|
|
|
- case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
|
|
|
- case 6: ctx.transform(0, 1, -1, 0, height, 0); break;
|
|
|
- case 7: ctx.transform(0, -1, -1, 0, height, width); break;
|
|
|
- case 8: ctx.transform(0, -1, 1, 0, 0, width); break;
|
|
|
- default: ctx.transform(1, 0, 0, 1, 0, 0);
|
|
|
- }
|
|
|
-
|
|
|
- // draw image
|
|
|
- ctx.drawImage(img, 0, 0, width, height);
|
|
|
+ getblobData() {
|
|
|
|
|
|
- // export base64
|
|
|
- callback(canvas.toDataURL());
|
|
|
- };
|
|
|
-
|
|
|
- img.src = srcBase64;
|
|
|
- },
|
|
|
- compress (img) {
|
|
|
- let canvas = document.createElement('canvas')
|
|
|
- let ctx = canvas.getContext('2d')
|
|
|
- //瓦片canvas
|
|
|
- let tCanvas = document.createElement('canvas')
|
|
|
- let tctx = tCanvas.getContext('2d')
|
|
|
- //let initSize = img.src.length
|
|
|
- let width = img.width
|
|
|
- let height = img.height
|
|
|
- //如果图片大于四百万像素,计算压缩比并将大小压至400万以下
|
|
|
- let ratio
|
|
|
- if ((ratio = (width * height) / 3500000) > 1) {
|
|
|
- //console.log('大于400万像素')
|
|
|
- ratio = Math.sqrt(ratio)
|
|
|
- width /= ratio
|
|
|
- height /= ratio
|
|
|
- } else {
|
|
|
- ratio = 1
|
|
|
- }
|
|
|
- canvas.width = width
|
|
|
- canvas.height = height
|
|
|
- // 铺底色
|
|
|
- ctx.fillStyle = '#fff'
|
|
|
- ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
|
- //如果图片像素大于100万则使用瓦片绘制
|
|
|
- let count
|
|
|
- if ((count = (width * height) / 1000000) > 1) {
|
|
|
- //console.log('超过100W像素')
|
|
|
- count = ~~(Math.sqrt(count) + 1) //计算要分成多少块瓦片
|
|
|
- // 计算每块瓦片的宽和高
|
|
|
- let nw = ~~(width / count)
|
|
|
- let nh = ~~(height / count)
|
|
|
- tCanvas.width = nw
|
|
|
- tCanvas.height = nh
|
|
|
- for (let i = 0; i < count; i++) {
|
|
|
- for (let j = 0; j < count; j++) {
|
|
|
- tctx.drawImage(
|
|
|
- img,
|
|
|
- i * nw * ratio,
|
|
|
- j * nh * ratio,
|
|
|
- nw * ratio,
|
|
|
- nh * ratio,
|
|
|
- 0,
|
|
|
- 0,
|
|
|
- nw,
|
|
|
- nh
|
|
|
- )
|
|
|
- ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- ctx.drawImage(img, 0, 0, width, height)
|
|
|
- }
|
|
|
- //进行最小压缩
|
|
|
- let ndata = canvas.toDataURL('image/jpeg', 0.5)
|
|
|
- tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0
|
|
|
- return ndata
|
|
|
},
|
|
|
- },
|
|
|
+ getFile(file) {
|
|
|
+ console.log(file)
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped>
|
|
|
+.dome {
|
|
|
+ display: flex;
|
|
|
+ justify-content: space-between;
|
|
|
+ padding-left: 22px;
|
|
|
+}
|
|
|
+.cropper {
|
|
|
+ width: 80px;
|
|
|
+ height: 80px;
|
|
|
+ line-height: 80px;
|
|
|
+ /* 切记position: relative一定要有 */
|
|
|
+ position: relative;
|
|
|
+ border-radius: 80px;
|
|
|
+ overflow: hidden;
|
|
|
+ text-align: center;
|
|
|
+}
|
|
|
+.img {
|
|
|
+ position: absolute;
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+ left: 0;
|
|
|
+ top: 0;
|
|
|
+}
|
|
|
+.info {
|
|
|
+ font-size: 18px;
|
|
|
+ height: 40px;
|
|
|
+ line-height: 40px;
|
|
|
+ margin-left: 30px;
|
|
|
+ flex: 1;
|
|
|
+ text-align: left;
|
|
|
}
|
|
|
-</script>
|
|
|
+</style>
|