发布
社区首页 >问答首页 >如何处理超过500 MB的图像?

如何处理超过500 MB的图像?
EN

Stack Overflow用户
提问于 2022-08-17 17:49:02
回答 1查看 83关注 0票数 0

当处理小于500 MB的图像时,代码可以正常工作。(我试图做+25 GB的值)有什么方法来修改代码,使它与更多的图像工作吗?

我收到一个错误,看起来是这样的:

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

错误: EIO: i/o错误,写{ errno:-5,代码:'EIO',syscall:‘写’}

或者这个:

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

错误:在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}

这就是我目前的情况:

代码语言:javascript
代码运行次数:0
复制
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);
    })
);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 22:48:28

所以看起来imagemin在到达for循环之前一次抓取了所有的图像,然后崩溃了,所以我做了它,这样它就可以同步处理所有的图像。

所以我做了下面的代码。它很健壮,可以一次完成所需的尽可能多的图像。它还复制文件夹结构,使事情变得简单。希望它能帮助其他人使用Mozjpeg编码器为他们自己的图像优化大量的jpeg文件。(如果您想要imageminJpegtranimageminPngquant等其他插件,也可以轻松地更改它。)

我想出的是:

代码语言:javascript
代码运行次数:0
复制
// 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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73392887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档