Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Chrome中可以调整图像大小,但在Firefox中不能_Javascript_Angularjs - Fatal编程技术网

Javascript 在Chrome中可以调整图像大小,但在Firefox中不能

Javascript 在Chrome中可以调整图像大小,但在Firefox中不能,javascript,angularjs,Javascript,Angularjs,我的Angular 1.5应用程序中有一个函数,如果base64编码的图像超过最大尺寸,它可以调整图像大小。这个函数在Chrome中非常有效,但在Firefox中它返回一个空字符串,而不是任何base64编码的字符串 但相关功能如下: // Image resizing $scope.resizeImage = function(base64Data, maxWidth, maxHeight) { img = document.createElement('img');

我的Angular 1.5应用程序中有一个函数,如果base64编码的图像超过最大尺寸,它可以调整图像大小。这个函数在Chrome中非常有效,但在Firefox中它返回一个空字符串,而不是任何base64编码的字符串

但相关功能如下:

  // Image resizing
  $scope.resizeImage = function(base64Data, maxWidth, maxHeight) {
    img = document.createElement('img');

    img.src = base64Data;
    height = img.height;
    width = img.width;

    if (width > maxWidth) {
      ratio = maxWidth / width; // get ratio for scaling image
      height = height * ratio; // Reset height to match scaled image
      width = width * ratio; // Reset width to match scaled image
    }

    // Check if current height is larger than max
    if (height > maxHeight) {
      ratio = maxHeight / height; // get ratio for scaling image
      width = width * ratio; // Reset width to match scaled image
      height = height * ratio; // Reset height to match scaled image
    }

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

    // We set the dimensions at the wanted size.
    canvas.width = width;
    canvas.height = height;

    // We resize the image with the canvas method drawImage();
    ctx.drawImage(img, 0, 0, width, height);

    var dataURI = canvas.toDataURL();
    return dataURI;
  }

您可能需要等待
加载:

img.onload = function() {
    // now your image is ready for use
};