Search code examples
node.jsgraphicsmagick

How to use graphicsmagick in node.js to crop circle picture (other use any other way to make this on node.js)


How to use graphicsmagick crop circle picture?

(use png transparent background outer circle)

I use graphicsmagick in node.js , or have any other way make this in node.js?

Before: https://i.sstatic.net/B34C5.png

After: https://i.sstatic.net/PWjQW.png


Solution

  • I use another module to solve this problem:

    this code is for square image (this.height = this.width)

    node-pngjs

    var fs = require('fs'),
    PNG = require('pngjs').PNG;
    
    fs.createReadStream(__dirname + "/input.png")
        .pipe(new PNG({
            filterType: 4
        }))
        .on('parsed', function() {
        for (var y = 0; y < this.height; y++) {
            for (var x = 0; x < this.width; x++) {
                var idx = (this.width * y + x) << 2;
                var radius = this.height / 2;
                if(y >= Math.sqrt(Math.pow(radius, 2) - Math.pow(x - radius, 2)) + radius || y <= -(Math.sqrt(Math.pow(radius, 2) - Math.pow(x - radius, 2))) + radius) {
                    this.data[idx + 3] = 0;
                }
            }
        }
            this.pack().pipe(fs.createWriteStream(__dirname + "/output.png"));
        });