puzzle/src/server/Images.ts

109 lines
2.7 KiB
TypeScript
Raw Normal View History

import sizeOf from 'image-size'
import fs from 'fs'
import exif from 'exif'
import sharp from 'sharp'
2021-05-17 00:27:47 +02:00
import {UPLOAD_DIR, UPLOAD_URL} from './Dirs'
2021-05-17 00:27:47 +02:00
const resizeImage = async (filename: string) => {
if (!filename.toLowerCase().match(/\.(jpe?g|webp|png)$/)) {
return
}
const imagePath = `${UPLOAD_DIR}/${filename}`
const imageOutPath = `${UPLOAD_DIR}/r/${filename}`
const orientation = await getExifOrientation(imagePath)
let sharpImg = sharp(imagePath, { failOnError: false })
// 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],
]
for (let [w,h] of sizes) {
console.log(w, h, imagePath)
await sharpImg.resize(w, h, { fit: 'contain' }).toFile(`${imageOutPath}-${w}x${h}.webp`)
}
}
2021-05-17 00:27:47 +02:00
async function getExifOrientation(imagePath: string) {
return new Promise((resolve, reject) => {
new exif.ExifImage({ image: imagePath }, function (error, exifData) {
if (error) {
resolve(0)
} else {
resolve(exifData.image.Orientation)
}
})
})
}
2021-05-21 00:43:02 +02:00
const allImages = (sort: string) => {
let images = fs.readdirSync(UPLOAD_DIR)
.filter(f => f.toLowerCase().match(/\.(jpe?g|webp|png)$/))
.map(f => ({
filename: f,
file: `${UPLOAD_DIR}/${f}`,
2021-05-04 09:48:14 +02:00
url: `${UPLOAD_URL}/${encodeURIComponent(f)}`,
2021-05-21 00:43:02 +02:00
title: '',
category: '',
ts: fs.statSync(`${UPLOAD_DIR}/${f}`).mtime.getTime(),
}))
2021-05-21 00:43:02 +02:00
switch (sort) {
case 'alpha_asc':
images = images.sort((a, b) => {
return a.file > b.file ? 1 : -1
})
break;
case 'alpha_desc':
images = images.sort((a, b) => {
return a.file < b.file ? 1 : -1
})
break;
case 'date_asc':
images = images.sort((a, b) => {
return a.ts > b.ts ? 1 : -1
})
break;
case 'date_desc':
default:
images = images.sort((a, b) => {
return a.ts < b.ts ? 1 : -1
})
break;
}
return images
}
2021-05-17 00:27:47 +02:00
async function getDimensions(imagePath: string) {
let dimensions = sizeOf(imagePath)
const orientation = await getExifOrientation(imagePath)
// 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 || orientation === 8) {
return {
width: dimensions.height,
height: dimensions.width,
}
}
return dimensions
}
export default {
allImages,
resizeImage,
getDimensions,
}