mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Improve support for extension-less output filenames
This commit is contained in:
parent
2820218609
commit
3699e61c20
66
src/sharp.cc
66
src/sharp.cc
@ -630,36 +630,44 @@ class ResizeWorker : public NanAsyncWorker {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "webp";
|
||||
} else if (is_jpeg(baton->output)) {
|
||||
// Write JPEG to file
|
||||
if (vips_jpegsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"Q", baton->quality, "optimize_coding", TRUE, "interlace", baton->progressive, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "jpeg";
|
||||
} else if (is_png(baton->output)) {
|
||||
// Write PNG to file
|
||||
if (vips_pngsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"compression", baton->compressionLevel, "interlace", baton->progressive, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "png";
|
||||
} else if (is_webp(baton->output)) {
|
||||
// Write WEBP to file
|
||||
if (vips_webpsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"Q", baton->quality, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "webp";
|
||||
} else if (is_tiff(baton->output)) {
|
||||
// Write TIFF to file
|
||||
if (vips_tiffsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", baton->quality, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "tiff";
|
||||
} else {
|
||||
(baton->err).append("Unsupported output " + baton->output);
|
||||
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
|
||||
if (vips_jpegsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"Q", baton->quality, "optimize_coding", TRUE, "interlace", baton->progressive, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "jpeg";
|
||||
} else if (output_png || (match_input && inputImageType == PNG)) {
|
||||
// Write PNG to file
|
||||
if (vips_pngsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"compression", baton->compressionLevel, "interlace", baton->progressive, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "png";
|
||||
} else if (output_webp || (match_input && inputImageType == WEBP)) {
|
||||
// Write WEBP to file
|
||||
if (vips_webpsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"Q", baton->quality, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "webp";
|
||||
} else if (output_tiff || (match_input && inputImageType == TIFF)) {
|
||||
// Write TIFF to file
|
||||
if (vips_tiffsave(output, baton->output.c_str(), "strip", !baton->withMetadata,
|
||||
"compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", baton->quality, NULL)) {
|
||||
return resize_error(baton, output);
|
||||
}
|
||||
baton->output_format = "tiff";
|
||||
} else {
|
||||
(baton->err).append("Unsupported output " + baton->output);
|
||||
return resize_error(baton, 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 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
|
||||
|
||||
// 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
|
||||
function(done) {
|
||||
sharp(inputJpg).metadata(function(err, metadata) {
|
||||
@ -545,7 +587,7 @@ async.series([
|
||||
},
|
||||
// Metadata - WebP
|
||||
function(done) {
|
||||
sharp(inputWebp).metadata(function(err, metadata) {
|
||||
sharp(inputWebP).metadata(function(err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', metadata.format);
|
||||
assert.strictEqual(1024, metadata.width);
|
||||
|
Loading…
x
Reference in New Issue
Block a user