Created
May 3, 2024 22:26
-
-
Save lporras/c1f39c6edf2ef3cf5cd608e3263ef5f9 to your computer and use it in GitHub Desktop.
Compress Image Imagemagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install imagemagick | |
const fs = require('fs'); | |
const { exec } = require('child_process'); | |
// Function to compress PNG using ImageMagick | |
function compressPNG(inputFile, outputFile, quality = 80) { | |
const command = `convert ${inputFile} -quality ${quality} ${outputFile}`; | |
exec(command, (error, stdout, stderr) => { | |
if (error) { | |
console.error(`Error: ${error.message}`); | |
return; | |
} | |
if (stderr) { | |
console.error(`stderr: ${stderr}`); | |
return; | |
} | |
console.log(`Image compressed successfully: ${outputFile}`); | |
}); | |
} | |
// Example usage | |
const inputFilePath = 'input.png'; | |
const outputFilePath = 'output_compressed.png'; | |
const compressionQuality = 80; // Adjust quality as needed (0-100) | |
// Compress PNG | |
compressPNG(inputFilePath, outputFilePath, compressionQuality); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment