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

Zoom param support #57

Merged
merged 4 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`: <ul><li>`attention`: good results, ~70% slower</li><li>`entropy`: mediocre results, ~30% slower</li><li>`smart`: best results, ~50% slower</li>|
|`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`.|
Expand Down
43 changes: 28 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ 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;
});
}

Expand Down Expand Up @@ -104,9 +105,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 ) {
Expand All @@ -133,14 +137,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
Expand All @@ -155,27 +159,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 Math.min(Math.max(Math.round( defaultValue - ( (Math.log(zoom) / Math.log(defaultValue / zoom)) * (defaultValue * zoom) ) ), Math.round(defaultValue / zoom)), defaultValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth making a clamp = ( val, min, max ) => Math.min( Math.max( val, min ), max ) to reuse across all of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. The whole codebase needs a good tidy up really!

}

// 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(Math.min(Math.max(Number(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(Math.min(Math.max(Number(args.quality), 0), 100)),
});
}

Expand Down