我有一个包含大约100个子文件夹的图像文件夹,每个文件夹大约有5个JPG。
我正在尝试使用节点循环遍历所有图像并调整它们以使最小尺寸为1200.它们是纵向和横向的混合,最小尺寸(w或h)应该是1200缩放其他尺寸保持宽高比。
我正在学习节点,所以不太了解平台,但对JS UI方面非常有信心。
我找到了一个npm walker模块,npm gm模块,它适用于单个JPG,所以我认为这很容易,但我有一个循环通过500多个图像的问题然后gm()代码决定在循环完成之后做这些事情但事情正在崩溃。
这是我的代码......
https://runkit.com/nmiddleweek/58d40b478c992a00146502ba
(我可以内联代码吗?)
以下是控制台错误......
在大约450次迭代后抛出......
processing... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-bruchetta-3500px-1.jpg
Finished... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-bruchetta-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-caprese-3500px-1.jpg
processing... /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Salads/italian-caprese-3500px-1.jpg
/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:228
proc.stdin.once('error', cb);
^
TypeError: Cannot read property 'once' of undefined
at gm._spawn (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:228:15)
at gm._exec (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/command.js:190:17)
at gm.proto.(anonymous function) [as size] (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/gm/lib/getters.js:68:12)
at resizeFile (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/scripts/imageResizer.js:17:22)
at Walker.<anonymous> (/Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/scripts/imageResizer.js:67:21)
at emitTwo (events.js:106:13)
at Walker.emit (events.js:191:7)
at /Users/nick.middleweek/dev/github/exp-ex225-Carousel-with-images/node_modules/walker/lib/walker.js:98:12
at FSReqWrap.oncomplete (fs.js:117:15)
JEG-UPD-PRT0018:scripts nick.middleweek$
取消注释第66行
如果我取消注释第66行并强制将正在处理的文件减少到20,我会得到一个不同的输出,并且正在使用其他console.log调用gm模块。见下文......
.
.
.
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Hungarian/istock-537623284.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Hungarian/istock-537729484.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-184348412.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-493159114.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-513809654.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-519681814.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-609908738.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/Ice Cream/istock-625144420.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/istock-496311554.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/istock-457422997.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/italian-bruchetta-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/italian-caprese-3500px-1.jpg
Current file: /Users/nick.middleweek/Google Drive/Exp-CDN (For Upload)/UK/exp/EX-225/images/_just-eat/shutterstock_504548443.jpg
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
... is wider
done
done
done
done
done
done
done
done
done
JEG-UPD-PRT0018:scripts nick.middleweek$
所以,这里正在执行gm模块,我可以检查磁盘上的一些JPG是否已经调整大小并保持纵横比 - 太棒了!
奇怪的是,日志中出现的顺序是......为什么Node会像这样批量执行代码?
这是同步/异步问题吗?我该如何将其更改为......
A)。请允许我处理数百张图片 B)。处理图像至少解决这个问题所以它才能正常工作: - )
提前感谢任何帮助和想法。
干杯, 尼克