123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import gm from 'gm';
- import axios from 'axios';
- import sizeOf from 'image-size';
- import path from 'node:path';
- import fs from 'node:fs';
- import { getImagicPath, getTempPath } from './utils';
- import {
- DrawTrackItem,
- DrawTrackCircleOption,
- DrawTrackLineOption,
- DrawTrackTextOption,
- } from './types';
- // macos install gm imagemagick https://github.com/aheckmann/gm/blob/master/README.md
- const gmInst =
- process.platform === 'win32'
- ? gm.subClass({
- imageMagick: '7+',
- appPath: getImagicPath(),
- })
- : gm.subClass({ imageMagick: '7+' });
- function cropImage(imgPath: string): Promise<string> {
- return new Promise((resolve, reject) => {
- const outpath = path.join(getTempPath(), '001.png');
- gmInst(imgPath)
- .crop(500, 200, 0, 0)
- .write(outpath, (err) => {
- if (!err) {
- return resolve(outpath);
- }
- return reject(err);
- });
- });
- }
- function drawTrack(
- imgPath: string,
- drawTrackList: DrawTrackItem[],
- outpath: string
- ): Promise<string> {
- return new Promise((resolve, reject) => {
- const gmObj = gmInst(imgPath);
- const defaultColor = '#f53f3f';
- const defaultFontSize = 14;
- drawTrackList.forEach((track) => {
- // text
- if (track.type === 'text') {
- const { x, y, text, color, fontSize } =
- track.option as DrawTrackTextOption;
- gmObj
- .fill(color || defaultColor)
- .fontSize(fontSize || defaultFontSize)
- .drawText(x, y, text);
- return;
- }
- // circle
- if (track.type === 'circle') {
- const { x0, y0, x1, y1 } = track.option as DrawTrackCircleOption;
- gmObj.drawCircle(x0, y0, x1, y1).stroke(defaultColor, 2);
- return;
- }
- // line
- if (track.type === 'line') {
- const { x0, y0, x1, y1 } = track.option as DrawTrackLineOption;
- gmObj.drawLine(x0, y0, x1, y1).stroke(defaultColor, 2);
- }
- });
- gmObj.write(outpath, (err) => {
- if (!err) {
- return resolve(outpath);
- }
- return reject(err);
- });
- });
- }
- async function downloadFile(url: string, outputPath: string) {
- const writer = fs.createWriteStream(outputPath);
- const response = await axios({
- url,
- method: 'GET',
- responseType: 'stream',
- });
- response.data.pipe(writer);
- return new Promise((resolve, reject) => {
- writer.on('finish', resolve);
- writer.on('error', reject);
- });
- }
- async function downloadImage(url: string, outputPath: string) {
- await downloadFile(url, outputPath);
- const size = sizeOf(outputPath);
- return {
- url: outputPath,
- width: size.width || 100,
- height: size.height || 100,
- };
- }
- function joinPath(paths: string[]) {
- return path.join(...paths);
- }
- async function combinePdf(urls: string[], outpath: string) {
- // TODO:
- console.log(urls, outpath);
- }
- const commonApi = {
- cropImage,
- drawTrack,
- joinPath,
- downloadImage,
- combinePdf,
- };
- export type CommonApi = typeof commonApi;
- export default commonApi;
|