diff --git a/README.md b/README.md
index 9f92a4b..41a9b38 100644
--- a/README.md
+++ b/README.md
@@ -53,6 +53,7 @@ It's really that simple!
|`crop_strategy`|String, "smart", "entropy", "attention"|There are 3 automatic cropping strategies for use with `resize`:
- `attention`: good results, ~70% slower
- `entropy`: mediocre results, ~30% slower
- `smart`: best results, ~50% slower
|
|`fit`|String, "w,h"|A comma separated string of the target maximum width and height. Does not crop the image.|
|`crop`|Boolean\|String, "x,y,w,h"|Crop an image by percentages x-offset, y-offset, width and height (x,y,w,h). Percentages are used so that you don’t need to recalculate the cropping when transforming the image in other ways such as resizing it. You can crop by pixel values too by appending `px` to the values. `crop=160px,160px,788px,788px` takes a 788 by 788 pixel square starting at 160 by 160.|
+|`zoom`|Number|Zooms the image by the specified amount for high DPI displays. `zoom=2` produces an image twice the size specified in `w`, `h`, `fit` or `resize`. The quality is automatically reduced to keep file sizes roughly equivalent to the non-zoomed image unless the `quality` argument is passed.|
|`webp`|Boolean, 1|Force WebP format.|
|`lb`|String, "w,h"|Add letterboxing effect to images, by scaling them to width, height while maintaining the aspect ratio and filling the rest with black or `background`.|
|`background`|String|Add background color via name (red) or hex value (%23ff0000). Don't forget to escape # as `%23`.|
diff --git a/index.js b/index.js
index cb892ef..07f1c53 100644
--- a/index.js
+++ b/index.js
@@ -39,13 +39,18 @@ module.exports.s3 = function(config, key, args, callback) {
);
};
-var getDimArray = function( dims ) {
+var getDimArray = function( dims, zoom ) {
var dimArr = typeof dims === 'string' ? dims.split(',') : dims;
+ zoom = zoom || 1;
return dimArr.map(function(v) {
- return Number(v) || null;
+ return (Number(v) * zoom) || null;
});
}
+var clamp = function( val, min, max ) {
+ return Math.min( Math.max( Number( val ), min ), max );
+}
+
module.exports.resizeBuffer = function(buffer, args, callback) {
return new Promise(function(resolve, reject) {
try {
@@ -104,9 +109,12 @@ module.exports.resizeBuffer = function(buffer, args, callback) {
});
}
+ // get zoom value
+ var zoom = parseFloat( args.zoom ) || 1;
+
// resize
if (args.resize) {
- args.resize = getDimArray( args.resize );
+ args.resize = getDimArray( args.resize, zoom );
// apply cropping strategies
if ( args.gravity ) {
@@ -133,14 +141,14 @@ module.exports.resizeBuffer = function(buffer, args, callback) {
args.resize
);
} else if (args.fit) {
- args.fit = getDimArray( args.fit );
+ args.fit = getDimArray( args.fit, zoom );
image.resize.apply(
image,
args.fit
);
image.max();
} else if (args.lb) {
- args.lb = getDimArray( args.lb );
+ args.lb = getDimArray( args.lb, zoom );
image.resize.apply(
image,
args.lb
@@ -155,27 +163,36 @@ module.exports.resizeBuffer = function(buffer, args, callback) {
image.embed();
} else if (args.w || args.h) {
image.resize(
- Number(args.w) || null,
- Number(args.h) || null
+ (Number(args.w) * zoom) || null,
+ (Number(args.h) * zoom) || null
);
if (!args.crop) {
image.max();
}
}
+ // return a default compression value based on a logarithmic scale
+ // defaultValue = 100, zoom = 2; = 65
+ // defaultValue = 80, zoom = 2; = 50
+ // defaultValue = 100, zoom = 1.5; = 86
+ // defaultValue = 80, zoom = 1.5; = 68
+ var applyZoomCompression = function( defaultValue, zoom ) {
+ return clamp( Math.round( defaultValue - ( (Math.log(zoom) / Math.log(defaultValue / zoom)) * (defaultValue * zoom) ) ), Math.round(defaultValue / zoom), defaultValue );
+ }
+
+ // set default quality slightly higher than sharp's default
+ if ( ! args.quality ) {
+ args.quality = applyZoomCompression( 82, zoom );
+ }
+
// allow override of compression quality
if (args.webp) {
image.webp({
- quality: args.quality
- ? Math.min(Math.max(Number(args.quality), 0), 100)
- : 80,
+ quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
- } else if (metadata.format === 'jpeg' && args.quality) {
+ } else if (metadata.format === 'jpeg') {
image.jpeg({
- quality: Math.min(
- Math.max(Number(args.quality), 0),
- 100
- ),
+ quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
}