Thumbnail : Thumbnail generator

更新时间:
2024-05-15
下载文档

Thumbnail : Thumbnail generator

The thumbnail module provides thumbnail image generate function.

User can use the following code to import the thumbnail module.

var thumbnail = require('thumbnail');

Support

The following shows thumbnail module APIs available for each permissions.

 User ModePrivilege Mode
thumbnail.generate

Thumbnail Object

thumbnail.generate(path[, opt])

  • path {String | Array} Single media file path or an array of multiple media file paths.
  • opt {Object} Thumbnail image attribute.
  • Returns: {Buffer} Thumbnail image file content.

Generate thumbnail image of specified media file. Currently supported image file formats include: 'jpg', 'png', 'bmp', 'tga', 'hdr', 'gif'. If the input media file is a video or GIF animation, this method will select the appropriate frame as the thumbnail.

opt can contain the following members:

  • max {Integer} Max side length, 1 ~ 640. default: 240.
  • format {String} Output format, 'jpg', 'png'. default: 'jpg'.
  • quality {Integer} If the target is JPEG format, quality can be specified here, optional range: 1 ~ 100. default: 70.
  • center {Boolean} Used center content as album cover, default: false

Example

This example show how to generate thumbnail image of single media file.

var fs = require('fs');
var thumbnail = require('thumbnail');

try {
  var jpg = thumbnail.generate('./test.jpg', {
    max: 320, format: 'jpg'
  });
} catch (error) {
  console.error(error.message);
  return;
}

if (jpg) {
  fs.writeFile('test-thumb.jpg', jpg);
}

This example show how to generate thumbnail image of multiple media file.

var fs = require('fs');
var thumbnail = require('thumbnail');

try {
  var png = thumbnail.generate(['1.mp4', '2.jpg', '3.jpg'], { 
    max: 640, format: 'png'
  });
} catch (error) {
  console.error(error.message);
  return;
}

if (png) {
  fs.writeFile('test-thumb.png', png);
}
文档内容是否对您有所帮助?
有帮助
没帮助