2021-04-21 09:54:10 +02:00
|
|
|
#!/bin/env node
|
|
|
|
|
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import sharp from 'sharp'
|
|
|
|
|
import exif from 'exif'
|
|
|
|
|
|
|
|
|
|
async function getExifOrientation(imagePath) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
new exif.ExifImage({ image: imagePath }, function (error, exifData) {
|
|
|
|
|
if (error) {
|
|
|
|
|
resolve(0)
|
|
|
|
|
} else {
|
|
|
|
|
resolve(exifData.image.Orientation)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 10:15:08 +02:00
|
|
|
const dir = `./../data/uploads`
|
2021-04-21 09:54:10 +02:00
|
|
|
const images = fs.readdirSync(dir)
|
|
|
|
|
images.forEach(async (image) => {
|
2021-04-21 10:15:08 +02:00
|
|
|
if (!image.toLowerCase().match(/\.(jpe?g|webp|png)$/)) {
|
2021-04-21 09:54:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
console.log(image)
|
|
|
|
|
|
|
|
|
|
const imagePath = `${dir}/${image}`
|
|
|
|
|
const iamgeOutPath = `${dir}/r/${image}`
|
|
|
|
|
const orientation = await getExifOrientation(imagePath)
|
|
|
|
|
|
2021-04-21 10:18:13 +02:00
|
|
|
let sharpImg = sharp(imagePath, { failOnError: false })
|
2021-04-21 09:54:10 +02:00
|
|
|
// when image is rotated to the left or right, switch width/height
|
|
|
|
|
// https://jdhao.github.io/2019/07/31/image_rotation_exif_info/
|
|
|
|
|
if (orientation === 6) {
|
|
|
|
|
sharpImg = sharpImg.rotate()
|
|
|
|
|
} else if (orientation === 3) {
|
|
|
|
|
sharpImg = sharpImg.rotate().rotate()
|
|
|
|
|
} else if (orientation === 8) {
|
|
|
|
|
sharpImg = sharpImg.rotate().rotate().rotate()
|
|
|
|
|
}
|
|
|
|
|
const sizes = [
|
|
|
|
|
[150, 100],
|
|
|
|
|
[375, 210],
|
|
|
|
|
]
|
|
|
|
|
sizes.forEach(([w, h]) => {
|
|
|
|
|
sharpImg.resize(w, h, {fit: 'contain'}).toFile(`${iamgeOutPath}-${w}x${h}.webp`)
|
|
|
|
|
})
|
|
|
|
|
})
|