This article mainly introduces the detailed explanation of nodejs middleware node-images for processing images. It is of great practical value. Friends in need can refer to it
Cross-platform image decoder(png/jpeg/gif) and encoder(png/jpeg) for Node.js
node.js lightweight cross-platform image encoding and decoding library
var images = require("images"); images("input.jpg") //Load image from file //加载图像文件 .size(400) //Geometric scaling the image to 400 pixels width //等比缩放图像到400像素宽 .draw(images("logo.png"), 10, 10) //Drawn logo at coordinates (10,10) //在(10,10)处绘制Logo .save("output.jpg", { //Save the image to a file,whih quality 50 quality : 50 //保存图片到文件,图片质量为50 });
Features Features
Lightweight: no need to install any image processing library.
Library.Cross-platform: Released a compiled .node file on windows, just download and start.
Cross-platform: Released a compiled .node file on windows, just download and start. A good .node file can be downloaded and used.API
.Simple and reliable!$ npm install images
node-images provide jQuery-like Chaining API,You can start the chain like this:
node-images Provide jQuery-like Chaining API,You can start the chain like this:
/* Load and decode image from file */ /* 从指定文件加载并解码图像 */ images(file) /* Create a new transparent image */ /* 创建一个指定宽高的透明图像 */ images(width, height) /* Load and decode image from a buffer */ /* 从Buffer数据中解码图像 */ images(buffer[, start[, end]]) /* Copy from another image */ /* 从另一个图像中复制区域来创建图像 */ images(image[, x, y, width, height])
images(width, height)
Create a new transparent imageCreate a transparent image with a specified width and heightimages(buffer[, start[, end]])
Load and decode image from a buffer decodes the image from the Buffer dataimages(image[, x, y, width, height])
Copy from another imageCopy from another image area to create the image.fill(red, green, blue[, alpha])
eg:images(200, 100).fill(0xff, 0x00, 0x00, 0.5) Fill image with color Fill the image with the specified color.draw(image, x, y)
Draw image on thecurrent
imageposition(x, y) draws image on the current image (x, y) image
.encode(type[, config])eg:images("input.png").encode("jpg", {operation:50}) Encode image to buffer, config is image setting.
Encode the current image in the specified format Image to Buffer, config is the image setting, currently supports setting the JPG image quality
Return
buffer
Return the filled Buffer
Note: The operation will cut off the chain
Encode and save the current image to file. If type is not specified, the file type is automatically determined based on file. config is image setting. Currently, it supports setting JPG images. Quality
.size([width[, height]])
Get size of the image or set the size of the image,if the height is not specified, then scaling based on the current width and height gets or sets the image width and height. If height is not specified, scale based on the current width and height
.resize(width[, height])
Set the size of the image, if the height is not specified, then scaling based on the current width and heightSet the size of the image, if the height is not specified, then scaling based on the current width and height Scaling, the bicubic algorithm is used by default..width([width])
Get width for the image or set width of the imageGet or set the width of the image.height([height])
Get height for the image or set height of the imageGet or set the height of the image##images.setLimit(width, height)Set the limit size of
eachimage Set the size limit for images processed by the library. After setting, it will take effect for all new operations (if the limit is exceeded,
throws an exception)
images.setGCThreshold(value)Set the garbage collection threshold Set the threshold for automatic gc of the image processing library (whenaddsmemory When usage exceeds this threshold, garbage collection is performed)
images.getUsedMemory()
Get used memory (in bytes)Get the memory size occupied by the image processing library (in bytes)
images.gc()
Forced call garbage collection forces the call to V8’s garbage collection mechanism
github.com/zhangyuanwei/node-images
[Related recommendations]
1.Free js online Video Tutorial
2.JavaScript Chinese Reference Manual
3.php.cnDugu Jiujian (3)-JavaScript Video Tutorial
The above is the detailed content of Middleware example tutorial for setting images in nodejs. For more information, please follow other related articles on the PHP Chinese website!