mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Improve support for extension-less output filenames
This commit is contained in:
parent
2820218609
commit
3699e61c20
16
src/sharp.cc
16
src/sharp.cc
@ -630,28 +630,34 @@ class ResizeWorker : public NanAsyncWorker {
|
|||||||
return resize_error(baton, output);
|
return resize_error(baton, output);
|
||||||
}
|
}
|
||||||
baton->output_format = "webp";
|
baton->output_format = "webp";
|
||||||
} else if (is_jpeg(baton->output)) {
|
} else {
|
||||||
|
bool output_jpeg = is_jpeg(baton->output);
|
||||||
|
bool output_png = is_png(baton->output);
|
||||||
|
bool output_webp = is_webp(baton->output);
|
||||||
|
bool output_tiff = is_tiff(baton->output);
|
||||||
|
bool match_input = !(output_jpeg || output_png || output_webp || output_tiff);
|
||||||
|
if (output_jpeg || (match_input && inputImageType == JPEG)) {
|
||||||
// Write JPEG to file
|
// Write JPEG to file
|
||||||
if (vips_jpegsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
if (vips_jpegsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||||
"Q", baton->quality, "optimize_coding", TRUE, "interlace", baton->progressive, NULL)) {
|
"Q", baton->quality, "optimize_coding", TRUE, "interlace", baton->progressive, NULL)) {
|
||||||
return resize_error(baton, output);
|
return resize_error(baton, output);
|
||||||
}
|
}
|
||||||
baton->output_format = "jpeg";
|
baton->output_format = "jpeg";
|
||||||
} else if (is_png(baton->output)) {
|
} else if (output_png || (match_input && inputImageType == PNG)) {
|
||||||
// Write PNG to file
|
// Write PNG to file
|
||||||
if (vips_pngsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
if (vips_pngsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||||
"compression", baton->compressionLevel, "interlace", baton->progressive, NULL)) {
|
"compression", baton->compressionLevel, "interlace", baton->progressive, NULL)) {
|
||||||
return resize_error(baton, output);
|
return resize_error(baton, output);
|
||||||
}
|
}
|
||||||
baton->output_format = "png";
|
baton->output_format = "png";
|
||||||
} else if (is_webp(baton->output)) {
|
} else if (output_webp || (match_input && inputImageType == WEBP)) {
|
||||||
// Write WEBP to file
|
// Write WEBP to file
|
||||||
if (vips_webpsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
if (vips_webpsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||||
"Q", baton->quality, NULL)) {
|
"Q", baton->quality, NULL)) {
|
||||||
return resize_error(baton, output);
|
return resize_error(baton, output);
|
||||||
}
|
}
|
||||||
baton->output_format = "webp";
|
baton->output_format = "webp";
|
||||||
} else if (is_tiff(baton->output)) {
|
} else if (output_tiff || (match_input && inputImageType == TIFF)) {
|
||||||
// Write TIFF to file
|
// Write TIFF to file
|
||||||
if (vips_tiffsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
if (vips_tiffsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||||
"compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", baton->quality, NULL)) {
|
"compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", baton->quality, NULL)) {
|
||||||
@ -660,6 +666,8 @@ class ResizeWorker : public NanAsyncWorker {
|
|||||||
baton->output_format = "tiff";
|
baton->output_format = "tiff";
|
||||||
} else {
|
} else {
|
||||||
(baton->err).append("Unsupported output " + baton->output);
|
(baton->err).append("Unsupported output " + baton->output);
|
||||||
|
return resize_error(baton, output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
g_object_unref(output);
|
g_object_unref(output);
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ var inputJpgWithExif = path.join(fixturesPath, "Landscape_8.jpg"); // https://gi
|
|||||||
var inputJpgWithGammaHoliness = path.join(fixturesPath, "gamma_dalai_lama_gray.jpg"); // http://www.4p8.com/eric.brasseur/gamma.html
|
var inputJpgWithGammaHoliness = path.join(fixturesPath, "gamma_dalai_lama_gray.jpg"); // http://www.4p8.com/eric.brasseur/gamma.html
|
||||||
|
|
||||||
var inputPng = path.join(fixturesPath, "50020484-00001.png"); // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
var inputPng = path.join(fixturesPath, "50020484-00001.png"); // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||||
var inputWebp = path.join(fixturesPath, "4.webp"); // http://www.gstatic.com/webp/gallery/4.webp
|
var inputWebP = path.join(fixturesPath, "4.webp"); // http://www.gstatic.com/webp/gallery/4.webp
|
||||||
var inputGif = path.join(fixturesPath, "Crash_test.gif"); // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif
|
var inputGif = path.join(fixturesPath, "Crash_test.gif"); // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif
|
||||||
|
|
||||||
// Ensure cache limits can be set
|
// Ensure cache limits can be set
|
||||||
@ -493,6 +493,48 @@ async.series([
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
// Output filename without extension should mirror input format
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).resize(320, 80).toFile(path.join(fixturesPath, 'output.zoinks'), function(err, info) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual('jpeg', info.format);
|
||||||
|
assert.strictEqual(320, info.width);
|
||||||
|
assert.strictEqual(80, info.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
sharp(inputPng).resize(320, 80).toFile(path.join(fixturesPath, 'output.zoinks'), function(err, info) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual('png', info.format);
|
||||||
|
assert.strictEqual(320, info.width);
|
||||||
|
assert.strictEqual(80, info.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
sharp(inputWebP).resize(320, 80).toFile(path.join(fixturesPath, 'output.zoinks'), function(err, info) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual('webp', info.format);
|
||||||
|
assert.strictEqual(320, info.width);
|
||||||
|
assert.strictEqual(80, info.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
sharp(inputTiff).resize(320, 80).toFile(path.join(fixturesPath, 'output.zoinks'), function(err, info) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual('tiff', info.format);
|
||||||
|
assert.strictEqual(320, info.width);
|
||||||
|
assert.strictEqual(80, info.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function(done) {
|
||||||
|
sharp(inputGif).resize(320, 80).toFile(path.join(fixturesPath, 'output.zoinks'), function(err, info) {
|
||||||
|
assert(!!err);
|
||||||
|
});
|
||||||
|
},
|
||||||
// Metadata - JPEG
|
// Metadata - JPEG
|
||||||
function(done) {
|
function(done) {
|
||||||
sharp(inputJpg).metadata(function(err, metadata) {
|
sharp(inputJpg).metadata(function(err, metadata) {
|
||||||
@ -545,7 +587,7 @@ async.series([
|
|||||||
},
|
},
|
||||||
// Metadata - WebP
|
// Metadata - WebP
|
||||||
function(done) {
|
function(done) {
|
||||||
sharp(inputWebp).metadata(function(err, metadata) {
|
sharp(inputWebP).metadata(function(err, metadata) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
assert.strictEqual('webp', metadata.format);
|
assert.strictEqual('webp', metadata.format);
|
||||||
assert.strictEqual(1024, metadata.width);
|
assert.strictEqual(1024, metadata.width);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user