首页 > 解决方案 > 使用画布压缩图像

问题描述

我正在尝试使用画布进行图像压缩,但 safari 无法正常工作。谷歌浏览器运行良好。

ResizeImage = function (base64data, index, size, orientation) {
  jQuery("#" + self.canvas).after('<canvas style="display:none" id="UsedCanvas"></canvas>');

  var canvas = document.getElementById("UsedCanvas");
  var ctx = canvas.getContext("2d");
  var image = new Image();
  image.src = base64data;
  image.onload = function () {
    canvas.width = image.width;
    canvas.height = image.height;
    var height = canvas.height,
      width = canvas.width;
    if (orientation > 4) {
      canvas.width = height;
      canvas.height = width;
    }
    if (canvas.width > 1600 || canvas.height > 1200) {
    if (canvas.width > canvas.height) {
      var newheight = (1600 * canvas.height) / canvas.width;
      canvas.height = newheight;
      canvas.width = 1600;
    } else {
      var newwidth = (1600 * canvas.width) / canvas.height;
      canvas.width = newwidth;
      canvas.height = 1600;
    }
  }
    ProcessNewImage(canvas.toDataURL('image/jpeg', 0.7));
  };
};

根据我的研究,datatourl 参数质量不适用于 safari。

但我没有找到替代方案。

标签: javascriptcanvashtml5-canvas

解决方案


推荐阅读