Skip to content

Commit

Permalink
no description
Browse files Browse the repository at this point in the history
  • Loading branch information
wuqibo committed Apr 17, 2019
1 parent 2a8ad82 commit 24124af
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions js/texture_utils.js
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
}

0 comments on commit 24124af

Please sign in to comment.