当处理小于500 MB的图像时,代码可以正常工作。(我试图做+25 GB的值)有什么方法来修改代码,使它与更多的图像工作吗?
我收到一个错误,看起来是这样的:
节点:内部/流程/承诺:279 triggerUncaughtException(err,true /* fromPromise */);^
错误: EIO: i/o错误,写{ errno:-5,代码:'EIO',syscall:‘写’}
或者这个:
错误:在tryReadStart (节点:net:614:20),Socket._read (节点:net:625:5),Socket.Readable.read (节点:内部/流/可读性:487:10),Socket.read (节点:net:666:39),新套接字(节点:net:415:12),Object.Socket (节点:net:286:41),createSocket (节点:内部/子进程:328:14),在ChildProcess.spawn (节点:内部/子进程:445:23),阅读ENOTCONN(file:///mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/execa/index.js:84:26) .spawn (node:child_process:700:9) { errno:-107,代码:'ENOTCONN',syscall:'read',originalMessage:'read ENOTCONN',shortMessage:‘'/mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg -quality 75\n’+ 'read ENOTCONN',命令:‘/mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg -quality 75','"/mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg“-quality 75',exitCode:未定义,信号:未定义,signalDescription:未定义,stdout: Buffer(0) Uint8Array,stderr: Buffer(0) Uint8Array,failed: true,timedOut: false,isCanceled: false,false :false:false}
这就是我目前的情况:
import imagemin from 'imagemin'; import imageminMozjpeg from 'imagemin-mozjpeg'; import { promises as fsPromises } from 'node:fs'; import { promisify } from 'node:util'; import path from 'node:path'; import fs from 'graceful-fs'; const writeFile = promisify(fs.writeFile); const srcdir = 'images/source'; const distdir = 'images/dist'; imagemin([srcdir + '/**/*.{jpg,jpeg,png}'], { plugins: [ imageminMozjpeg({ quality: 75 }).then(files => files .forEach(async v => { let source = path.parse(v.sourcePath); v.destinationPath = `${source.dir.replace(srcdir, distdir)}/${source.name}${source.ext}`; await fsPromises.mkdir(path.dirname(v.destinationPath), { recursive: true }); await writeFile(v.destinationPath, v.data); );
【玩转 GPU】有奖征文
精美礼品等你拿!
所以看起来imagemin在到达for循环之前一次抓取了所有的图像,然后崩溃了,所以我做了它,这样它就可以同步处理所有的图像。
所以我做了下面的代码。它很健壮,可以一次完成所需的尽可能多的图像。它还复制文件夹结构,使事情变得简单。希望它能帮助其他人使用Mozjpeg编码器为他们自己的图像优化大量的jpeg文件。(如果您想要 imageminJpegtran 、 imageminPngquant 等其他插件,也可以轻松地更改它。)
imageminJpegtran
imageminPngquant
我想出的是:
// mozjpegify.mjs import path from 'path'; import glob from 'glob'; import imagemin from 'imagemin'; import imageminMozjpeg from 'imagemin-mozjpeg'; const srcdir = 'images/source'; const distdir = 'images/dist'; Main(); async function Main() { GetPath(srcdir, GetPathCallback); function GetPath( src, callback ) { glob(src + '/**/*', callback); async function GetPathCallback(err, filePath) { if (!err) { for(let i=0; i<filePath.length; i++) { //console.log( filePath[i] + ' -> ' + filePath[i].replace(srcdir, distdir) ); // source -> target let ext = filePath[i].split('.').pop(); // get file extension if( ext == 'jpg' || ext == 'jpeg' || ext == 'png' ) { // make sure it's an image and not a folder or something await Optimize( filePath[i], ParsePath(filePath[i].replace(srcdir, distdir)) ); else { console.log('Error:', err); async function Optimize( src, destFolder ) { const files = await imagemin( [src], destination: destFolder, plugins: [ imageminMozjpeg({ quality: 75 })