forked from zhouruizhe/gmTouringMiniApp
108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
import sharp from 'sharp'
|
|
import { readdir, mkdir, unlink } from 'node:fs/promises'
|
|
import { join, dirname } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const projectRoot = join(__dirname, '..')
|
|
const sourceDir = join(projectRoot, 'src/static/poi/photos')
|
|
const outputDir = sourceDir // 处理后直接覆盖到同目录(原文件先删除)
|
|
|
|
// 中文名 → POI ID 映射(全部30个景点)
|
|
const NAME_TO_POI_ID = {
|
|
// 文化场馆(已处理,保留映射以便确认)
|
|
'文化艺术中心': 'poi_gm_culture_center',
|
|
'光明区图书馆': 'poi_amap_b0hbtu4bfr',
|
|
'光明区文化馆': 'poi_amap_b0ffk306gx',
|
|
'少年儿童图书馆': 'poi_amap_b02f38meio',
|
|
'深圳国际美术馆': 'poi_amap_b0jun773j3',
|
|
'城市规划馆': 'poi_amap_b0jbbzx4oo',
|
|
'茅洲河图书馆': 'poi_amap_b0k025w32e',
|
|
// 公园湿地
|
|
'虹桥公园': 'poi_hongqiao_park',
|
|
'红花山公园': 'poi_honghuashan_park',
|
|
'科学公园': 'poi_amap_b0j2jc4n0w',
|
|
'鹅颈水湿地公园': 'poi_amap_b0ffmdhve1',
|
|
'明湖公园': 'poi_amap_b0fffwu1l5',
|
|
'光明新城公园': 'poi_amap_b0ffhfbsje',
|
|
'开明公园': 'poi_amap_b0ffjf1b20',
|
|
'左岸科技公园': 'poi_amap_b0g0kzrt5s',
|
|
'西田公园': 'poi_amap_b02f30abgs',
|
|
'大雁山森林公园': 'poi_amap_b0jgbp18uf',
|
|
'楼村湿地公园': 'poi_amap_b0grac6nzz',
|
|
'大顶岭山林公园': 'poi_amap_b02f309r88',
|
|
// 田园休闲
|
|
'光明农场大观园': 'poi_farm_grand_view',
|
|
'华侨城光明欢乐田园': 'poi_happy_pastoral',
|
|
'双晖稻田农场': 'poi_amap_b0ffkkmzsc',
|
|
// 人文地标
|
|
'弘源寺': 'poi_amap_b02f38nj9k',
|
|
'陈仙姑祠': 'poi_amap_b0ffgthbcf',
|
|
'黄氏大宗祠': 'poi_amap_b0fffttiov',
|
|
'玉律醒狮文化馆': 'poi_amap_b0kb4cxaby',
|
|
// 绿道户外
|
|
'大顶岭绿道': 'poi_dadingling_greenway',
|
|
'光明湖碧道': 'poi_amap_b0ldbsbeea',
|
|
'茅洲河碧道': 'poi_amap_b0kup558j4',
|
|
'五指耙森林公园': 'poi_amap_b0jb2aurvw',
|
|
}
|
|
|
|
const TARGET_WIDTH = 800
|
|
const TARGET_HEIGHT = 600
|
|
const JPEG_QUALITY = 80
|
|
|
|
async function process() {
|
|
const files = await readdir(sourceDir)
|
|
const jpgFiles = files.filter(f => f.endsWith('.jpg') || f.endsWith('.jpeg') || f.endsWith('.png'))
|
|
|
|
console.log(`Found ${jpgFiles.length} source images\n`)
|
|
|
|
for (const file of jpgFiles) {
|
|
const nameWithoutExt = file.replace(/\.(jpg|jpeg|png)$/i, '')
|
|
const poiId = NAME_TO_POI_ID[nameWithoutExt]
|
|
|
|
if (!poiId) {
|
|
console.log(`SKIP: "${file}" — no matching POI ID found`)
|
|
continue
|
|
}
|
|
|
|
const inputPath = join(sourceDir, file)
|
|
const outputName = `${poiId}.jpg`
|
|
const outputPath = join(outputDir, outputName)
|
|
|
|
console.log(`Processing: ${file} → ${outputName}`)
|
|
|
|
try {
|
|
await sharp(inputPath)
|
|
.rotate() // 根据 EXIF 自动旋转
|
|
.resize(TARGET_WIDTH, TARGET_HEIGHT, {
|
|
fit: 'cover',
|
|
position: 'attention', // 智能裁剪,尽量保留重要区域
|
|
})
|
|
.jpeg({
|
|
quality: JPEG_QUALITY,
|
|
mozjpeg: true,
|
|
chromaSubsampling: '4:2:0',
|
|
})
|
|
.toFile(outputPath)
|
|
|
|
// 删除原始中文名文件
|
|
if (file !== outputName) {
|
|
await unlink(inputPath)
|
|
}
|
|
|
|
console.log(` ✓ Done`)
|
|
} catch (err) {
|
|
console.error(` ✗ Failed: ${err.message}`)
|
|
}
|
|
}
|
|
|
|
console.log('\nDone! Listing output directory:')
|
|
const outputFiles = await readdir(outputDir)
|
|
for (const f of outputFiles.filter(f => f.endsWith('.jpg')).sort()) {
|
|
console.log(` ${f}`)
|
|
}
|
|
}
|
|
|
|
process().catch(console.error)
|