Home  >  Article  >  Web Front-end  >  Use gm circular cropping and synthesis of pictures under Nodejs

Use gm circular cropping and synthesis of pictures under Nodejs

小云云
小云云Original
2018-02-23 09:28:412425browse

When it comes to image processing under Nodejs, the first thing that comes to mind is gm. The bottom layer of gm can be GraphicsMagic (which is actually the origin of gm) or ImageMagick (in fact, GraphicsMagic itself was also split from ImageMagic and is now independent). Although these two tools themselves are not js implementations, so they require additional installation. However, this tool is very common and may have been pre-installed in the Linux system, and the installation is also very convenient, so there is no need to give up just because it is a "third party". Although these two software are just the bottom layer and do not need to be concerned about, the author found in practice that GraphicsMagic must be used, so here I only introduce the installation and use of GraphicsMagics. This article mainly introduces you to the example of using gm circular cropping and compositing pictures under Nodejs. I hope it can help you.

GaphicsMagic Installation

GraphicsMagic official website is: http://www.graphicsmagick.org/

The official website and most tutorials online teach how to Compile, but I personally think it can be installed directly through the software library, such as


apt-get install graphicsmagic

gm installation under Nodejs

gm is a node library, so you can install it with npm


npm install gm

Official document: http://aheckmann.github.io/gm/

Principle of circular clipping

gm is an encapsulation of GraphicsMagic, so in theory, all functions of GraphicsMagic can be implemented in the form of interfaces through gm. GM itself does not support circular clipping (at least I don't know how to achieve it), which also means that its underlying layer does not directly support it.

The more commonly used functions of gm are: scaling, square cropping, and format conversion.

So the core of circular cropping in this tutorial is to use SVG to construct a circular picture through svg, and then convert it into png through gm, that is, use svg to transform the picture format.

SVG can be used to crop circular images. There are two ways to achieve circular cropping found on the Internet

1. Define a circle through clip-path

path, and then set the clip-path in the style of the picture, that is, through this, the picture is cropped. In theory, it is "cropping" in the true sense


<svg>
  <defs>
    <clipPathid="clipPath">
      <circlecx="5"cy="5"r="5"/>
    </clipPath>
  </defs>
  <imagestyle="clip-path: url(#clipPath);"href="{{icon_img}}" rel="external nofollow" rel="external nofollow" x="0"y="0"width="10"height="10"/> 
</svg>

If You can use clip-path to watch this tutorial

But there is no problem with this configuration in the browser, but I found that after converting it to png through gm, the style has no effect and is still square.

2. Use the circle tag


<svg>
  <defs>
    <patternpatternUnits="userSpaceOnUse"id="raduisImage"x="0"y="0"width="10"height="10">
      <imagehref="{{icon_img}}" rel="external nofollow" rel="external nofollow" x="0"y="0"width="10"height="10"/>
    </pattern>
  </defs>
  <circlecx="5"cy="5"r="5"fill="url(#raduisImage)"></circle>
</svg>

First define a pattern, which is the original picture, then create a circle and use the circle you just defined Just fill the circle with the pattern.

Principles of compositing pictures

If you understand the above-mentioned cropping principles, compositing will become simple. Just put the pictures you want to combine together in svg format. Although gm itself supports image synthesis, using compose or mosaic (see this tutorial for details), it feels not as clear as svg.

Note that the author tried to add text through svg, but found that Chinese characters could not be recognized, so it can still be added through gm. The font needs to be set when adding (see the next chapter for code implementation)

If you want to embed two circular pictures in a large picture, you need to set two patterns. Here is a rule of thumb:

  1. The x and y of the pattern are set to 0. 0

  2. The width and height of the pattern are set the same as the canvas

  3. The x and y of the image are set to its "actual position", that is Corresponding to cx-r and cy-r of circle, r is cut because cx and cy refer to the center of the circle, while x and y are the positions of the upper left corner of the figure.

Code implementation


const gm = require(&#39;gm&#39;)
const fs = require(&#39;fs&#39;)
let templateSVG = "/path/to/original.svg"
let outputSVG = "/path/to/repalced.svg"
let input = "/path/to/icon.png"
let font = "/path/to/font.ttf"
let fontColor = "white"
let fontSize = 10

fs.readFile(templateSVG,&#39;utf-8&#39;,function(err,data){
  if (err) throw err
  var d = data.replace(&#39;{{icon_img}}&#39;,input)
  fs.writeFile(outputSVG,d,function(err){
    if (err) throw err
    gm(outputSVG)
    .font(font)
    .fill(fontColor)
    .fontSize(fontSize)
    .drawText(textPosition[0], textPosition[1], text)//
    .write(output,function(err){
      if(err) cb(err)
      // next
    })
  })
})

The above is the detailed content of Use gm circular cropping and synthesis of pictures under Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn