mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Expose preset option for WebP output #3639
This commit is contained in:
parent
66f7cef253
commit
85b26dab68
@ -294,6 +294,7 @@ Use these WebP options for output image.
|
|||||||
| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression mode |
|
| [options.lossless] | <code>boolean</code> | <code>false</code> | use lossless compression mode |
|
||||||
| [options.nearLossless] | <code>boolean</code> | <code>false</code> | use near_lossless compression mode |
|
| [options.nearLossless] | <code>boolean</code> | <code>false</code> | use near_lossless compression mode |
|
||||||
| [options.smartSubsample] | <code>boolean</code> | <code>false</code> | use high quality chroma subsampling |
|
| [options.smartSubsample] | <code>boolean</code> | <code>false</code> | use high quality chroma subsampling |
|
||||||
|
| [options.preset] | <code>string</code> | <code>"'default'"</code> | named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text |
|
||||||
| [options.effort] | <code>number</code> | <code>4</code> | CPU effort, between 0 (fastest) and 6 (slowest) |
|
| [options.effort] | <code>number</code> | <code>4</code> | CPU effort, between 0 (fastest) and 6 (slowest) |
|
||||||
| [options.loop] | <code>number</code> | <code>0</code> | number of animation iterations, use 0 for infinite animation |
|
| [options.loop] | <code>number</code> | <code>0</code> | number of animation iterations, use 0 for infinite animation |
|
||||||
| [options.delay] | <code>number</code> \| <code>Array.<number></code> | | delay(s) between animation frames (in milliseconds) |
|
| [options.delay] | <code>number</code> \| <code>Array.<number></code> | | delay(s) between animation frames (in milliseconds) |
|
||||||
|
@ -6,6 +6,9 @@ Requires libvips v8.14.2
|
|||||||
|
|
||||||
### v0.32.3 - TBD
|
### v0.32.3 - TBD
|
||||||
|
|
||||||
|
* Expose `preset` option for WebP output.
|
||||||
|
[#3639](https://github.com/lovell/sharp/issues/3639)
|
||||||
|
|
||||||
* Ensure decoding remains sequential for all operations (regression in 0.32.2).
|
* Ensure decoding remains sequential for all operations (regression in 0.32.2).
|
||||||
[#3725](https://github.com/lovell/sharp/issues/3725)
|
[#3725](https://github.com/lovell/sharp/issues/3725)
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
@ -291,6 +291,7 @@ const Sharp = function (input, options) {
|
|||||||
webpLossless: false,
|
webpLossless: false,
|
||||||
webpNearLossless: false,
|
webpNearLossless: false,
|
||||||
webpSmartSubsample: false,
|
webpSmartSubsample: false,
|
||||||
|
webpPreset: 'default',
|
||||||
webpEffort: 4,
|
webpEffort: 4,
|
||||||
webpMinSize: false,
|
webpMinSize: false,
|
||||||
webpMixed: false,
|
webpMixed: false,
|
||||||
|
@ -483,6 +483,7 @@ function png (options) {
|
|||||||
* @param {boolean} [options.lossless=false] - use lossless compression mode
|
* @param {boolean} [options.lossless=false] - use lossless compression mode
|
||||||
* @param {boolean} [options.nearLossless=false] - use near_lossless compression mode
|
* @param {boolean} [options.nearLossless=false] - use near_lossless compression mode
|
||||||
* @param {boolean} [options.smartSubsample=false] - use high quality chroma subsampling
|
* @param {boolean} [options.smartSubsample=false] - use high quality chroma subsampling
|
||||||
|
* @param {string} [options.preset='default'] - named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text
|
||||||
* @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 6 (slowest)
|
* @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 6 (slowest)
|
||||||
* @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
|
* @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
|
||||||
* @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
|
* @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
|
||||||
@ -517,6 +518,13 @@ function webp (options) {
|
|||||||
if (is.defined(options.smartSubsample)) {
|
if (is.defined(options.smartSubsample)) {
|
||||||
this._setBooleanOption('webpSmartSubsample', options.smartSubsample);
|
this._setBooleanOption('webpSmartSubsample', options.smartSubsample);
|
||||||
}
|
}
|
||||||
|
if (is.defined(options.preset)) {
|
||||||
|
if (is.string(options.preset) && is.inArray(options.preset, ['default', 'photo', 'picture', 'drawing', 'icon', 'text'])) {
|
||||||
|
this.options.webpPreset = options.preset;
|
||||||
|
} else {
|
||||||
|
throw is.invalidParameterError('preset', 'one of: default, photo, picture, drawing, icon, text', options.preset);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (is.defined(options.effort)) {
|
if (is.defined(options.effort)) {
|
||||||
if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) {
|
if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) {
|
||||||
this.options.webpEffort = options.effort;
|
this.options.webpEffort = options.effort;
|
||||||
|
@ -894,6 +894,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
|||||||
->set("lossless", baton->webpLossless)
|
->set("lossless", baton->webpLossless)
|
||||||
->set("near_lossless", baton->webpNearLossless)
|
->set("near_lossless", baton->webpNearLossless)
|
||||||
->set("smart_subsample", baton->webpSmartSubsample)
|
->set("smart_subsample", baton->webpSmartSubsample)
|
||||||
|
->set("preset", baton->webpPreset)
|
||||||
->set("effort", baton->webpEffort)
|
->set("effort", baton->webpEffort)
|
||||||
->set("min_size", baton->webpMinSize)
|
->set("min_size", baton->webpMinSize)
|
||||||
->set("mixed", baton->webpMixed)
|
->set("mixed", baton->webpMixed)
|
||||||
@ -1099,6 +1100,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
|||||||
->set("lossless", baton->webpLossless)
|
->set("lossless", baton->webpLossless)
|
||||||
->set("near_lossless", baton->webpNearLossless)
|
->set("near_lossless", baton->webpNearLossless)
|
||||||
->set("smart_subsample", baton->webpSmartSubsample)
|
->set("smart_subsample", baton->webpSmartSubsample)
|
||||||
|
->set("preset", baton->webpPreset)
|
||||||
->set("effort", baton->webpEffort)
|
->set("effort", baton->webpEffort)
|
||||||
->set("min_size", baton->webpMinSize)
|
->set("min_size", baton->webpMinSize)
|
||||||
->set("mixed", baton->webpMixed)
|
->set("mixed", baton->webpMixed)
|
||||||
@ -1377,6 +1379,7 @@ class PipelineWorker : public Napi::AsyncWorker {
|
|||||||
{"lossless", baton->webpLossless ? "TRUE" : "FALSE"},
|
{"lossless", baton->webpLossless ? "TRUE" : "FALSE"},
|
||||||
{"near_lossless", baton->webpNearLossless ? "TRUE" : "FALSE"},
|
{"near_lossless", baton->webpNearLossless ? "TRUE" : "FALSE"},
|
||||||
{"smart_subsample", baton->webpSmartSubsample ? "TRUE" : "FALSE"},
|
{"smart_subsample", baton->webpSmartSubsample ? "TRUE" : "FALSE"},
|
||||||
|
{"preset", vips_enum_nick(VIPS_TYPE_FOREIGN_WEBP_PRESET, baton->webpPreset)},
|
||||||
{"min_size", baton->webpMinSize ? "TRUE" : "FALSE"},
|
{"min_size", baton->webpMinSize ? "TRUE" : "FALSE"},
|
||||||
{"mixed", baton->webpMixed ? "TRUE" : "FALSE"},
|
{"mixed", baton->webpMixed ? "TRUE" : "FALSE"},
|
||||||
{"effort", std::to_string(baton->webpEffort)}
|
{"effort", std::to_string(baton->webpEffort)}
|
||||||
@ -1629,6 +1632,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
|
|||||||
baton->webpLossless = sharp::AttrAsBool(options, "webpLossless");
|
baton->webpLossless = sharp::AttrAsBool(options, "webpLossless");
|
||||||
baton->webpNearLossless = sharp::AttrAsBool(options, "webpNearLossless");
|
baton->webpNearLossless = sharp::AttrAsBool(options, "webpNearLossless");
|
||||||
baton->webpSmartSubsample = sharp::AttrAsBool(options, "webpSmartSubsample");
|
baton->webpSmartSubsample = sharp::AttrAsBool(options, "webpSmartSubsample");
|
||||||
|
baton->webpPreset = sharp::AttrAsEnum<VipsForeignWebpPreset>(options, "webpPreset", VIPS_TYPE_FOREIGN_WEBP_PRESET);
|
||||||
baton->webpEffort = sharp::AttrAsUint32(options, "webpEffort");
|
baton->webpEffort = sharp::AttrAsUint32(options, "webpEffort");
|
||||||
baton->webpMinSize = sharp::AttrAsBool(options, "webpMinSize");
|
baton->webpMinSize = sharp::AttrAsBool(options, "webpMinSize");
|
||||||
baton->webpMixed = sharp::AttrAsBool(options, "webpMixed");
|
baton->webpMixed = sharp::AttrAsBool(options, "webpMixed");
|
||||||
|
@ -153,6 +153,7 @@ struct PipelineBaton {
|
|||||||
bool webpNearLossless;
|
bool webpNearLossless;
|
||||||
bool webpLossless;
|
bool webpLossless;
|
||||||
bool webpSmartSubsample;
|
bool webpSmartSubsample;
|
||||||
|
VipsForeignWebpPreset webpPreset;
|
||||||
int webpEffort;
|
int webpEffort;
|
||||||
bool webpMinSize;
|
bool webpMinSize;
|
||||||
bool webpMixed;
|
bool webpMixed;
|
||||||
@ -318,6 +319,7 @@ struct PipelineBaton {
|
|||||||
webpNearLossless(false),
|
webpNearLossless(false),
|
||||||
webpLossless(false),
|
webpLossless(false),
|
||||||
webpSmartSubsample(false),
|
webpSmartSubsample(false),
|
||||||
|
webpPreset(VIPS_FOREIGN_WEBP_PRESET_DEFAULT),
|
||||||
webpEffort(4),
|
webpEffort(4),
|
||||||
webpMinSize(false),
|
webpMinSize(false),
|
||||||
webpMixed(false),
|
webpMixed(false),
|
||||||
|
@ -102,6 +102,29 @@ describe('WebP', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should produce a different file size with specific preset', () =>
|
||||||
|
sharp(fixtures.inputJpg)
|
||||||
|
.resize(320, 240)
|
||||||
|
.webp({ preset: 'default' })
|
||||||
|
.toBuffer()
|
||||||
|
.then(presetDefault =>
|
||||||
|
sharp(fixtures.inputJpg)
|
||||||
|
.resize(320, 240)
|
||||||
|
.webp({ preset: 'picture' })
|
||||||
|
.toBuffer()
|
||||||
|
.then(presetPicture => {
|
||||||
|
assert.notStrictEqual(presetDefault.length, presetPicture.length);
|
||||||
|
})
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
it('invalid preset throws', () => {
|
||||||
|
assert.throws(
|
||||||
|
() => sharp().webp({ preset: 'fail' }),
|
||||||
|
/Expected one of: default, photo, picture, drawing, icon, text for preset but received fail of type string/
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('should produce a smaller file size with increased effort', () =>
|
it('should produce a smaller file size with increased effort', () =>
|
||||||
sharp(fixtures.inputJpg)
|
sharp(fixtures.inputJpg)
|
||||||
.resize(320, 240)
|
.resize(320, 240)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user