• 微信小程序,获取网络SVG图片宽高算法


    该算法来源于小程序开发社区一位大佬帮我搞的.

    /**
       * 获取网络SVG图片宽高
       * @param src SVG网络资源链接
       */
      public static GetSvgInfoFormUrl = (src: string): Promise<any> =>
        new Promise((resolve, reject) => {
          request({ url: src }).then((res: any) => {
            const { data } = res;
            // <svg height="" width=""
            const r1: RegExp = /(?:[Ss]+)?<svg(?=.*(height)="(d+)(?:[^"]+)?")(?=.*(width)="(d+)(?:[^"]+)?")(?:[Ss]+)?/;
            // <svg viewBox="0 0 1103 711"
            const r2: RegExp = /(?:[Ss]+)?viewBox="d+ d+ (d+) (d+)"(?:[Ss]+)?/;
            // <svg style="height:***px;***px"
            const r3: RegExp = /(?:[Ss]+)?(?:<svg[^>]+style="(?=.*(height):(d+)(?:[^>]+)?)(?=.*(width):(d+)(?:[^>]+)?))(?:[Ss]+)?/;
            let str: string = '{}';
            let info: {  number; height: number } = {  0, height: 0 };
            let isSuccess: boolean = false;
            try {
              str = data
                .replace(r1, '{"$1":$2,"$3":$4}')
                .replace(/$(2|4)/, '0')
                .replace(/$1/, 'height')
                .replace(/$3/, 'width');
              if (str.length > 45) isSuccess = false;
              else {
                isSuccess = true;
                info = JSON.parse(str);
              }
              if (!isSuccess) {
                str = data.replace(r2, '{"width":$1,"height":$2}').replace(/$(1|2)/, '0');
                if (str.length > 45) isSuccess = false;
                else {
                  isSuccess = true;
                  info = JSON.parse(str);
                }
                console.log('r2', info);
              }
              if (!isSuccess) {
                str = data
                  .replace(r3, '{"$1":$2,"$3":$4}')
                  .replace(/$(2|4)/, '0')
                  .replace(/$1/, 'height')
                  .replace(/$3/, 'width');
                if (str.length > 45) isSuccess = false;
                else {
                  isSuccess = true;
                  info = JSON.parse(str);
                }
                console.log('r3', info);
              }
            } catch (error) {
              console.log(error);
              reject(error);
            }
            if (isSuccess) resolve(info);
            else reject(new Error('无法正确解析SVG数据'));
          });
        });
  • 相关阅读:
    基于VLC的视频播放器
    IOS开发之新浪微博OAuth2
    Android之官方导航栏ActionBar
    IOS中键盘隐藏几种方式
    在Android中使用Android Ksoap2调用WebService
    Android之属性动画(二)
    IOS 内存管理
    利用scp 远程上传下载文件/文件夹和ssh远程执行命令
    Centos 检查磁盘读写性能
    JPA, JNDI, OSGi
  • 原文地址:https://www.cnblogs.com/dygood/p/12937624.html
Copyright © 2020-2023  润新知