Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在不改变图像纵横比的情况下使用Sharp压缩图像大小_Node.js_Sharp - Fatal编程技术网

Node.js 在不改变图像纵横比的情况下使用Sharp压缩图像大小

Node.js 在不改变图像纵横比的情况下使用Sharp压缩图像大小,node.js,sharp,Node.js,Sharp,我有一个aws图像文件,我正在使用sharp压缩图像,但压缩后,文件大小大幅增加。在我的例子中,我有一个6.5MB的图像大小,经过压缩后它变成了19.5MB 以下是我的代码: const width = parseInt(image.height); const height = parseInt(image.width); var getObjectParams = { Bucket: BUCKET, Key: FILE_NAME } s3.getObject(getObjec

我有一个aws图像文件,我正在使用sharp压缩图像,但压缩后,文件大小大幅增加。在我的例子中,我有一个6.5MB的图像大小,经过压缩后它变成了19.5MB

以下是我的代码:

const width = parseInt(image.height);
const height = parseInt(image.width);

var getObjectParams = {
  Bucket: BUCKET,
  Key: FILE_NAME
}

  s3.getObject(getObjectParams).promise()
    .then(data => {
      sharp(data.Body)
       .resize(width, height)
        .ignoreAspectRatio()
        .toFormat('png')
        .toBuffer()
        .then((buffer) => {
         ........
         ........
    }
我甚至尝试过使用
.max()

 sharp(data.Body)
 .withoutEnlargement()
 .resize(width, height)
 .toFormat('png')
 .toBuffer()

同样的问题是,文件大小正在增加。在我的情况下,图像纵横比应该保持不变。如有任何建议,将不胜感激。谢谢

这是夏普的一个常见问题,但这是一个很好的惊人的软件包。阅读
png
Sharp

如果你需要玩玩;
options.quality
options.colors
options.dither
压缩大量PNG图像并根据其文档

需要使用libimagequant支持编译的libvips

因此,您需要
libimagequant
的帮助才能实现您的目标。 据该网站称,libimagequant是一个小型便携式C库,用于将RGBA图像高质量转换为8位索引彩色(调色板)图像

相反,您可以使用,我相信它使用相同的方法来压缩PNG图像。使用下面的一小段代码,您可以减小PNG的大小

const files = await imagemin(['images/*.{png}'], {
    destination: 'build/images',
        plugins: [
            imageminJpegtran(),
            imageminPngquant({
                quality: [0.6, 0.8]
            })
        ]
});

使用
quality:[0.6,0.8]
like
quality:[0.2,0.2]
减小大小,您可以传递单个图像文件,如
const files=wait imagemin(['fileName.png']

您已经在上问过这个问题了吗?如果没有,请这样做,因为作为一个开放源码库,这确实是第一个要问的地方。@Mike'Pomax'Kamermans是的,我已经在github中问过了很好。看起来您想在那里编辑您的问题,github有不同的代码格式(所以使用缩进,GH在代码块之前和之后使用“```)@Mike'Pomax'Kamermans,谢谢你的建议,我会研究一下的