2021-04-21 19:22:58 +02:00
|
|
|
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
|
|
|
|
2021-05-17 01:12:39 +02:00
|
|
|
import {UPLOAD_DIR, UPLOAD_URL} from './Dirs'
|
2021-04-21 19:22:58 +02:00
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const resizeImage = async (filename: string) => {
|
2021-04-21 19:22:58 +02:00
|
|
|
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) {
|
2021-04-21 19:22:58 +02:00
|
|
|
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)
|
2021-04-21 19:22:58 +02:00
|
|
|
.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-04-21 19:22:58 +02:00
|
|
|
}))
|
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;
|
|
|
|
|
}
|
2021-04-21 19:22:58 +02:00
|
|
|
return images
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
async function getDimensions(imagePath: string) {
|
2021-04-21 19:22:58 +02:00
|
|
|
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,
|
|
|
|
|
}
|