📅  最后修改于: 2023-12-03 15:33:09.230000             🧑  作者: Mango
The shave()
function is a method provided by the GraphicsMagick (abbreviated as GM) library for Node.js. It is used to trim the edges of an image to remove any unwanted whitespace or border.
gm('/path/to/image.png')
.shave(width, height)
.write('/path/to/trimmed-image.png', function (err) {
if (!err) console.log('Image has been trimmed!');
});
width
(required): The width in pixels of the area to shave from each side of the image.height
(required): The height in pixels of the area to shave from each side of the image.The following example code demonstrates how to use the shave()
function to trim an image:
const gm = require('gm');
gm('/path/to/image.png')
.shave(50, 50)
.write('/path/to/trimmed-image.png', function (err) {
if (!err) console.log('Image has been trimmed!');
});
In this example, we're shaving 50 pixels from each side of the image, which will remove a 50-pixel border all around the image.
The shave()
function also supports additional options such as crop
, gravity
, color
, and bordercolor
. These options allow you to further customize the trimming behavior.
For more information on these options, please refer to the GraphicsMagick documentation.