Created
August 9, 2024 09:10
-
-
Save ainsleyclark/5af2d785638cde47e381ec3ae213e52b to your computer and use it in GitHub Desktop.
Digital Ocean Sharp Crashes
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
| const sharp = require('sharp'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const imageSizes = [ | |
| // Original Size (for WebP & Avif) | |
| { | |
| name: 'original_webp', | |
| width: undefined, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'webp', | |
| options: { | |
| quality: 80, | |
| }, | |
| }, | |
| }, | |
| { | |
| name: 'original_avif', | |
| width: undefined, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'avif', | |
| options: { | |
| quality: 60, | |
| effort: 1, | |
| chromaSubsampling: '4:4:4', | |
| bitdepth: 8, | |
| lossless: false, | |
| }, | |
| }, | |
| }, | |
| // Thumbnail Sizes | |
| { | |
| name: 'thumbnail', | |
| width: 200, | |
| height: undefined, | |
| position: 'centre', | |
| }, | |
| { | |
| name: 'thumbnail_webp', | |
| width: 200, | |
| height: undefined, | |
| position: 'centre', | |
| formatOptions: { | |
| format: 'webp', | |
| options: { | |
| quality: 80, | |
| }, | |
| }, | |
| }, | |
| { | |
| name: 'thumbnail_avif', | |
| width: 200, | |
| height: undefined, | |
| position: 'centre', | |
| formatOptions: { | |
| format: 'avif', | |
| options: { | |
| quality: 60, | |
| effort: 1, | |
| chromaSubsampling: '4:4:4', | |
| bitdepth: 8, | |
| lossless: false, | |
| }, | |
| }, | |
| }, | |
| // Mobile Sizes | |
| { | |
| name: 'mobile', | |
| width: 500, | |
| height: undefined, | |
| }, | |
| { | |
| name: 'mobile_webp', | |
| width: 500, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'webp', | |
| options: { | |
| quality: 80, | |
| }, | |
| }, | |
| }, | |
| { | |
| name: 'mobile_avif', | |
| width: 500, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'avif', | |
| options: { | |
| quality: 60, | |
| effort: 1, | |
| chromaSubsampling: '4:4:4', | |
| bitdepth: 8, | |
| lossless: false, | |
| }, | |
| }, | |
| }, | |
| // Tablet Sizes | |
| { | |
| name: 'tablet', | |
| width: 800, | |
| height: undefined, | |
| }, | |
| { | |
| name: 'tablet_webp', | |
| width: 800, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'webp', | |
| options: { | |
| quality: 80, | |
| }, | |
| }, | |
| }, | |
| { | |
| name: 'tablet_avif', | |
| width: 800, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'avif', | |
| options: { | |
| quality: 60, | |
| effort: 1, | |
| chromaSubsampling: '4:4:4', | |
| bitdepth: 8, | |
| lossless: false, | |
| }, | |
| }, | |
| }, | |
| // Desktop Sizes | |
| { | |
| name: 'desktop', | |
| width: 1200, | |
| height: undefined, | |
| }, | |
| { | |
| name: 'desktop_webp', | |
| width: 1200, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'webp', | |
| options: { | |
| quality: 80, | |
| }, | |
| }, | |
| }, | |
| { | |
| name: 'desktop_avif', | |
| width: 1200, | |
| height: undefined, | |
| formatOptions: { | |
| format: 'avif', | |
| options: { | |
| quality: 60, | |
| effort: 1, | |
| chromaSubsampling: '4:4:4', | |
| bitdepth: 8, | |
| lossless: false, | |
| }, | |
| }, | |
| }, | |
| ]; | |
| const inputDirectory = 'path/to/your/images'; // Replace with your input directory | |
| const outputDirectory = 'path/to/output/images'; // Replace with your output directory | |
| async function processImage(imagePath) { | |
| const filename = path.basename(imagePath); | |
| const image = sharp(imagePath); | |
| for (const size of imageSizes) { | |
| const { name, width, height, formatOptions } = size; | |
| const outputPath = path.join(outputDirectory, `${filename}-${name}.${formatOptions?.format || 'jpg'}`); | |
| try { | |
| await image | |
| .clone() // Create a clone to avoid modifying the original image | |
| .resize(width, height, { | |
| fit: 'inside', // Adjust fit as needed | |
| }) | |
| .toFormat(formatOptions?.format || 'jpg', formatOptions?.options) | |
| .toFile(outputPath); | |
| console.log(`Created ${outputPath}`); | |
| } catch (error) { | |
| console.error(`Error processing image: ${error}`); | |
| } | |
| } | |
| } | |
| async function main() { | |
| const files = fs.readdirSync(inputDirectory); | |
| for (const file of files) { | |
| const imagePath = path.join(inputDirectory, file); | |
| await processImage(imagePath); | |
| } | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment