api.ts 800 B

12345678910111213141516171819202122232425262728293031323334
  1. import gm from 'gm';
  2. import path from 'node:path';
  3. import { getImagicPath, getTempPath } from './utils';
  4. // macos install gm imagemagick https://github.com/aheckmann/gm/blob/master/README.md
  5. const gmInst =
  6. process.platform === 'win32'
  7. ? gm.subClass({
  8. appPath: getImagicPath(),
  9. })
  10. : gm.subClass({ imageMagick: '7+' });
  11. function cropImage(imgPath: string): Promise<string> {
  12. return new Promise((resolve, reject) => {
  13. const outpath = path.join(getTempPath(), '001.png');
  14. gmInst(imgPath)
  15. .crop(500, 200, 0, 0)
  16. .write(outpath, (err) => {
  17. if (!err) {
  18. return resolve(outpath);
  19. }
  20. return reject(err);
  21. });
  22. });
  23. }
  24. const commonApi = {
  25. cropImage,
  26. };
  27. export type CommonApi = typeof commonApi;
  28. export default commonApi;