Created
January 19, 2023 12:08
-
-
Save larafale/e9cbdb8275a882ffa649f353fd03c505 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
# Resize an Image | |
- Prompts user to install "jimp" | |
- Uses image path from currently selected file in Finder | |
- Prompts user for resize dimensions | |
- Resizes and reveals new file in Finder | |
*/ | |
// Menu: Resize an Image | |
// Description: Select an Image in Finder to Resize | |
// Author: John Lindquist | |
// Twitter: @johnlindquist | |
import "@johnlindquist/kit" | |
let Jimp = await npm("jimp") | |
let imagePath = await getSelectedFile() | |
if (!imagePath) imagePath = await selectFile(`Choose an image:`) | |
console.log({ imagePath }) | |
let extension = path.extname(imagePath) | |
let allowImageExtensions = [".png", ".jpg"] | |
while (!allowImageExtensions.includes(extension)) { | |
let fileName = path.basename(imagePath) | |
imagePath = await selectFile(`${fileName} wasn't an image:`) | |
if (!imagePath) exit() | |
extension = path.extname(imagePath) | |
} | |
let width = Number( | |
await arg({ | |
placeholder: "Enter width", | |
hint: imagePath, | |
panel: `<img src="${imagePath}" class="w-full"/>`, | |
}) | |
) | |
let image = await Jimp.read(imagePath) | |
let newHeight = Math.floor(image.bitmap.height * (width / image.bitmap.width)) | |
let resizedImagePath = imagePath.replace(new RegExp(`${extension}$`), `-${width}${extension}`) | |
await image.resize(width, newHeight).write(resizedImagePath) | |
revealFile(resizedImagePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment