mediac 1.6.8 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,7 @@ import { cpus } from "os"
18
18
  import pMap from 'p-map'
19
19
  import path from "path"
20
20
  import sharp from "sharp"
21
+ import * as core from '../lib/core.js'
21
22
  import * as log from '../lib/debug.js'
22
23
  import * as mf from '../lib/file.js'
23
24
  import * as helper from '../lib/helper.js'
@@ -106,9 +107,9 @@ const handler = async function cmdCompress(argv) {
106
107
  const walkOpts = {
107
108
  needStats: true,
108
109
  entryFilter: (f) =>
109
- f.stats.isFile()
110
+ f.isFile
110
111
  && !RE_THUMB.test(f.path)
111
- && f.stats.size > minFileSize
112
+ && f.size > minFileSize
112
113
  && helper.isImageFile(f.path)
113
114
  }
114
115
  log.showGreen(logTag, `Walking files ...`)
@@ -176,7 +177,7 @@ const handler = async function cmdCompress(argv) {
176
177
  })
177
178
  log.show(logTag, `in ${helper.humanTime(startMs)} tasks:`)
178
179
  tasks.slice(-1).forEach(t => {
179
- log.show(helper._omit(t, "stats", "bar1"))
180
+ log.show(core.omit(t, "stats", "bar1"))
180
181
  })
181
182
  log.info(logTag, argv)
182
183
  testMode && log.showYellow("++++++++++ TEST MODE (DRY RUN) ++++++++++")
@@ -267,11 +268,13 @@ async function preCompress(f, options = {}) {
267
268
  const st = await fs.stat(fileSrc)
268
269
  const m = await sharp(fileSrc).metadata()
269
270
  try {
271
+ // 跳过以前由mediac压缩过的图片,避免重复压缩
272
+ // 可能需要添加一个命令行参数控制
270
273
  if (m?.exif) {
271
274
  const md = exif(m.exif)?.Image
272
- if (md && (md.Copyright?.includes("mediac")
275
+ if (md.Copyright?.includes("mediac")
273
276
  || md.Software?.includes("mediac")
274
- || md.Artist?.includes("mediac"))) {
277
+ || md.Artist?.includes("mediac")) {
275
278
  log.info(logTag, "skip:", fileDst)
276
279
  return {
277
280
  ...f,
@@ -289,12 +292,7 @@ async function preCompress(f, options = {}) {
289
292
  log.fileLog(`ExifErr: <${fileSrc}> ${error.message}`, logTag)
290
293
  }
291
294
 
292
- const newWidth =
293
- m.width > m.height ? maxWidth : Math.round((maxWidth * m.width) / m.height)
294
- const newHeight = Math.round((newWidth * m.height) / m.width)
295
-
296
- const dstWidth = newWidth > m.width ? m.width : newWidth
297
- const dstHeight = newHeight > m.height ? m.height : newHeight
295
+ const { dstWidth, dstHeight } = calculateImageScale(m.width, m.height, maxWidth)
298
296
  if (f.total < 1000 || f.index > f.total - 1000) {
299
297
  log.show(logTag, `${f.index}/${f.total}`,
300
298
  helper.pathShort(fileSrc),
@@ -358,4 +356,18 @@ async function purgeSrcFiles(results) {
358
356
  const deleted = await pMap(toDelete, deletecFunc, { concurrency: cpus().length * 8 })
359
357
  log.showCyan(logTag, `${deleted.filter(Boolean).length} files are safely removed`)
360
358
 
359
+ }
360
+
361
+ // 给定图片长宽,给定长边数值,计算缩放后的长宽,只缩小不放大
362
+ function calculateImageScale(imgWidth, imgHeight, maxSide) {
363
+ // 不需要缩放的情况
364
+ if (imgWidth <= maxSide && imgHeight <= maxSide) {
365
+ return { dstWidth: imgWidth, dstHeight: imgHeight }
366
+ }
367
+ // 计算缩放比例
368
+ let scaleFactor = maxSide / Math.max(imgWidth, imgHeight)
369
+ // 计算新的长宽
370
+ let dstWidth = Math.round(imgWidth * scaleFactor)
371
+ let dstHeight = Math.round(imgHeight * scaleFactor)
372
+ return { dstWidth, dstHeight }
361
373
  }