如何处理超过500 MB的图像



代码在处理小于500 MB的图像时工作良好。(我正在尝试做+25 GB(有没有办法修改代码,使其能够处理更多的图像?

我得到一个错误,看起来像这样:

节点:内部/进程/承诺:279triggerUncaughtException(err,true/*fromPromise*/(;^

[错误:EIO:i/o错误,写入]{错误号:-5,代码:"EIO",syscall:'write'}

或者这个:

节点:内部/进程/承诺:279triggerUncaughtException(err,true/*fromPromise*/(;^

错误:读取ENOTCONN在tryReadStart(节点:net:614:20(在Socket_读取(节点:网络:625:5(位于Socket.Readable.read(节点:internal/streams/Readable:487:10(在Socket.read(节点:net:666:39(在新套接字(节点:net:451:12(位于Object.Socket(节点:net:286:41(在createSocket(节点:internal/child_process:328:14(位于ChildProcess.spawn(节点:internal/child_process:445:23(位于Object.spawn(节点:child_process:700:9(在execa(file:///mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/execa/index.js:84:26){错误号:-107,代码:"ENOTCONN",syscall:'read',originalMessage:"读取ENOTCONN",shortMessage:'命令失败,ENOTCONN:/mnt/d/Projects/GH_2022/imagemin mozjpeg/node_modules/mozjpeg/vender/cjpeg-quality 75\n'+'读取ENOTCONN',命令:'/mnt/d/Projects/GH_2022/imagemin mozjpeg/node_modules/mozjpeg/vender/cjpeg-quality 75',escapedCommand:"/mnt/d/Projects/GH_2022/imagemin mozjpeg/node_modules/mozjpeg/vender/cjpeg"-质量75’,exitCode:未定义,信号:未定义,signalDescription:未定义,stdout:Buffer(0([Uint8Array][],stderr:Buffer(0([Uint8Array][],失败:true,timedOut:false,isCanceled:false,killed: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);
})
);

所以看起来imagemin在进入for循环并崩溃之前,会一次抓取所有图像,所以我只是让它同步处理所有图像。

所以我制作了以下代码。它很强大,可以一次完成所需的图像。它还复制了文件夹结构,使事情变得简单。希望它能帮助其他想要使用Mozjpeg编码器为自己的图像优化大量jpeg文件的人。(如果你想要imageminJpegtranimageminPngquant等插件,你也可以很容易地将其更改为其他插件。(

以下是我的想法:

// 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 })
]
}
);
console.log( src + '...Done' );
}
function ParsePath(filepath) {
return path.parse(filepath).dir;
}

相关内容

  • 没有找到相关文章

最新更新