Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic image optimization #6350

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions site/scripts/get-contributors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require('dotenv/config');
const fs = require('fs');
const fetch = require('node-fetch');
const Jimp = require('jimp');

process.chdir(__dirname);

const base = `https://api.github.com/repos/sveltejs/svelte/contributors`;
const { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } = process.env;

const SIZE = 64;

Expand All @@ -15,7 +13,7 @@ async function main() {
let page = 1;

while (true) {
const res = await fetch(`${base}?client_id=${GITHUB_CLIENT_ID}&client_secret=${GITHUB_CLIENT_SECRET}&per_page=100&page=${page++}`);
const res = await fetch(`${base}?per_page=100&page=${page++}`);
const list = await res.json();

if (list.length === 0) break;
Expand All @@ -41,9 +39,29 @@ async function main() {
sprite.composite(image, i * SIZE, 0);
}

await sprite.quality(80).write(`../static/contributors.jpg`);
// TODO: Optimizing the static/contributors.jpg image should probably get automated as well
console.log('remember to additionally optimize the resulting /static/contributors.jpg image file via e.g. https://squoosh.app ');
await sprite.quality(80).getBuffer(Jimp.MIME_JPEG, (err, buffer) => {
let quality = 75;

let content = `--xxxxxxxxxx\r\nContent-Disposition: form-data; name="qlty"; \r\n\r\n${quality}\r\n--xxxxxxxxxx\r\nContent-Disposition: form-data; name="files"; filename="contributors.jpg"\r\nContent-Type:application/octet-stream\r\n\r\n`;
let end = `\r\n--xxxxxxxxxx--\r\n`;

let body = Buffer.concat([Buffer.from(content, "utf8"), new Buffer(buffer, "binary"), Buffer.from(end)]);

fetch('https://api.resmush.it/ws.php', {
method: 'POST',
headers: {
"Content-type": 'multipart/form-data; boundary=xxxxxxxxxx',
},
body: body
}).then(async (res) => {
alb marked this conversation as resolved.
Show resolved Hide resolved
let response = await res.json()
console.log(`Reduced file size by ${response.percent}%`)

fetch(response.dest).then(async image => {
fs.writeFileSync('../static/contributors.jpg', await image.buffer());
});
});
});

const str = `[\n\t${authors.map(a => `'${a.login}'`).join(',\n\t')}\n]`;

Expand Down