Use gm cropping to synthesize pictures under Nodejs
This article mainly introduces the example of using gm circular cropping and compositing pictures under Nodejs. Now I share it with you and give you a reference.
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 it can be 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.
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
If you want to learn to use GraphicsMagic from the command line, you can read here: http://www.jb51.net/LINUXjishu/120332.html
gm installation under Nodejs
gm is a node library, so it can be installed 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 clip-path in the style of the image, that is, through this way, image cropping is achieved. 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 use clip-path, you can see this tutorial
However, 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. First define a pattern through 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>
, which is the original picture, then create a circle and fill it with the pattern just defined.
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 details on 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:
The x and y of the pattern are set to 0. 0
The width and height of the pattern are set the same as the canvas
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('gm') const fs = require('fs') 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,'utf-8',function(err,data){ if (err) throw err var d = data.replace('{{icon_img}}',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 what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement custom global methods in vue
How to implement global registration and local registration in vue components
How to modify the project name through the vue-cli webpack project
The above is the detailed content of Use gm cropping to synthesize pictures under Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.
