Skip to content

Instantly share code, notes, and snippets.

@erikside
Forked from skeletoncrewrip/png_increase.mjs
Created May 24, 2023 09:18
Show Gist options
  • Save erikside/98bac4d7924848f2b014cca424be8333 to your computer and use it in GitHub Desktop.
Save erikside/98bac4d7924848f2b014cca424be8333 to your computer and use it in GitHub Desktop.
Enlarge the size of PNGs by repeatedly writing a string to metadata
import png from 'png-metadata';
import fs from 'fs';
const desiredSize = 24000;
const folder = './assets/';
const creatorString = 'Skeleton Crew';
const fillerString = 'https://treattoolbox.com ';
const bumpImageSize = async (file) => {
if (file.endsWith('.png')) {
const filePath = folder + file;
let fileSize = (await fs.promises.stat(filePath)).size;
if (fileSize <= desiredSize) {
console.log('bumping ' + filePath);
// load from file
let s = png.readFileSync(filePath);
// split
let list = png.splitChunk(s);
// append
let iend = list.pop(); // remove IEND
const creatorSize = getBytes(creatorString);
let creatorChunk = png.createChunk('aaAa', creatorString);
list.push(creatorChunk);
fileSize += creatorSize;
let fillingString = '';
while (fileSize + getBytes(fillingString) <= desiredSize) {
// const fillingSize = getBytes(fillingString);
fillingString += fillerString;
}
let fillingChunk = png.createChunk('aaAb', fillingString)
list.push(fillingChunk);
list.push(iend);
// join and save
let newpng = png.joinChunk(list);
fs.writeFileSync(filePath, newpng, 'binary');
}
}
}
function getBytes(string) {
return Buffer.byteLength(string, 'utf8')
}
fs.readdirSync(folder).forEach(file => bumpImageSize(file));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment