Skip to content

Instantly share code, notes, and snippets.

@larafale
Created January 19, 2023 12:08
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 larafale/e9cbdb8275a882ffa649f353fd03c505 to your computer and use it in GitHub Desktop.
Save larafale/e9cbdb8275a882ffa649f353fd03c505 to your computer and use it in GitHub Desktop.
/*
# 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