Skip to content

Instantly share code, notes, and snippets.

@keegandonley
Last active October 30, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keegandonley/ddbb42448c650fc1b42ff22b1482fc70 to your computer and use it in GitHub Desktop.
Save keegandonley/ddbb42448c650fc1b42ff22b1482fc70 to your computer and use it in GitHub Desktop.
Image Resizer
name: Resize images
on: push
jobs:
build:
if: "!contains(github.event.head_commit.message, 'ci skip')"
name: Resize Images and Convert to WebP
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master
- name: Compress Images
id: resize-images
uses: petems/image-resizer-gh-action@master
with:
target: public/uploads # directory to look for images in
dimensions: 50% # parameter to change size
widthLimit: 2000 # max width to check
heightLimit: 2000 # max height to check
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm install
- run: npm run webp
- name: Commit changes
uses: EndBug/add-and-commit@v4
with:
add: 'public/uploads/'
author_name: 'Keegan Donley'
author_email: 'kd@keegandonley.com'
message: 'Images resized (ci skip)'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
import cwebp from 'cwebp-bin';
import fs from 'fs';
import { execFile } from 'node:child_process';
import path from 'path';
fs.readdirSync('./public/uploads').forEach((file) => {
if (file.endsWith('.png')) {
console.log('converting', file);
execFile(cwebp, [
path.join('public', 'uploads', file),
'-o',
path.join('public', 'uploads', file.replace('.png', '.webp')),
]);
}
if (file.endsWith('.jpg')) {
console.log('converting', file);
execFile(cwebp, [
path.join('public', 'uploads', file),
'-o',
path.join('public', 'uploads', file.replace('.jpg', '.webp')),
]);
}
if (file.endsWith('.JPG')) {
console.log('converting', file);
execFile(cwebp, [
path.join('public', 'uploads', file),
'-o',
path.join('public', 'uploads', file.replace('.JPG', '.webp')),
]);
}
if (file.endsWith('.jpeg')) {
console.log('converting', file);
execFile(cwebp, [
path.join('public', 'uploads', file),
'-o',
path.join('public', 'uploads', file.replace('.jpeg', '.webp')),
]);
}
});
console.log('done with webp conversion!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment