0

我正在尝试在我的 nodejs 服务器上使用 imagemin.buffer() 和 'imagemin-mozjpeg' 插件。

我的目标是使用 axios(作为缓冲区)从 url 下载外部图像,使用 imagemin-mozjpeg(有损压缩)对其进行压缩,然后返回压缩后的图像缓冲区。

我的代码运行没有任何错误。但由于某种原因,返回的缓冲区与原始缓冲区相同(因此,由于某种原因未应用压缩)。

我的代码:

  const axios = require('axios');
    const imagemin = require('imagemin');
    const imageminMozjpeg = require('imagemin-mozjpeg');
    
    export async function compress() {
        console.log("COMPRESS CALLLED");
    
        const initialBuffer = await axios.get('https://yt3.ggpht.com/ytc/AKedOLQZtTtwHGLWHDlhHaTvLw5RH9I-9oGOsv8_8FBm=s151-c-k-c0x00ffffff-no-rj', {
                responseType: 'arraybuffer'
            })
            .then(response => {
                return Buffer.from(response.data);
            });
    
        const compressedBuffer = await imagemin.buffer(initialBuffer, {
            use: [
                imageminMozjpeg({
                    quality: 40
                })
            ]
        });
        return compressedBuffer;
    }
4

0 回答 0