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

Add back format inference from filename [possible breaking change] #493

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ examples/imgs/*
!examples/imgs/original.jpg
!examples/imgs/original.png
!examples/imgs/original.gif
!examples/imgs/original.svg
!examples/imgs/originalSideways.jpg
!examples/imgs/morpher.jpg
!examples/imgs/photo.JPG
!examples/imgs/orientation/*\d.jpg
!examples/imgs/orientation/*.correct.jpg
!examples/imgs/icon.ico
*.sw*
.idea
Binary file added examples/imgs/icon.ico
Binary file not shown.
17 changes: 17 additions & 0 deletions examples/imgs/original.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ function gm (source, height, color) {
this.sourceFrames = source.substr(frames.index, frames[0].length);
source = source.substr(0, frames.index);
}

// parse out extension
var ext = source.match(/\.(\w+)$/);
if (ext && source !== 'unknown.jpg') {
this.sourceExt = ext[1].toLowerCase();
}
}

this.source = source;
Expand All @@ -74,6 +80,7 @@ function gm (source, height, color) {
var inputFromStdin = this.sourceStream || this.sourceBuffer;
var ret = inputFromStdin ? '-' : this.source;

if (inputFromStdin && this.sourceExt) ret = this.sourceExt + ':-';
if (ret && this.sourceFrames) ret += this.sourceFrames;

src.length = 0;
Expand Down
6 changes: 5 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ module.exports = function (proto) {
} catch (e) {
return cb(e);
}
proc.stdin.once('error', cb);
proc.stdin.on('error', function (err) {
if (err.code !== 'ECONNRESET') {
cb(err);
}
});

if (timeout) {
timeoutId = setTimeout(function(){
Expand Down
2 changes: 1 addition & 1 deletion lib/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function (gm) {

self.bufferStream = !!opts.bufferStream;

if (val.verbose) {
if (val.verbose || this.inputIs('svg')) {
self.identify(opts, function (err, stdout, stderr, cmd) {
if (err) {
self.emit(getter, err, self.data[key], stdout, stderr, cmd);
Expand Down
21 changes: 21 additions & 0 deletions test/formatInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var assert = require('assert')
var fs = require('fs');

module.exports = function (_, dir, finish, gm) {
var m = gm(fs.createReadStream(dir + '/icon.ico'), 'icon.ico')

var args = m.args();
assert.equal('convert', args[0]);
assert.equal('ico:-', args[1]);

if (!gm.integration)
return finish();

m
.size({bufferStream: true}, function (err, size) {
if (err) return finish(err);
assert.equal(128, size.width);
assert.equal(128, size.height);
finish();
});
}
38 changes: 27 additions & 11 deletions test/getterSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ module.exports = function (_, dir, finish, gm) {
sizeGIF(function (err) {
if (err) return finish(err);

sizePNG(finish);
})
})
sizePNG(function (err) {
if (err) return finish(err);

sizeSVG(finish);
});
});
});

function sizeJPEG(done) {
gm(dir + '/original.jpg')
Expand All @@ -32,22 +36,34 @@ module.exports = function (_, dir, finish, gm) {
.size(function (err, size) {
if (err) return done(err);

assert.equal(size.width, 192)
assert.equal(size.height, 56)
assert.equal(size.width, 192);
assert.equal(size.height, 56);

done()
})
done();
});
}

function sizePNG(done) {
gm(dir + '/original.png')
.size(function (err, size) {
if (err) return done(err);

assert.equal(size.width, 460)
assert.equal(size.height, 155)
assert.equal(size.width, 460);
assert.equal(size.height, 155);

done()
})
done();
});
}

function sizeSVG(done) {
gm(dir + '/original.svg')
.size(function (err, size) {
if (err) return done(err);

assert.equal(size.width, 216);
assert.equal(size.height, 216);

done();
});
}
}
2 changes: 1 addition & 1 deletion test/gifFrameStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (_, dir, finish, gm) {

var args = m.args();
assert.equal('convert', args[0]);
assert.equal('-[0]', args[1]);
assert.equal('gif:-[0]', args[1]);

if (!gm.integration)
return finish();
Expand Down
2 changes: 1 addition & 1 deletion test/resizeAndAutoOrientFromBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (_, dir, finish, gm) {

var args = m.args();
assert.equal('convert', args[0]);
assert.equal('-', args[1]);
assert.equal('jpg:-', args[1]);
assert.equal('-resize', args[2]);
if (m._options.imageMagick) {
assert.equal('20%', args[3]);
Expand Down
2 changes: 1 addition & 1 deletion test/resizeBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function (_, dir, finish, gm) {

var args = m.args();
assert.equal('convert', args[0]);
assert.equal('-', args[1]);
assert.equal('jpg:-', args[1]);
assert.equal('-resize', args[2]);
if (m._options.imageMagick) {
assert.equal('48%', args[3]);
Expand Down
3 changes: 1 addition & 2 deletions test/streamerr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module.exports = function (_, dir, finish, gm) {
// The following invocation should fail, because 'convert'
// has no way of interpreting the file.
gm(
svg_file,
'./test.svg' // fiction
svg_file
).options({
imageMagick: true
}).setFormat('png').toBuffer(function(err, buffer) {
Expand Down