|
|
@@ -649,4 +649,101 @@ export async function SaveMPSSchool(ctx) {
|
|
649
|
649
|
|
|
650
|
650
|
function computerHeight(title){
|
|
651
|
651
|
return Math.round(((title.length/5)+1)*33+60);
|
|
652
|
|
-}
|
|
|
652
|
+}
|
|
|
653
|
+
|
|
|
654
|
+//以下代码用于《秒过分数线》生成缩略图
|
|
|
655
|
+
|
|
|
656
|
+import path from 'path';
|
|
|
657
|
+import gm from 'gm';
|
|
|
658
|
+
|
|
|
659
|
+const imageMagick = gm.subClass({ imageMagick: false, appPath: '/opt/homebrew/bin/' });
|
|
|
660
|
+
|
|
|
661
|
+/**
|
|
|
662
|
+ * 获取图片尺寸
|
|
|
663
|
+ */
|
|
|
664
|
+function getImageSize(inputPath) {
|
|
|
665
|
+ return new Promise((resolve, reject) => {
|
|
|
666
|
+ imageMagick(inputPath).size((err, size) => {
|
|
|
667
|
+ if (err) reject(err);
|
|
|
668
|
+ else resolve(size);
|
|
|
669
|
+ });
|
|
|
670
|
+ });
|
|
|
671
|
+}
|
|
|
672
|
+
|
|
|
673
|
+/**
|
|
|
674
|
+ * 裁切并缩放图片
|
|
|
675
|
+ */
|
|
|
676
|
+function cropAndResize(inputPath, outputPath, width, height, x, y, newHeight) {
|
|
|
677
|
+ return new Promise((resolve, reject) => {
|
|
|
678
|
+ imageMagick(inputPath)
|
|
|
679
|
+ .crop(width, height, x, y)
|
|
|
680
|
+ .resize(null, newHeight)
|
|
|
681
|
+ .write(outputPath, (err) => {
|
|
|
682
|
+ if (err) reject(err);
|
|
|
683
|
+ else resolve();
|
|
|
684
|
+ });
|
|
|
685
|
+ });
|
|
|
686
|
+}
|
|
|
687
|
+
|
|
|
688
|
+/**
|
|
|
689
|
+ * 对指定路径的图片生成小图片用于小程序
|
|
|
690
|
+ * @param {Object} options 配置项
|
|
|
691
|
+ * @param {string} options.sourceDir 源图片目录
|
|
|
692
|
+ * @param {string} options.targetDir 目标图片目录
|
|
|
693
|
+ * @param {string} options.idMin 最小图片ID(如 '04')
|
|
|
694
|
+ * @param {string} options.idMax 最大图片ID(如 '12')
|
|
|
695
|
+ * @param {number} options.targetHeight 目标高度(默认210)
|
|
|
696
|
+ */
|
|
|
697
|
+export async function CroppingImage(options = {}) {
|
|
|
698
|
+ const {
|
|
|
699
|
+ sourceDir = '/Users/chengjie/Downloads/source/',
|
|
|
700
|
+ targetDir = '/Users/chengjie/Downloads/target/',
|
|
|
701
|
+ idMin = '04',
|
|
|
702
|
+ idMax = '12',
|
|
|
703
|
+ targetHeight = 210,
|
|
|
704
|
+ } = options;
|
|
|
705
|
+
|
|
|
706
|
+ try {
|
|
|
707
|
+ const files = await fs.readdir(sourceDir);
|
|
|
708
|
+
|
|
|
709
|
+ for (const file of files) {
|
|
|
710
|
+ const inputImagePath = path.join(sourceDir, file);
|
|
|
711
|
+ const imageID = file.substr(file.length - 7, 2);
|
|
|
712
|
+
|
|
|
713
|
+ if (imageID < idMin || imageID > idMax) continue;
|
|
|
714
|
+
|
|
|
715
|
+ const stats = await fs.stat(inputImagePath);
|
|
|
716
|
+ if (!stats.isFile()) continue;
|
|
|
717
|
+
|
|
|
718
|
+ // 生成输出路径:原文件名_s.jpeg
|
|
|
719
|
+ const ext = path.extname(file);
|
|
|
720
|
+ const baseName = path.basename(file, ext);
|
|
|
721
|
+ const outputImagePath = path.join(targetDir, baseName + '_s.jpeg');
|
|
|
722
|
+
|
|
|
723
|
+ try {
|
|
|
724
|
+ const { width: originalWidth, height: originalHeight } = await getImageSize(inputImagePath);
|
|
|
725
|
+ const newWidth = originalHeight;
|
|
|
726
|
+ const x = Math.round((originalWidth - newWidth) / 2);
|
|
|
727
|
+
|
|
|
728
|
+ await cropAndResize(inputImagePath, outputImagePath, newWidth, originalHeight, x, 0, targetHeight);
|
|
|
729
|
+ console.log(`生成缩略图成功: ${file}`);
|
|
|
730
|
+ } catch (err) {
|
|
|
731
|
+ console.error(`处理图片失败 ${file}:`, err.message);
|
|
|
732
|
+ }
|
|
|
733
|
+ }
|
|
|
734
|
+
|
|
|
735
|
+ console.log('所有图片处理完成');
|
|
|
736
|
+ } catch (err) {
|
|
|
737
|
+ console.error('生成缩略图失败:', err.message);
|
|
|
738
|
+ }
|
|
|
739
|
+}
|
|
|
740
|
+
|
|
|
741
|
+
|
|
|
742
|
+//// 生成学校缩略图
|
|
|
743
|
+// await CroppingImage({
|
|
|
744
|
+// sourceDir: '/Users/chengjie/Downloads/source/',
|
|
|
745
|
+// targetDir: '/Users/chengjie/Downloads/target/',
|
|
|
746
|
+// idMin: '04',
|
|
|
747
|
+// idMax: '12',
|
|
|
748
|
+// targetHeight: 210,
|
|
|
749
|
+// });
|