
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
limby-resize
Advanced tools
npm install limby-resize
canvas
or imagemagick
node modules.Wrapper for both.
####Better resizing with canvas
Normally, resizing with canvas
produces some not so great images. This module implements it's own resizing algorithm.
lets say we have 3 pixels being resized into 2 pixels.
normally, each pixel will have 4 numbers: red, green, blue, alpha. Lets just look at a simplified version where pixels are just 1 number.
Lets say the original image is: 0 | 100 | 255
The regular canvas drawImage resize will result in either 0 | 100 or 0 | 255
This sometimes is fine, but it loses details and it can be a very ugly and jagged image. If you think about it, the sum of all the color in the original is 355 (0 + 100 + 255), leaving the average pixel 118.33. The resized average pixel would be 50 or 127.5, which could look okay or very different!
The image algorithm implemented in limby-resize
will produce a similar image to imagemagick, keeping all the pixel data, so the average pixel will be the same.
Our algorithm would produce the following image: 33 | 201.3
(0 * .66 + 100 * .33) | (100 * .33 + 255 * .66)
The total in ours is 234.3, leaving the average of 117.15, which is going to equal the first image ( if we weren't rounding to 2 decimals for this example ).
This image resizer is better than the default canvas drawImage, but it is slower and slightly processor intensive.
Overall, this library wraps both imagemagic
and canvas
so you can switch out either one at a whim
var resizer = require('limby-resize')({
canvas: require('canvas'),
});
resizer.resize('/tmp/image01.jpg', {
width: 300,
height: 500,
destination: '/uploads/myimage.jpg',
});
var resizer = require('limby-resize')({
imagemagick: require('imagemagick'),
});
resizer.resize('/tmp/myanimation.gif', {
width: 300,
height: 500,
coalesce: true, // animated gif support ( if your image magick supports )
destination: '/uploads/myanimation.gif',
});
// [0] takes first frame for previews, etc
resizer.resize('/tmp/myanimation.gif[0]', {
width: 300,
height: 500,
destination: '/uploads/myanimation_preview.gif',
});
Note: Currently the algorithm supports resizing down, or resizing up to a scale of 2, if you scale up bigger than 2x, it won't look right. I'll patch this next version.
canvas will just take the first frame, similar to using [0]
with image magick
lib/canvas_resize.js
should be able to be included on the frontend for better resizing client side.
var img, canvas, resized;
img = new Image;
img.onload = function(){
canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
resized = document.createElement('canvas');
resized.width = 300;
resized.height = 500;
// see lib/canvas_resize for window.canvasResize = function(){...}
canvasResize(canvas, resized);
// resized will now be a properly resized version of canvas
}
img.src = '/path/to/img.jpg';
FAQs
Wrapper for resizing using ImageMagick OR Canvas.
The npm package limby-resize receives a total of 19 weekly downloads. As such, limby-resize popularity was classified as not popular.
We found that limby-resize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.