-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
56 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,62 @@ | ||
//压缩图片 | ||
const resize = function(imagePath, newW, newH, callback) { | ||
// #ifdef APP-PLUS | ||
new Promise((res) => { | ||
var localPath = plus.io.convertAbsoluteFileSystem(imagePath.replace('file://', '')) | ||
plus.io.resolveLocalFileSystemURL(localPath, (entry) => { | ||
entry.file((file) => { | ||
plus.zip.compressImage({ | ||
src: imagePath, | ||
dst: imagePath.replace('.jpg', '2.jpg').replace('.JPG', '2.JPG').replace('.png', '2.png').replace('.PNG','2.PNG'), | ||
width: newW, | ||
height: newH, | ||
quality: 90, | ||
overwrite: true | ||
}, (event) => { | ||
let newImg = imagePath.replace('.jpg', '2.jpg').replace('.JPG', '2.JPG').replace('.png', '2.png').replace('.PNG', '2.PNG') | ||
callback(newImg) | ||
}, function(error) { | ||
uni.showModal({ | ||
content: '图片太大,请重新选择图片!', | ||
showCancel: false | ||
}) | ||
//拍一张图片或从相册取图片并等比缩小优化 | ||
const chooseImage = function(minSideSize, callback) { | ||
uni.chooseImage({ | ||
count: 1, | ||
sourceType: ['album', 'camera'], | ||
sizeType: ['compressed', 'original'], | ||
success: (res) => { | ||
resize(res.tempFilePaths[0], minSideSize, function(newPath) { | ||
callback(newPath); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
|
||
//压缩图片(minSideSize自动付给更小的边,另外一边会按比例缩放) | ||
const resize = function(imgPath, minSideSize, callback) { | ||
uni.getImageInfo({ | ||
src: imgPath, | ||
success: function(img) { | ||
let newW, newH; | ||
if (img.width > img.height) { | ||
newW = minSideSize * img.width / img.height; | ||
newH = minSideSize; | ||
} else if (newH <= 0) { | ||
newW = minSideSize; | ||
newH = minSideSize * img.height / img.width; | ||
} | ||
plus.io.resolveLocalFileSystemURL(plus.io.convertAbsoluteFileSystem(imgPath.replace('file://', '')), (entry) => { | ||
entry.file((file) => { | ||
let compressedPath = imgPath.replace('.jpg', '2.jpg').replace('.JPG', '2.JPG').replace('.jpeg', '2.jpeg').replace( | ||
'.JPEG', '2.JPEG').replace('.png', '2.png').replace('.PNG', '2.PNG'); | ||
plus.zip.compressImage({ | ||
src: imgPath, | ||
dst: compressedPath, | ||
width: newW, | ||
height: newH, | ||
quality: 90, | ||
overwrite: true | ||
}, (event) => { | ||
callback(compressedPath); | ||
}, function(error) { | ||
uni.showModal({ | ||
content: '分享图片太大,需要请重新选择图片!', | ||
showCancel: false | ||
}) | ||
}); | ||
}); | ||
}, (e) => { | ||
uni.showModal({ | ||
content: '分享图片太大,需要请重新选择图片!', | ||
showCancel: false | ||
}) | ||
}) | ||
}, (error) => { | ||
console.error('出错了: ' + error.message) | ||
uni.showModal({ | ||
content: '图片太大,请重新选择图片!', | ||
showCancel: false | ||
}) | ||
}) | ||
}) | ||
return; | ||
// #endif | ||
callback(imagePath); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export default { | ||
chooseImage, | ||
resize | ||
} |