|
@@ -5,6 +5,11 @@ import mkdirp from "mkdirp";
|
|
import { Store, Student } from "@/types";
|
|
import { Store, Student } from "@/types";
|
|
import gmType from "gm";
|
|
import gmType from "gm";
|
|
|
|
|
|
|
|
+const arrayBufferToBase64Img = (buffer: ArrayBuffer) => {
|
|
|
|
+ const str = String.fromCharCode(...new Uint8Array(buffer));
|
|
|
|
+ return `data:image/jpg;base64,${window.btoa(str)}`;
|
|
|
|
+};
|
|
|
|
+
|
|
let gm = null as unknown as typeof gmType;
|
|
let gm = null as unknown as typeof gmType;
|
|
|
|
|
|
export async function addWatermark(
|
|
export async function addWatermark(
|
|
@@ -18,14 +23,15 @@ export async function addWatermark(
|
|
trackMode: string,
|
|
trackMode: string,
|
|
x = 0.01,
|
|
x = 0.01,
|
|
y = 0.03,
|
|
y = 0.03,
|
|
- colorMap: any = {}
|
|
|
|
|
|
+ colorMap: any = {},
|
|
|
|
+ onlyUsePdf = false
|
|
): Promise<boolean | string> {
|
|
): Promise<boolean | string> {
|
|
const file = path.join(...filePath);
|
|
const file = path.join(...filePath);
|
|
if (
|
|
if (
|
|
index !== 1 &&
|
|
index !== 1 &&
|
|
(student.tags == undefined || student.tags[index] == undefined)
|
|
(student.tags == undefined || student.tags[index] == undefined)
|
|
) {
|
|
) {
|
|
- return await saveImage(store, imageData, filePath);
|
|
|
|
|
|
+ return await saveImage(store, imageData, filePath, onlyUsePdf);
|
|
}
|
|
}
|
|
|
|
|
|
if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
|
|
if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
|
|
@@ -48,6 +54,7 @@ export async function addWatermark(
|
|
});
|
|
});
|
|
// console.log(path.join(__dirname, "../../imagemagick/"));
|
|
// console.log(path.join(__dirname, "../../imagemagick/"));
|
|
// console.log(path.join(__dirname, store.config.imagemagickDev));
|
|
// console.log(path.join(__dirname, store.config.imagemagickDev));
|
|
|
|
+ require("gm-base64");
|
|
}
|
|
}
|
|
const fontFile = store.config.watermark.fontFile;
|
|
const fontFile = store.config.watermark.fontFile;
|
|
const color = store.config.watermark.color;
|
|
const color = store.config.watermark.color;
|
|
@@ -258,25 +265,36 @@ export async function addWatermark(
|
|
}
|
|
}
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- mkdirp.sync(path.dirname(file));
|
|
|
|
- imgData.write(file, (error) => {
|
|
|
|
- if (error) {
|
|
|
|
- // logger.error("add watermark error: " + file);
|
|
|
|
- // logger.error(error);
|
|
|
|
- reject(error);
|
|
|
|
- } else {
|
|
|
|
- // resolve(true);
|
|
|
|
- resolve(file);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ if (onlyUsePdf) {
|
|
|
|
+ imgData.toBase64("jpg", true, function (err: any, base64: any) {
|
|
|
|
+ if (err) {
|
|
|
|
+ reject(err);
|
|
|
|
+ } else {
|
|
|
|
+ resolve(base64);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ mkdirp.sync(path.dirname(file));
|
|
|
|
+ imgData.write(file, (error) => {
|
|
|
|
+ if (error) {
|
|
|
|
+ // logger.error("add watermark error: " + file);
|
|
|
|
+ // logger.error(error);
|
|
|
|
+ reject(error);
|
|
|
|
+ } else {
|
|
|
|
+ // resolve(true);
|
|
|
|
+ resolve(file);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
export async function saveImage(
|
|
export async function saveImage(
|
|
store: Store,
|
|
store: Store,
|
|
imageData: ArrayBuffer,
|
|
imageData: ArrayBuffer,
|
|
- filePath: string[]
|
|
|
|
-): Promise<boolean> {
|
|
|
|
|
|
+ filePath: string[],
|
|
|
|
+ onlyUsePdf = false
|
|
|
|
+): Promise<any> {
|
|
const file = path.join(...filePath);
|
|
const file = path.join(...filePath);
|
|
// console.log("saveImage file:", file);
|
|
// console.log("saveImage file:", file);
|
|
if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
|
|
if (store.pageInputs["/image-download"].append && fs.existsSync(file)) {
|
|
@@ -285,16 +303,23 @@ export async function saveImage(
|
|
}
|
|
}
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
- const image = Buffer.from(imageData);
|
|
|
|
- mkdirp.sync(path.dirname(file));
|
|
|
|
- fs.writeFile(file, image, (error) => {
|
|
|
|
- if (error) {
|
|
|
|
- reject(error);
|
|
|
|
- } else {
|
|
|
|
- // resolve(true);
|
|
|
|
- resolve(file);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ if (onlyUsePdf) {
|
|
|
|
+ // const buffer = Buffer.from(imageData, "base64");
|
|
|
|
+ // const base64Str = "data:image/jpeg;base64," + buffer.toString("base64");
|
|
|
|
+ const base64Str = arrayBufferToBase64Img(imageData);
|
|
|
|
+ resolve(base64Str);
|
|
|
|
+ } else {
|
|
|
|
+ const image = Buffer.from(imageData);
|
|
|
|
+ mkdirp.sync(path.dirname(file));
|
|
|
|
+ fs.writeFile(file, image, (error) => {
|
|
|
|
+ if (error) {
|
|
|
|
+ reject(error);
|
|
|
|
+ } else {
|
|
|
|
+ // resolve(true);
|
|
|
|
+ resolve(file);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|