nodejs怎么实现对图片进行批量裁剪?


当前第2页 返回上一页

3、app.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

const fs = require('fs')

const { basename } = require('path')

// 压缩文件

const archiver = require('archiver')

// canvas库,用于裁剪图片

const { createCanvas, loadImage } = require('canvas')

// 批量获取路径

const glob = require('glob')

const config = require('./config')

 

// 根据宽高获取配置

function getOptions(options, config) {

  const [sourceWidth, sourceHeight] = options

  const { width, height, isWidth, isHeight, scale } = config

  const haveWidth = [width, (sourceHeight * width * scale) / sourceWidth]

  const haveHeight = [(sourceWidth * height * scale) / sourceHeight, height]

  if (width === 0 || height === 0) {

    return [0, 0]

  }

  if (width && height) {

    if (isWidth) {

      return haveWidth

    }

    if (isHeight) {

      return haveHeight

    }

    return [width / scale, height / scale]

  }

  if (width && !height) {

    return haveWidth

  }

  if (height && !width) {

    return haveHeight

  }

  return options.map((item) => item / scale)

}

 

!(async () => {

  const paths = glob.sync('./images/*')

  // 压缩成zip

  const archive = archiver('zip', {

    zlib: {

      level: 9,

    },

  })

  // 输出到当前文件夹下的 image-resize.zip

  const output = fs.createWriteStream(__dirname + '/image-resize.zip')

  archive.pipe(output)

  for (let i = 0; i < paths.length; i++) {

    const path = paths[i]

    const image = await loadImage(path)

    const { width, height } = image

 

    // 由于使用了扩展运算符展开对象,这里需要为对象定义迭代器

    const obj = { width, height }

    obj[Symbol.iterator] = function () {

      return {

        next: function () {

          let objArr = Reflect.ownKeys(obj)

          if (this.index < objArr.length - 1) {

            let key = objArr[this.index]

            this.index++

            return { value: obj[key] }

          } else {

            return { done: true }

          }

        },

        index: 0,

      }

    }

    // 默认缩放2倍

    // const options = [width, height].map((item) => item / 2)

    const options = getOptions(obj, config)

    const canvas = createCanvas(...options)

    const ctx = canvas.getContext('2d')

    ctx.drawImage(image, 0, 0, ...options)

    archive.append(canvas.toBuffer(), { name: `${basename(path)}` })

  }

})()

4、config.js用于修改宽高等配置

1

2

3

4

5

6

7

8

9

10

module.exports = {

  width: 300,

  height: '',

  // 根据宽度等比缩放,优先级更高

  isWidth: true,

  // 根据高度等比缩放

  isHeight: false,

  // 宽高整体缩放

  scale: 1,

}

更多编程相关知识,请访问:编程视频课程!!

以上就是nodejs怎么实现对图片进行批量裁剪?的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

javascript怎么改变src属性值

h5里js和servlet实现文件上传的实现步骤

深入了解node.js中的非阻塞 i/o

javascript怎么校验ip地址是否合法

javascript的bom

学javascript要什么基础?

javascript如何设置只能输入数字

2021q1最受欢迎语言是什么?

js如何获取session中的值

javascript函数是什么

更多相关阅读请进入《node.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...