I'm trying to crop picture by a shape picture and tried this
let imageDoc = gm(filePath).resize(100, 100);
imageDoc.mask(`${shapesPath}/hexagon.svg`);
It acts like nothing was done but resizes correctly. Also tried using png file instead of svg, but there is no result at all, maybe there is some way to debug it, or I'm doing something wrong?
According to user Pirijan:
So it seems that
.mask()it only useful when used together with another command.The documentation for GraphicsMagick can be quite confusing, and I'm sure there are multiple ways of masking an image. Here's how I do it:
However, this doesn't use the alpha channel from
mask, instead it works with a black and white mask with no alpha channel. It also requires bothimgandmaskto have identical dimensions.It works by copying the value of each pixel in
maskto the alpha channel ofimg. The-matteoption tells gm to create an alpha channel onimgif it doesn't already have one.Since node-gm uses the debug library from visionmedia you can turn on debug output to the console by setting the environment variable
DEBUG=gm, like so (in Unix/OS X):This will print the exact commands that node-gm invokes.