Files
ai_dianshang/miniprogram/components/webp-image/utils.wxs
2025-11-17 13:32:54 +08:00

154 lines
3.9 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var isString = function (value) {
return typeof value === 'string';
};
var isNumber = function (value) {
return typeof value === 'number';
};
var getFileExt = function (src) {
var fileUrl = src.split('?')[0];
var splitUlr = fileUrl.split('/');
var filepath = splitUlr[splitUlr.length - 1];
return filepath.split('.')[1] || 'jpg';
};
function isUrl(url) {
// NOCC:ToolNameCheck(非敏感词)
var urlReg = getRegExp(
'/[(http(s)?)://(www.)?a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/',
'ig',
);
return urlReg.test(url);
}
function rpx2px(rpx, screenWidth) {
// px / systemWidth = rpx / 750
var result = (rpx * (screenWidth || 375)) / 750;
return Math.round(result);
}
function imageMogr(url, options) {
if (!isString(url) || !url) return '';
if (
url.indexOf('qlogo.cn') !== -1 ||
url.indexOf('wxfile://') === 0 ||
url.indexOf('http://tmp/wx') === 0 ||
url.indexOf('imageMogr2') !== -1 ||
url.indexOf('tral.cc') !== -1 ||
url.indexOf('localhost') !== -1 ||
url.indexOf('192.168.') !== -1 ||
url.indexOf('127.0.0.1') !== -1
) {
//qlogo.cn域名、tral.cc域名、本地开发环境或者本地图片不做转换
return url;
} //强制转https
if (url.indexOf('http://') === 0) {
url = url.replace('http://', 'https://');
} else if (url.indexOf('//') === 0) {
url = 'https:' + url;
}
if (!options) return url;
var width = Math.ceil(options.width),
height = Math.ceil(options.height),
format = options.format,
_optionsQuality = options.quality,
quality = _optionsQuality === undefined ? 70 : _optionsQuality,
_optionsStrip = options.strip,
strip = _optionsStrip === undefined ? true : _optionsStrip,
crop = options.crop;
var isValidWidth = isNumber(width) && width > 0;
var isValidHeight = isNumber(height) && height > 0;
var imageMogrStr = '';
var size = '';
if (isValidWidth && isValidHeight) {
size = ''.concat(width, 'x').concat(height);
} else if (isValidWidth) {
size = ''.concat(width, 'x');
} else if (isValidHeight) {
size = 'x'.concat(height);
}
if (size) {
//缩放或者裁剪
imageMogrStr += '/'.concat(crop ? 'crop' : 'thumbnail', '/').concat(size);
if (crop) {
//裁剪目前需求只有以图片中心为基准
imageMogrStr += '/gravity/center';
}
}
if (isNumber(quality)) {
//质量变换
imageMogrStr += '/quality/'.concat(quality);
}
if (strip) {
//去除元信息
imageMogrStr += '/strip';
}
var ext = getFileExt(url);
// gif 图片不做格式转换,否则会损坏动图
if (ext === 'gif') {
imageMogrStr += '/cgif/1';
} else if (format) {
//格式转换
imageMogrStr += '/format/'.concat(format);
}
if (format === 'jpg' || (!format && (ext === 'jpg' || ext === 'jpeg'))) {
//渐进式 jpg 加载
imageMogrStr += '/interlace/1';
}
if (!imageMogrStr) return url;
return ''
.concat(url)
.concat(url.indexOf('?') !== -1 ? '&' : '?', 'imageMogr2')
.concat(imageMogrStr);
}
function getSrc(options) {
if (!options.src) return '';
// 检查是否为本地开发环境如果是则直接返回原始URL
if (
options.src.indexOf('localhost') !== -1 ||
options.src.indexOf('192.168.') !== -1 ||
options.src.indexOf('127.0.0.1') !== -1
) {
return options.src;
}
if (options.thumbWidth || options.thumbHeight) {
return imageMogr(options.src, {
width:
options.mode !== 'heightFix'
? rpx2px(options.thumbWidth, options.systemInfo.screenWidth) *
options.systemInfo.pixelRatio
: null,
height:
options.mode !== 'widthFix'
? rpx2px(options.thumbHeight, options.systemInfo.screenWidth) *
options.systemInfo.pixelRatio
: null,
format: options.webp ? 'webp' : null,
});
}
return '';
}
module.exports = {
imageMogr: imageMogr,
getSrc: getSrc,
};