import { isSupportWebp } from 'nw-detect'; /** * 解析url参数字符串,取得key对应的值 * @param {String} _url url字符串或hash串 * @param {String} _key key值 * @param {String} _separator 分隔符 * @return {String} key对应的value */ export function getParmvalByKey(url, _key, _separator) { let _url = url || ''; _separator = _separator || '?'; _url = _url.trim(); var _separatorIndex = _url.indexOf(_separator); if (_separatorIndex < 0) { return ''; } if (_separator != '#') { var _index = _url.indexOf('#') || 0; if (_index >= 0 && _separatorIndex < _index) { _url = _url.substring(0, _index); } } _url = _url.substr(_separatorIndex + 1); if (!_url) return ''; _url = _url.split('&'); for (var i = 0, l = _url.length, d; i < l; i++) { d = _url[i].indexOf('='); if (_url[i].substring(0, d) === _key) { return _url[i].substr(d + 1) || ''; } } return ''; } /** * 生成实时压缩图片地址 * */ export function imgResize(_imgUrl: string, _width: number, _height: number, _crop?: number, _enlarge?: boolean, _qulity?: number) { if (!_imgUrl) return null; _crop = _crop || 0; _enlarge = _enlarge || false; if (_width >= 300) { _qulity = _qulity || 95; } else { _qulity = _qulity || 90; } //LOFTER-11583: 从网易摄影导入的图片,在dashboard转载时,页面不能正常显示图片,不正确 //提取_imgUrl中真正的图片地址(剔除http://imgsize.ph.126.net/) if (_imgUrl.toLowerCase().indexOf('http://imgsize.ph.126.net') >= 0) { _imgUrl = getParmvalByKey(_imgUrl, 'imgurl', '?') || ''; var _index = _imgUrl.lastIndexOf('_'); if (!!_imgUrl && _index >= 0) { _imgUrl = _imgUrl.substring(0, _index); } if (!_imgUrl) return null; } if (!_width || _width < 0) return _imgUrl; if (!_height || _height < 0) _height = _width; _qulity = parseInt(_qulity) || 0; if (_qulity > 100) _qulity = 100; if (_imgUrl.indexOf('nos.netease.com') > 0 || _imgUrl.indexOf('nosdn.127.net') > 0 || _imgUrl.indexOf('nosdn0.126.net') > 0 || _imgUrl.indexOf('.lf126.net') > 0 || _imgUrl.indexOf('.lf127.net') > 0) { var resized = _imgUrl; var imageview = 'thumbnail=' + _width + (_crop == 1 ? 'y' : 'x') + _height + (!!_enlarge ? '&enlarge=1' : ''); if (/\.gif/.test(_imgUrl)) { if (_width < 500) { imageview += '&type=jpg'; } } else if (/\.png/.test(_imgUrl)) { //LOFTER-7537: PC端发布多图时解决PNG图片预览问题 imageview += '&type=png'; } else { imageview += '&type=jpg'; } if (/thumbnail=\d+[xyz]\d+/.test(_imgUrl)) { resized = _imgUrl.replace(/thumbnail=\d+[xyz]\d+/, imageview); } else if (/imageView/.test(_imgUrl)) { resized = _imgUrl.replace(/imageView/, 'imageView&' + imageview); } else if (/\?/.test(_imgUrl) && /#/.test(_imgUrl)) { resized = _imgUrl.replace(/#/, '&imageView&' + imageview + "#"); } else if (/\?/.test(_imgUrl) && !/#/.test(_imgUrl)) { resized = _imgUrl + '&imageView&' + imageview; } else { resized = _imgUrl + '?imageView&' + imageview; } //LOFTER-21319:修复专题中裁剪nos图片引起的水印大小异常 if (/watermark&/.test(resized)) { var _dx = 8; var _dy = 10; var _fontsize = 240; if (_width >= 1680) { _dx = 32; _dy = 36; _fontsize = 680; } else if (_width > 500) { _dx = 16; _dy = 20; _fontsize = 360; } else if (_width >= 300) { _dx = 8; _dy = 10; _fontsize = 240; } resized = resized.replace(/dx=\d+/, 'dx=' + _dx); resized = resized.replace(/dy=\d+/, 'dy=' + _dy); resized = resized.replace(/fontsize=\d+/, 'fontsize=' + _fontsize); } return resized; } else if (_imgUrl.indexOf('img.alicdn.com') >= 0) { // console.log('_imgUrl', _imgUrl); // alicdn的裁剪参数暂未找到文档,全部转成400x400的jpg,png有几率404 let resize = _imgUrl + '_400x400Q80.jpg' // console.log('resize', resize); return resize; } else { // ph.126.net 不在使用 // return 'http://imgsize.ph.126.net/?' + (!!_enlarge ? 'enlarge=true&' : '') + 'imgurl=' + _imgUrl + '_' + _width + 'x' + _height + 'x' + _crop + (_qulity > 0 ? ('x' + _qulity) : '') + '.jpg'; return _imgUrl; } } export const removeImgQuery = (url?: string) => { return url ? url.split('?')[0] : url } export const formatImage = (url: string, options: { width: number, height: number, crop?: boolean, enlarge?: boolean, quality?: number, } = { width: 1125, height: 0, crop: false, enlarge: true, quality: 75 }) => { if (!url) return url; const { width = 1125, height = 0, crop = false, enlarge = true, quality = 75, } = options; url = removeImgQuery(url); if (url.indexOf('nos.netease.com') > 0 || url.indexOf('nosdn.127.net') > 0 || url.indexOf('.lf126.net') > 0 || url.indexOf('.lf127.net') > 0) { // nos图片 const isGif = /\.gif/.test(url); const webp = isSupportWebp(); const size = `&thumbnail=${width}${(crop ? 'y' : 'x')}${height}${enlarge ? '&enlarge=1' : ''}` let type = '&typefallback=jpg'; if (webp) { type += '&type=webp'; if (isGif) { type += '&tostatic=0'; } } url += `?imageView${size}${quality !== 75 ? '&quality=' + quality : ''}${type}` } else if (url.indexOf('img.alicdn.com') > 0) { // alicdn图片 url += '_400x400Q80.jpg' } return url; }