Use GM in node.js to crop to a circled image

1.6k views Asked by At

I have created an upload image functionality with express 4 and I want to, in the process, create several different sizes and shapes for the uploaded image.

    mkdirp(smallPath, function (err) {
    if (err) {
        console.error('resizeImg err='+err+']');
        return;
    } else {
      gm(basePath+'/original/'+image)
        .resize(35, 35)
        .noProfile()
        .write(smallOutputFilePath, function (err) {
          if (err) console.log(err);
        });
    }

Now, I want this 35x35 image to be cropped circled with transparent background. Like this: enter image description here

I found this similar question: Rounded corner using gm in nodejs. But the answer uses command line ImageMagick and I wanted to use gm methods and capabilities. Does anybody knows how to solve it?

1

There are 1 answers

0
Bruno Siqueira On BEST ANSWER

After a while, I decided to migrate to the great node-easyimage. It gives me more flexibility and allows me to reproduce my command line taking advantages of callbacks for success and error responses.

function resizeImageToSize(path, size, outputTempFilePath, outputFilePath) {
easyimg.exec('convert '+path+' -resize ' +
  (size) + 'x' + (size) + '^  -gravity center -crop ' +
  (size) + 'x' + (size) + '+0+0 +repage '+outputTempFilePath).then(
  function(file) {

    easyimg.exec('convert '+outputTempFilePath+' \\( -size ' +
      (size) + 'x' + (size) + ' xc:none -fill white -draw "circle ' +
      (size / 2) + ',' + (size / 2) + ' ' + (size / 2) + ',0" \\) -compose copy_opacity -composite '+
      outputFilePath).then(
      function(file) {
        fs.unlink(outputTempFilePath, function (err) {
          if (err) {
            console.log(err);
          }
        });
      }, function (err) {
        console.log(err);
      }
    );
  }, function (err) {
    console.log(err);
  }
);}

The imagemagick plugin is really deprecated