Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation for FFmpeg support #1245

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
language: node_js
before_install:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:mc3man/trusty-media; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y ffmpeg; fi
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew update; fi
- if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew install ffmpeg; fi
install:
- npm install --build-from-source
node_js:
Expand All @@ -18,4 +24,4 @@ addons:
- g++-4.9
env:
- CXX=g++-4.9
sudo: false
sudo: required
66 changes: 54 additions & 12 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
* Module dependencies.
*/

const bindings = require('./bindings')
const Image = module.exports = bindings.Image
const http = require("http")
const https = require("https")
const bindings = require('./bindings');
const Image = module.exports = bindings.Image;
const fs = require('fs');
const cp = require('child_process');
const http = require('http');
const https = require('https');

Object.defineProperty(Image.prototype, 'src', {
/**
Expand All @@ -33,8 +35,7 @@ Object.defineProperty(Image.prototype, 'src', {
// 'base64' must come before the comma
const isBase64 = val.lastIndexOf('base64', commaI)
const content = val.slice(commaI + 1)
this.source = Buffer.from(content, isBase64 ? 'base64' : 'utf8')
this._originalSource = val
setSource(this, Buffer.from(content, isBase64 ? 'base64' : 'utf8'), val);
} else if (/^\s*https?:\/\//.test(val)) { // remote URL
const onerror = err => {
if (typeof this.onerror === 'function') {
Expand All @@ -52,17 +53,14 @@ Object.defineProperty(Image.prototype, 'src', {
const buffers = []
res.on('data', buffer => buffers.push(buffer))
res.on('end', () => {
this.source = Buffer.concat(buffers)
this._originalSource = undefined
setSource(this, Buffer.concat(buffers));
})
}).on('error', onerror)
} else { // local file path assumed
this.source = val
this._originalSource = undefined
setSource(this, val);
}
} else if (Buffer.isBuffer(val)) {
this.source = val
this._originalSource = undefined
setSource(this, val);
}
},

Expand Down Expand Up @@ -90,3 +88,47 @@ Image.prototype.inspect = function(){
+ (this.complete ? ' complete' : '')
+ ']';
};

function setSource(img, val, origSrc){
var {onload, onerror} = img;
var isBuff = Buffer.isBuffer(val);

img.onload = () => {
restore();
if(onload) img.onload();
};

img.onerror = err => {
img.onerror = () => {
restore();
if(onerror) img.onerror(err);
};

var buff = cp.spawnSync('ffmpeg', [
'-hide_banner',
'-i', isBuff ? '-' : val,

'-vframes', '1',
'-f', 'apng',
'-',
], {
input: isBuff ? val : undefined,
}).stdout;

if(buff === null || buff.length === 0){
img.onerror();
return;
}

origSrc = val;
img.source = buff;
};

img.source = val;

function restore(){
img.onload = onload;
img.onerror = onerror;
img._originalSource = origSrc;
}
}
Binary file modified test/fixtures/159-crash1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/obscure/1160.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/obscure/122.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/obscure/562.webp
Binary file not shown.
Binary file added test/fixtures/obscure/73.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/obscure/953.bmp
Binary file not shown.
42 changes: 42 additions & 0 deletions test/image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Image = require('../').Image
const assert = require('assert')
const assertRejects = require('assert-rejects')
const fs = require('fs')
const path = require('path')

const png_checkers = `${__dirname}/fixtures/checkers.png`
const png_clock = `${__dirname}/fixtures/clock.png`
Expand Down Expand Up @@ -268,4 +269,45 @@ describe('Image', function () {

return Promise.all(corruptSources.map(src => loadImage(src).catch(() => null)))
})

describe('Obscure image formats', () => {
var imgsDir = path.join(__dirname, '/fixtures/obscure');
var getNum = name => +name.match(/^\d+/)[0];

var imgNames = fs.readdirSync(imgsDir).sort((n1, n2) => {
n1 = getNum(n1);
n2 = getNum(n2);
return (n1 > n2) - (n1 < n2);
});

var test = description => {
var imgName = imgNames.shift();
var imgPath = path.join(imgsDir, imgName);
var num = getNum(imgName);

it(`${description} (#${num})`, done => {
var img = new Image();

img.onload = () => {
assert.strictEqual(img.width, 64);
assert.strictEqual(img.height, 64);
done();
};

img.onerror = err => {
throw err;
};

img.src = imgPath;
});
};

[
'Dubious gAMA chunk',
'PNG with cHRM chunk',
'WEBP format',
'BMP format',
'JPEG with precision set to 12',
].forEach(test);
});
})