简体   繁体   English

尝试通过 canvas.toDataURL 压缩 img

[英]Trying to compress img via canvas.toDataURL

I'm trying to work out img/canvas scaling but it's not working.我正在尝试解决 img/canvas 缩放,但它不起作用。

I've got an image, and I want to compress it, so I'm using canvas to do this using the following code:我有一个图像,我想压缩它,所以我使用 canvas 使用以下代码执行此操作:

<img id='srcimg'>
<img id='copyimg'>

And

function doSendToCanvasThenCopyImg(srcimg, destimg) {
  var scale = 1;
  var natW = srcimg.naturalWidth;
  var natH = srcimg.naturalHeight;
  var newCanvas = document.createElement('canvas');
  newCanvas.width = parseInt(natW * scale);
  newCanvas.height = parseInt(natH * scale);

  var ctx = newCanvas.getContext("2d");
  ctx.drawImage(srcimg, 0, 0, natW, natH, 0, 0, newCanvas.width, newCanvas.height);

  var base64 = newCanvas.toDataURL('image/jpeg', 1);
  destimg.src = base64;
}

And

function scaleToFit(img, newW, natW, natH) {
  var scale = newW / natW;

  var newCanvas = document.createElement('canvas');
  var ctx = newCanvas.getContext("2d");
  ctx.drawImage(img, 0, 0, natW, natH, 0, 0, parseInt(natW * scale), parseInt(natH * scale));

  var base64 = newCanvas.toDataURL('image/jpeg',1);
  img.src = base64;
}

But this seems to only show the top left corner of the src image.但这似乎只显示了 src 图像的左上角。 Why is this and how can I get the whole image?为什么会这样,我怎样才能得到整个图像?

Secondly, I am trying to measure the diskspace size of the image, so I use the function:其次,我试图测量图像的磁盘空间大小,所以我使用 function:

function getImageSize(img) {
  var can = document.createElement('canvas');
  var natW = img.naturalWidth;
  var natH = img.naturalHeight;
  can.width = natW;
  can.height = natH;
  var ctx = can.getContext("2d");
  ctx.drawImage(img, 0, 0, natW, natH, 0, 0, natW, natH);
  var base64 = can.toDataURL('image/jpeg',1);
  return base64.length;
}

But this seems to result in an incorrect result - after the copy the img [base64] src is blank - can anyone help?但这似乎导致了不正确的结果-复制后 img [base64] src 为空白-有人可以帮忙吗?

I have a running sample found on https://jsfiddle.net/Abeeee/9bzk7h6w/ (sorry - it includes a very large base64 image as I couldn't find a way to include images in jsfiddle and external images will cause security issues with the script).我在https://jsfiddle.net/Abeeee/9bzk7h6w/上找到了一个正在运行的示例(抱歉 - 它包含一个非常大的 base64 图像,因为我找不到在 jsfiddle 中包含图像的方法,外部图像会导致安全问题剧本)。

  1. When running the sample, and clicking [Run step 1] I see the resulting image as expected but with indicated image sizes wrongly stated.运行示例并单击 [运行步骤 1] 时,我看到了预期的结果图像,但指示的图像尺寸错误地说明了。
  2. When running the sample, and clicking [Run step 1 and 2] I see only the partial result.运行示例时,单击 [Run step 1 and 2] 我只看到部分结果。

To recap - I'm trying to resize an image in JavaScript and want to see the resulting file size as I go.回顾一下 - 我正在尝试调整 JavaScript 中的图像大小,并希望看到生成的文件大小为 go。

What goes on?怎么回事?

Ok, it looks like the partial-result image is caused by two issues - a) the newCanvas needed to have.width and.height set,好的,看起来部分结果图像是由两个问题引起的 - a) newCanvas 需要设置.width 和.height,

function doSendToCanvasThenCopyImg(srcimg, destimg) {
  var scale = 1;
  var natW = srcimg.naturalWidth;
  var natH = srcimg.naturalHeight;
  var newCanvas = document.createElement('canvas');
  newCanvas.width = parseInt(natW * scale);            <----------
  newCanvas.height = parseInt(natH * scale);           <----------

  var ctx = newCanvas.getContext("2d");
  ctx.drawImage(srcimg, 0, 0, natW, natH, 0, 0, newCanvas.width, newCanvas.height);

  var base64 = newCanvas.toDataURL('image/jpeg', 1);
  destimg.src = base64;

b) the call to doSendToCanvasThenCopyImg seems to need to wait for the copyimg to be loaded before being processed b) 对 doSendToCanvasThenCopyImg 的调用似乎需要等待 copyimg 加载完毕才能处理

  var done=false; // bit quick and nasty but prevents the scaleToFit from recursively triggering onload
  copyimg.onload = function() {
    if ( !done ) {
      done=true;
      var destW = 500;
      scaleToFit(copyimg, destW, srcimg.naturalWidth, srcimg.naturalHeight);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM