用javascript

时间:2018-07-10 19:08:25

标签: javascript webp

我甚至想在不支持webP的浏览器(例如firefox)上打开webP图像。 到目前为止,我的代码基于webp_js library提供的示例:

'use strict';

var Module = {
  noInitialRun : true
};


// main wrapper for the function decoding a WebP into a canvas object
var WebpToCanvas;

function init() {
  WebpToCanvas = Module.cwrap('WebpToSDL', 'number', ['array', 'number']);
}
window.onload = init;

function decode(webp_data, canvas_id) {
  // get the canvas to decode into
  var canvas = document.getElementById(canvas_id);
  if (canvas == null) return;
  // clear previous picture (if any)
  Module.canvas = canvas;
  canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
  // decode and measure timing
  var ret = WebpToCanvas(webp_data, webp_data.length);
  }
}

function loadfile(filename, canvas_id) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', filename);
  xhr.responseType = 'arraybuffer';
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var webp_data = new Uint8Array(xhr.response);
      decode(webp_data, canvas_id);
    }
  };
  xhr.send();
}

此代码在示例图像中效果很好,但是不幸的是,当我向图像添加alpha通道并使用GIMP导出它时,它会发送此错误:

Cannot enlarge memory arrays. Either (1) compile with  -s TOTAL_MEMORY=X  with X higher than the current value 16777216, (2) compile with  -s ALLOW_MEMORY_GROWTH=1  which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with  -s ABORTING_MALLOC=0 

以下是有关图像的信息:

默认图片:

RIFF HEADER:
  File size: 1321542
Chunk VP8L at offset     12, length 1321530
  Width: 2048
  Height: 396
  Alpha: 0
  Animation: 0
  Format: Lossless (2)
No error detected.

默认图像有损:

RIFF HEADER:
  File size: 378184
Chunk VP8  at offset     12, length 378172
  Width: 2048
  Height: 396
  Alpha: 0
  Animation: 0
  Format: Lossy (1)
No error detected.

具有Alpha通道的图像(无损与否无关紧要,两种情况均以alpha失败)

RIFF HEADER:
  File size:  49840
Chunk VP8L at offset     12, length  49828
  Width: 1920
  Height: 1080
  Alpha: 1
  Animation: 0
  Format: Lossless (2)
No error detected.

此外,奇怪的是,即使代码和nginx配置相同,它也可以与localhost完美配合,但不适用于远程服务器。 显然,默认的16GB RAM必须足够,所以我在某个地方搞砸了,但找不到它。

希望您能提供帮助!

0 个答案:

没有答案