image operations via Images.js, directory fun via Dirs.js
This commit is contained in:
parent
0cbc8e69a3
commit
1089f70d5e
9 changed files with 122 additions and 166 deletions
|
|
@ -1,7 +1,5 @@
|
|||
#!/bin/sh
|
||||
|
||||
cd "$RUN_DIR/server"
|
||||
|
||||
npm install
|
||||
|
||||
if [ ! -e "$RUN_DIR/server/config.js" ]; then
|
||||
|
|
|
|||
|
|
@ -1,48 +1,9 @@
|
|||
#!/bin/env node
|
||||
|
||||
import fs from 'fs'
|
||||
import sharp from 'sharp'
|
||||
import exif from 'exif'
|
||||
import Images from './../server/Images.js'
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const dir = `./../data/uploads`
|
||||
const images = fs.readdirSync(dir)
|
||||
const images = Images.allImages()
|
||||
images.forEach(async (image) => {
|
||||
if (!image.toLowerCase().match(/\.(jpe?g|webp|png)$/)) {
|
||||
return
|
||||
}
|
||||
console.log(image)
|
||||
|
||||
const imagePath = `${dir}/${image}`
|
||||
const iamgeOutPath = `${dir}/r/${image}`
|
||||
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],
|
||||
]
|
||||
sizes.forEach(([w, h]) => {
|
||||
sharpImg.resize(w, h, {fit: 'contain'}).toFile(`${iamgeOutPath}-${w}x${h}.webp`)
|
||||
})
|
||||
Images.resizeImage(image.filename)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
import fs from 'fs'
|
||||
import Protocol from '../common/Protocol.js'
|
||||
import { logger } from '../common/Util.js'
|
||||
import { DATA_DIR } from '../server/Dirs.js'
|
||||
|
||||
const log = logger('rewrite_logs')
|
||||
|
||||
const DATA_DIR = '../data'
|
||||
|
||||
const filename = (gameId) => `${DATA_DIR}/log_${gameId}.log`
|
||||
|
||||
const rewrite = (gameId) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue