JS实现压缩上传图片base64长度功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JS实现压缩上传图片base64长度功能

慕尘   2020-05-17 我要评论

这篇文章主要介绍了js实现压缩上传图片base64长度功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

im发送图片,现将图片压缩再上传

1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象.

2) 在image对象的 onload 事件中, 通过 canvas 的 canvas.getContext('2d') 的 drawImage 方法, 将Image 改变大小绘制到canvas上.

3) 通过 canvas.toDataURL("image/jpeg", 1); 方法, 将图片变成base64字符串, 放到send_image_value

toDataURL

canvas.toDataURL(type, encoderOptions); 

返回值

包含 data URI 的DOMString

type

图片格式,默认为 image/png

encoderOptions

在指定图片格式为 image/jpeg 或 image/webp的情况下,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他参数会被忽略。

html

<input id="sendImage" title="send a picture" type="file" accept="image/*" onchange="imgChange(this)">
 <input type="hidden" value="" id="send_image_value">
 <img id="showLoadingimg" src="'+CHAT_SITE_URL+'/templateshttps://img.qb5200.com/download-x/default/images/loading.gif" style="position:relative;left:200px;top:200px;z-index:999;display:none;">

js

function imgChange(e){
  //检查是否有文件被选中
    if (e.files.length != 0) {
      var file = e.files[0],
      fileType = file.type,
      reader = new FileReader();
      if (!reader) {
        e.value = '';
        return;
      };
      var size = file.size;
      var max_size = 2*1024*1024;
      if(size>max_size){
        e.value = '';
        $("#send_alert").html('file is too large(>2M)');
        return;
      }
      $("#showLoadingimg").show();
      reader.onload = function(e) {
        //读取成功,显示到页面并发送到服务器
        e.value = '';
        var org_img = e.target.result;
        var image_base64 = org_img;
        if(size>1024*80){//>80K的
          var img = new Image(); 
          img.src = org_img; 
          img.onload=function(){
            var scale = 1;  
            if(this.width > 300 || this.height > 300){   
              if(this.width > this.height){  
                scale = 300 / this.width; 
              }else{  
                scale = 300 / this.height;  
              }  
            } 
            var canvas = document.createElement("canvas"), drawer = canvas.getContext("2d");
            if(scale!=1) {//按最大高度等比缩放
              img.width *= scale;
              img.height *= scale;
            }
            canvas.width = img.width;
            canvas.height = img.width * (img.height / img.width);
            drawer.drawImage(img, 0, 0, canvas.width, canvas.height);
            var tmp_code = image_base64 = canvas.toDataURL(fileType);
            if(tmp_code!='data:,'){
              image_base64 = tmp_code;
            }
            img_send(image_base64);
          }; 
             
        }else{
          img_send(image_base64);
        }
         
      };
      reader.readAsDataURL(file);
    }
  }
  /**
   *为将图片赋值给消息
  **/
  function img_send(image_base64){
    if(image_base64!='data:,'){
      $("#send_image_value").val(image_base64);
      send_msg();
      $("#showLoadingimg").hide();
      $('#sendImage').val("");
    }
  }  

开始时,toDataURL获取的值是data:,

$("#send_image_value").val(image_base64);
send_msg();

这两句写在

reader.onload方法的最下面这就导致图片并没有压缩

因为img.onload还没执行完

发送的还是原来的图片

在调整后就可以实现图片的压缩了

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们