api.ts 827 B

1234567891011121314151617181920212223242526272829303132333435
  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. imageMagick: '7+',
  9. appPath: getImagicPath(),
  10. })
  11. : gm.subClass({ imageMagick: '7+' });
  12. function cropImage(imgPath: string): Promise<string> {
  13. return new Promise((resolve, reject) => {
  14. const outpath = path.join(getTempPath(), '001.png');
  15. gmInst(imgPath)
  16. .crop(500, 200, 0, 0)
  17. .write(outpath, (err) => {
  18. if (!err) {
  19. return resolve(outpath);
  20. }
  21. return reject(err);
  22. });
  23. });
  24. }
  25. const commonApi = {
  26. cropImage,
  27. };
  28. export type CommonApi = typeof commonApi;
  29. export default commonApi;