zoukankan      html  css  js  c++  java
  • 图片压缩传输

    在注册时有时需要传输身份证照片,但现在手机相片动不动就是5,6兆,传输速度太慢,因此压缩一下。

    对图片压缩

     1 var canvas = document.createElement("canvas");
     2 var ctx = canvas.getContext('2d');
     3 // 瓦片canvas
     4 var tCanvas = document.createElement("canvas");
     5 var tctx = tCanvas.getContext("2d");
     6 var maxsize = 100 * 1024;
     7 //使用canvas对大图片进行压缩
     8 function compress(img) {
     9     //var initSize = img.src.length;
    10     var width = img.width;
    11     var height = img.height;
    12     var bili = 1;
    13     if (width > 480) {
    14         bili = 480 / width;
    15     } else {
    16         if (height > 640) {
    17             bili = 640 / height;
    18         } else {
    19             bili = 1;
    20         }
    21     }
    22     //如果图片大于四百万像素,计算压缩比并将大小压至400万以下
    23     var ratio;
    24     if ((ratio = width * height / 4000000) > 1) {
    25         ratio = Math.sqrt(ratio);
    26         width /= ratio;
    27         height /= ratio;
    28     } else {
    29         ratio = 1;
    30     }
    31     canvas.width = width;
    32     canvas.height = height;
    33     // 铺底色
    34     ctx.fillStyle = "#fff";
    35     ctx.fillRect(0, 0, canvas.width, canvas.height);
    36 
    37     //如果图片像素大于100万则使用瓦片绘制
    38     var count;
    39     if ((count = width * height / 1000000) > 1) {
    40         count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
    41         //计算每块瓦片的宽和高
    42         var nw = ~~(width / count);
    43         var nh = ~~(height / count);
    44         tCanvas.width = nw;
    45         tCanvas.height = nh;
    46         for (var i = 0; i < count; i++) {
    47             for (var j = 0; j < count; j++) {
    48                 tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
    49                 ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
    50             }
    51         }
    52     } else {
    53         ctx.drawImage(img, 0, 0, width, height);
    54     }
    55     //进行最小压缩
    56     var ndata = canvas.toDataURL('image/jpeg', bili);
    57     tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
    58     return ndata;
    59 }

    在控件选中图片时,对图片调用这个方法,常常获取不到图片的长宽,作如下操作

           //创建对象
            var img2 = new Image();
            img2.onload = function () {
                img2 = compress(img2);
    
                //document.getElementById("imgPhoto").setAttribute("value", img2);
            }
            // 改变图片的src
            img2.src = imgArr[i];

    重大发现,由于前段选择文件时通常是 <input  type="file">  ,因此上面获取图片长宽的方法变了变

      var myimg=URL.createObjectURL(file);
      var img=new Image();
      img.src=myimg;
      img.onload=function()
          {
         var base64 =    that.compress(img);
           }

    其中的 file  就是用 input 框选中的文件。

    另外测了一下压缩的能力。 本来 5M  的图片, 大概压缩到原来的三分之一。压缩能力还是可以的。

    此时便可以获取压缩过后的img的Base64位编码

    将此编码设置为图片的Src属性,可以显示成图片。

    在进行表单提交时有时文件过大,表单会提交失败,

    1   <system.web>
    2     <httpRuntime maxRequestLength="102400" executionTimeout="360"/>
    3   </system.web>

    设置以上属性便可。

    设置最大 HTTP 请求大小限制

    • 打开 IIS 控制台
    • 双击 ASP,展开限制属性,修改醉倒请求实体主体限制为需要的值(如10240000 即 10M) > ASP 文件中也有上传文件大小的限制,不过先验证的限制是 IIS 中设置的,所以如果 IIS 中设置最大 256K,那么就算 ASP 中设置了最大 10M,那么超过 256K 的文件也无法上传,而且 ASP 没法给出错误信息。

    UEditor     给Action加一个Attribute:[ValidateInput(false)],这样只会让该页面不验证提交的内容,而不会影响到其他页面。

    在表单提交时如果设置  input  属性  disabled=true   则这项数据不会被提交 , disabled=false  则会提交

     string ImgPath = HttpContext.Current.Server.MapPath("/idcard/" + PicID + ".jpg");
                 Base64StringToImage(ImgBase64, ImgPath);
     1         private static void Base64StringToImage(string inputStr,string  ImgPath )
     2         {
     3             if (string.IsNullOrWhiteSpace(inputStr))
     4                 return;
     5             try
     6             {
     7                 string filePath = ImgPath;
     8                 byte[] arr = Convert.FromBase64String(inputStr.Substring(inputStr.IndexOf("base64,") + 7).Trim(''));
     9                 using (MemoryStream ms = new MemoryStream(arr))
    10                 {
    11                     Bitmap bmp = new Bitmap(ms);
    12                     //新建第二个bitmap类型的bmp2变量。
    13                     Bitmap bmp2 = new Bitmap(bmp, bmp.Width, bmp.Height);
    14                     //将第一个bmp拷贝到bmp2中
    15                     Graphics draw = Graphics.FromImage(bmp2);
    16                     draw.DrawImage(bmp, 0, 0);
    17                     draw.Dispose();
    18                     bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    19                     ms.Close();
    20                 }
    21 
    22             }
    23             catch (Exception ex)
    24             {
    25 
    26             }
    27         }
    28    

    C# 后台进行保存图片。

  • 相关阅读:
    oracle在线重定义表
    Oracle 分区表
    oracle表空间不足扩容的方法
    oracle查看锁表进程,杀掉锁表进程[转载]
    创建sequence和触发器出现权限不足
    百万级数据库优化方案[转载]
    oracle 表空间
    数据库MySQL——SQL语句(命令)
    HTTP状态码
    http和https
  • 原文地址:https://www.cnblogs.com/cwmizlp/p/9855202.html
Copyright © 2011-2022 走看看