• JS 图片绝对路径,转base6


    主要思想:

      使用canvas.toDataURL()将图片的绝对路径  转为 base64编码.

    一、图片在本地服务器:

    复制代码
    var imgSrc = "/img/1.jpg";//本地项目文件夹下的图片
          function getBase64(img){//传入图片路径,返回base64
            function getBase64Image(img,width,height) {
              var canvas = document.createElement("canvas");
              canvas.width = width ? width : img.width;
              canvas.height = height ? height : img.height;
              var ctx = canvas.getContext("2d");
              ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
              var dataURL = canvas.toDataURL();
              return dataURL;
            }
            var image = new Image();
            image.src = img;
            var deferred=$.Deferred();
            if(img){
              image.onload =function (){
                deferred.resolve(getBase64Image(image));//将base64传给done上传处理
              }
              return deferred.promise();//问题要让onload完成后再return sessionStorage['imgTest']
            }
          }
          getBase64(imgSrc)
            .then(function(base64){
              console.log(base64);
            },function(err){
              console.log(err);
            });
    复制代码

    二、跨域

    复制代码
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>nick getBase64</title>
    </head>
    <body>
    <div><img id="test" src="" alt=""/></div>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        var imgSrc = "https://img.alicdn.com/bao/uploaded/TB1qimQIpXXXXXbXFXXSutbFXXX.jpg";
    //    var imgSrc = "/img/1.jpg";
          function getBase64(img){//传入图片路径,返回base64
            function getBase64Image(img,width,height) {//width、height调用时传入具体像素值,控制大小 ,不传则默认图像大小
              var canvas = document.createElement("canvas");
              canvas.width = width ? width : img.width;
              canvas.height = height ? height : img.height;
     
              var ctx = canvas.getContext("2d");
              ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
              var dataURL = canvas.toDataURL();
              return dataURL;
            }
            var image = new Image();
            image.crossOrigin = '';
            image.src = img;
            var deferred=$.Deferred();
            if(img){
              image.onload =function (){
                deferred.resolve(getBase64Image(image));//将base64传给done上传处理
              }
              return deferred.promise();//问题要让onload完成后再return sessionStorage['imgTest']
            }
          }
          getBase64(imgSrc)
            .then(function(base64){
              console.log(base64);
            },function(err){
              console.log(err);
            });
    </script>
    </body>
    </html>
    复制代码

    转载来自:https://www.cnblogs.com/LChenglong/p/7691548.html

  • 相关阅读:
    Shared variable in python's multiprocessing
    File checksum
    Windows createprocess linux fork
    人工调用系统调用
    如何提高团队开发效率
    Redis' High Availability
    并发MD5计算方法
    开博宣言
    NYOJ 55 懒省事的小明
    HDU 1237 简单计算器
  • 原文地址:https://www.cnblogs.com/liuzheng0612/p/11636219.html
Copyright © 2020-2023  润新知