Search code examples
node.jsgraphicsmagick

Composite images in Graphicsmagick


I'm trying to request an image from an API and "paste" it on top of another image. In Photoshop, I would paste the image into a new layer and then merge the layers. I can accomplish this with Graphicsmagick using gm's composite().

gm().command("composite")
.in("path/to/topImg.png")
.in("path/to/bottomImg.png")
.toBuffer('PNG', function(err, buffer) {
  if (!err) {return buffer;}
});

However, composite only takes file paths. So let's say I want to get the logo from http://www.google.com. I could save the image, use it in the code above, and then delete it. What I'm looking for is a way to accomplish this without having to save the image to disk first.


Solution

  • You can use URL directly as image path, without downloading and saving it

    gm()
      .command("composite")
      .in("http://someurl...")
      .in("http://someurl...")
      .toBuffer('PNG', function(err, buffer) {
        if (!err) {return buffer;}
      });
    

    But GraphicsMagick uses the HTTP support from libxml2, which does not currently support HTTPS. So if you want to download images over HTTPS you will need external program.