0

我正在做的是压缩从文件中获得的缓冲区并将压缩后的缓冲区再次写入内存。发生的情况是方形和矩形图像被正确压缩,但垂直图像被旋转。我可以将任何选项传递给 imageminJpegtran 插件,以便图像保持不变,只是压缩?这是我的代码:

const fs = require("fs");
const imagemin = require("imagemin");
const imageminJpegtran = require('imagemin-jpegtran');
const imageminPngquant = require('imagemin-pngquant');

async function outputFile() {

    let imageBase64 = fs.readFileSync("test_ios.jpg", {encoding: "base64"})
    const butmap = Buffer.from(imageBase64, "base64")

    try {
        let compressedBuffer = await imagemin.buffer(butmap, {
            plugins: [
                imageminJpegtran(),
                imageminPngquant({
                    quality: [0.1, 0.8]
                })
            ]
        });
    
    
        compressedBuffer = compressedBuffer.toString("base64");
        const compressedBitmap = Buffer.from(compressedBuffer, "base64")

        fs.writeFileSync("./test_ios_copy.jpg", compressedBitmap)
    } catch (error) {
        console.log(error)
    }
    
}

4

1 回答 1

0

我最终使用了另一个名为 imagemin-mozjpeg 的插件。它允许在不旋转图像的情况下压缩图像。

const fs = require("fs");
const imagemin = require("imagemin");
const imageMozg = require("imagemin-mozjpeg");
const imageminPngquant = require('imagemin-pngquant');

async function outputFile() {

    let imageBase64 = fs.readFileSync("test_ios.jpg", {encoding: "base64"})
    const butmap = Buffer.from(imageBase64, "base64")

    try {
        let compressedBuffer = await imagemin.buffer(butmap, {
            plugins: [
                imageMozg({
                        quality: 75, 
                        progressive: true
                    }),
                imageminPngquant({
                    quality: [0.1, 0.8]
                })
            ]
        });
    
    
        compressedBuffer = compressedBuffer.toString("base64");
        const compressedBitmap = Buffer.from(compressedBuffer, "base64")

        fs.writeFileSync("./test_ios_copy.jpg", compressedBitmap)
    } catch (error) {
        console.log(error)
    }
    
}

于 2021-03-30T05:54:07.667 回答