|
| 1 | +# Image Optimization Guide (PNG to WebP) |
| 2 | + |
| 3 | +This project includes a script to convert PNG images into optimized WebP files. |
| 4 | + |
| 5 | +## Script |
| 6 | + |
| 7 | +- Source: `src/scripts/optimize-png-to-webp.ts` |
| 8 | +- NPM command: `npm run optimize:png:webp` |
| 9 | + |
| 10 | +## Basic Usage |
| 11 | + |
| 12 | +Run with default settings: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm run optimize:png:webp |
| 16 | +``` |
| 17 | + |
| 18 | +Default behavior: |
| 19 | +- Scans `public/assets` recursively |
| 20 | +- Writes `.webp` files in the same directory structure |
| 21 | +- Skips files that are already up to date |
| 22 | + |
| 23 | +## Important: Pass Arguments Correctly |
| 24 | + |
| 25 | +When using custom options with an npm script, always use `--` before script arguments: |
| 26 | + |
| 27 | +```bash |
| 28 | +npm run optimize:png:webp -- --input public/assets/projects/red-ant-express.com.kh --output public/assets/projects/red-ant-express.com.kh |
| 29 | +``` |
| 30 | + |
| 31 | +Without `--`, npm may swallow the arguments and the script can fail. |
| 32 | + |
| 33 | +## Available Options |
| 34 | + |
| 35 | +- `--input <dir>`: source directory to scan recursively |
| 36 | +- `--output <dir>`: destination directory for generated `.webp` files |
| 37 | +- `--quality <1-100>`: WebP quality (default: `82`) |
| 38 | +- `--effort <0-6>`: encoder effort, higher = slower but better compression (default: `4`) |
| 39 | +- `--lossless`: enable lossless WebP output |
| 40 | +- `--overwrite`: overwrite existing `.webp` files |
| 41 | +- `--delete-original`: delete `.png` files after successful conversion |
| 42 | +- `--help`: show help |
| 43 | + |
| 44 | +## Common Examples |
| 45 | + |
| 46 | +Convert one project folder in place: |
| 47 | + |
| 48 | +```bash |
| 49 | +npm run optimize:png:webp -- --input public/assets/projects/pphat.netlify.app --output public/assets/projects/pphat.netlify.app |
| 50 | +``` |
| 51 | + |
| 52 | +Convert with stronger compression: |
| 53 | + |
| 54 | +```bash |
| 55 | +npm run optimize:png:webp -- --input public/assets/projects/blog-leatsophat.vercel.app --quality 75 --effort 6 |
| 56 | +``` |
| 57 | + |
| 58 | +Create lossless WebP files: |
| 59 | + |
| 60 | +```bash |
| 61 | +npm run optimize:png:webp -- --input public/assets/projects --lossless |
| 62 | +``` |
| 63 | + |
| 64 | +Overwrite existing output files: |
| 65 | + |
| 66 | +```bash |
| 67 | +npm run optimize:png:webp -- --input public/assets/projects --overwrite |
| 68 | +``` |
| 69 | + |
| 70 | +Delete original PNG files after conversion: |
| 71 | + |
| 72 | +```bash |
| 73 | +npm run optimize:png:webp -- --input public/assets/projects --delete-original |
| 74 | +``` |
| 75 | + |
| 76 | +## Output Summary |
| 77 | + |
| 78 | +After execution, the script prints: |
| 79 | +- scanned |
| 80 | +- converted |
| 81 | +- skipped |
| 82 | +- failed |
| 83 | + |
| 84 | +If `failed > 0`, the command exits with a non-zero code. |
| 85 | + |
| 86 | +## Recommended Workflow |
| 87 | + |
| 88 | +1. Start with a single folder using `--input`. |
| 89 | +2. Verify generated `.webp` images in browser. |
| 90 | +3. Use `--overwrite` only when you need to regenerate. |
| 91 | +4. Use `--delete-original` only after visual validation. |
0 commit comments