js---使用canvas压缩照片,图片旋转回正位

/**

* 使用canvas压缩照片

* @param img 需要压缩的照片

* @param Orientation 照片原始方向

* @returns {string} base64格式照片数据

*/

function compress(img, Orientation) {

      var canvas =document.createElement("canvas");

      var ctx = canvas.getContext('2d');

      //瓦片canvas

      var tCanvas =document.createElement("canvas");

      var tctx = tCanvas.getContext("2d");

      // var initSize = img.src.length;

      var width = img.width;

      var height = img.height;

      //图片像素大于400万像素,计算压缩到400万以下

      var ratio;

      if ((ratio = width * height /4000000) >1) {

        ratio = Math.sqrt(ratio);

        width /= ratio;

        height /= ratio;

      }else {

        ratio =1;

      }

      canvas.width = width;

      canvas.height = height;

      ctx.fillStyle ="#fff";

      ctx.fillRect(0, 0, canvas.width, canvas.height);

      //如果图片太大(大于100万像素)则使用瓦片绘制

      var count;

      if ((count = width * height /1000000 >1)) {

          count = ~~(Math.sqrt(count) +1);//计算分成的瓦片数

          var nw = ~~(width / count);

          var nh = ~~(height / count);

          tCanvas.width = nw;

          tCanvas.height = nh;

          for (var i =0; i < count; i++) {

            for (var j =0; j < count; j++) {

                tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);

                ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)

            }

        }

    }else {

        ctx.drawImage(img, 0, 0, width, height)

    }

    //修复ios上传图片的时候 被旋转的问题

    if (Orientation !="" && Orientation !=1) {

        switch (Orientation) {

        case 6://需要顺时针(向左)90度旋转

            this.rotateImg(img, "left", canvas);

            break;

        case 8://需要逆时针(向右)90度旋转

            this.rotateImg(img, "right", canvas);

            break;

        case 3://需要180度旋转

            this.rotateImg(img, "right", canvas); //转两次

            this.rotateImg(img, "right", canvas);

            break;

      }

    }

    //进行最小压缩

    var ndata = canvas.toDataURL('image/jpeg', 0.1);

    tCanvas.width = tCanvas.height = canvas.width = canvas.height =0;

    return ndata;

}

/**

* 旋转照片

* @param img 需要旋转的照片

* @param direction 旋转方向

* @param canvas 绘图canvas

*/

function rotateImg(img, direction, canvas) {

    //最小与最大旋转方向,图片旋转4次后回到原方向

    const min_step =0;

    const max_step =3;

    if (img ==null)return;

    //img的高度和宽度不能在img元素隐藏后获取,否则会出错

    let height = img.height;

    let width = img.width;

    let step =2;

    if (step ==null) {

        step = min_step;

    }

    if (direction =="right") {

        step++;

        //旋转到原位置,即超过最大值

        step > max_step && (step = min_step);

    }else {

        step--;

        step < min_step && (step = max_step);

    }

    //旋转角度以弧度值为参数

    let degree = (step *90 * Math.PI) /180;

    let ctx = canvas.getContext("2d");

    switch (step) {

        case 0:

            canvas.width = width;

            canvas.height = height;

            ctx.drawImage(img, 0, 0);

            break;

        case 1:

            canvas.width = height;

            canvas.height = width;

            ctx.rotate(degree);

            ctx.drawImage(img, 0, -height);

            break;

        case 2:

            canvas.width = width;

            canvas.height = height;

            ctx.rotate(degree);

            ctx.drawImage(img, -width, -height);

            break;

        case 3:

            canvas.width = height;

            canvas.height = width;

            ctx.rotate(degree);

            ctx.drawImage(img, -width, 0);

            break;

    }

}

你可能感兴趣的:(js---使用canvas压缩照片,图片旋转回正位)