1

我有一个包含大约 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,我会得到不同的输出,并且 gm 模块正在与其他 console.logs 一起调用。见下文...

.
.
.
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 会像这样批量执行代码?

这是同步/异步问题吗?以及如何将其更改为...

一个)。请允许我处理数百张图像 b)。处理图像以至少解决此问题,使其正常工作:-)

提前感谢您的任何帮助和想法。

干杯,尼克

4

0 回答 0