Home  >  Article  >  Web Front-end  >  Middleware example tutorial for setting images in nodejs

Middleware example tutorial for setting images in nodejs

零下一度
零下一度Original
2017-05-09 10:08:431221browse

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

  1. Lightweight: no need to install any image processing library.

    Library.
  2. 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.
  3. Easy-to-use: Provide
  4. jQuery
  5. -like chaining

    API

    .Simple and reliable!
  6. Easy to use: jQuery style API, simple and reliable.

  7. Installation Installation
  8. $ npm install images

  9. API
Interface

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(file)


Load and decode image from fileLoad and decode image from the specified file

images(width, height)

Create a new transparent imageCreate a transparent image with a specified width and height

images(buffer[, start[, end]])

Load and decode image from a buffer decodes the image from the Buffer data

images(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 the

current

image

position(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

Note: This operation will cut off the call chain


See:.save(file[, type [, config]]) Reference:.save(file[, type[, config]])


.save(file[, type[, config]])


eg:images("input.png").encode("output.jpg", {operation:50}) Encoding and save the current image to a file, if the type is not specified, type well be automatically determined according to the file, config is image setting. eg: { operation:50 }

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 height

Set 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

each

image 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 (when adds memory 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!

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