@soga/imgutils 1.3.24 → 1.3.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.d.mts CHANGED
@@ -17,17 +17,35 @@ declare class Compress {
17
17
  ffmpeg_path: string;
18
18
  });
19
19
  protected ffmpeg(args: string[], options?: SpawnOptions): Promise<unknown>;
20
- start({ input_path, output_path, }: {
20
+ start({ input_path, output_root, output_basename, }: {
21
21
  input_path: string;
22
+ output_root: string;
23
+ output_basename: string;
24
+ }): Promise<{
25
+ state: boolean;
26
+ width: number;
27
+ height: number;
28
+ output_format: string;
29
+ output_filename: string;
22
30
  output_path: string;
23
- }): Promise<void>;
31
+ }>;
24
32
  private generateFile;
33
+ private generateByFfmpeg;
34
+ private generateBySharp;
25
35
  }
26
- declare const compress: ({ input_path, output_path, ffmpeg_path, }: {
36
+ declare const compress: ({ input_path, output_basename, output_root, ffmpeg_path, }: {
27
37
  input_path: string;
28
- output_path: string;
38
+ output_basename: string;
39
+ output_root: string;
29
40
  ffmpeg_path: string;
30
- }) => Promise<void>;
41
+ }) => Promise<{
42
+ state: boolean;
43
+ width: number;
44
+ height: number;
45
+ output_format: string;
46
+ output_filename: string;
47
+ output_path: string;
48
+ }>;
31
49
 
32
50
  declare class Combine {
33
51
  start({ inputs, output_path, col, width, height, }: {
package/dist/main.d.ts CHANGED
@@ -17,17 +17,35 @@ declare class Compress {
17
17
  ffmpeg_path: string;
18
18
  });
19
19
  protected ffmpeg(args: string[], options?: SpawnOptions): Promise<unknown>;
20
- start({ input_path, output_path, }: {
20
+ start({ input_path, output_root, output_basename, }: {
21
21
  input_path: string;
22
+ output_root: string;
23
+ output_basename: string;
24
+ }): Promise<{
25
+ state: boolean;
26
+ width: number;
27
+ height: number;
28
+ output_format: string;
29
+ output_filename: string;
22
30
  output_path: string;
23
- }): Promise<void>;
31
+ }>;
24
32
  private generateFile;
33
+ private generateByFfmpeg;
34
+ private generateBySharp;
25
35
  }
26
- declare const compress: ({ input_path, output_path, ffmpeg_path, }: {
36
+ declare const compress: ({ input_path, output_basename, output_root, ffmpeg_path, }: {
27
37
  input_path: string;
28
- output_path: string;
38
+ output_basename: string;
39
+ output_root: string;
29
40
  ffmpeg_path: string;
30
- }) => Promise<void>;
41
+ }) => Promise<{
42
+ state: boolean;
43
+ width: number;
44
+ height: number;
45
+ output_format: string;
46
+ output_filename: string;
47
+ output_path: string;
48
+ }>;
31
49
 
32
50
  declare class Combine {
33
51
  start({ inputs, output_path, col, width, height, }: {
package/dist/main.js CHANGED
@@ -1 +1 @@
1
- var t,e=Object.create,a=Object.defineProperty,i=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,r=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,n=(t,e,r,n)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let p of s(e))o.call(t,p)||p===r||a(t,p,{get:()=>e[p],enumerable:!(n=i(e,p))||n.enumerable});return t},p={};((t,e)=>{for(var i in e)a(t,i,{get:e[i],enumerable:!0})})(p,{Combine:()=>b,Compress:()=>_,combine:()=>j,compress:()=>w,getMetadata:()=>h}),module.exports=(t=p,n(a({},"__esModule",{value:!0}),t));var u=require("image-size/fromFile");async function h(t){return await(0,u.imageSizeFromFile)(t)}var c=require("fs-extra"),f=require("path"),l=require("uuid"),m=((t,i,s)=>(s=null!=t?e(r(t)):{},n(!i&&t&&t.__esModule?s:a(s,"default",{value:t,enumerable:!0}),t)))(require("heic-convert")),g=require("child_process"),_=class{ffmpeg_path;output_root;meta;is_heic=!1;input_path="";output_filename="";constructor({ffmpeg_path:t}){this.ffmpeg_path=t}async ffmpeg(t,e){const a=this.ffmpeg_path;return await new Promise((i,s)=>{const r=(0,g.spawn)(a,t,Object.assign({stdio:"ignore"},e));r.on("close",t=>{i(t)}),r.on("error",t=>{s(t)})})}async start({input_path:t,output_path:e}){this.meta=await h(t);const{type:a}=this.meta,i=(0,f.parse)(e),s=i.base,r=i.dir;if(this.output_root=r,this.output_filename=s,"svg"==a){const e=(0,f.join)(r,s);await(0,c.copy)(t,e)}else if("heic"==a){this.is_heic=!0;const e=(0,l.v4)()+".png",a=(0,f.join)(r,e);this.input_path=a;const i=await(0,c.readFile)(t),s=await(0,m.default)({buffer:i,format:"PNG",quality:1});await(0,c.writeFile)(a,s)}else this.input_path=t;await this.generateFile()}async generateFile(){const t=this.input_path;if(!t)return;const e=["-i",t],{width:a,height:i}=this.meta;if(a/i>1){const t=Math.min(1080,2*Math.ceil(i/2));e.push("-vf",`scale=-2:${t}:flags=lanczos`)}else{const t=Math.min(1920,2*Math.ceil(a/2));e.push("-vf",`scale=${t}:-2:flags=lanczos`)}e.push("-y",this.output_filename),await this.ffmpeg(e,{cwd:this.output_root}),this.is_heic&&await(0,c.remove)(this.input_path)}},w=async({input_path:t,output_path:e,ffmpeg_path:a})=>{const i=new _({ffmpeg_path:a});await i.start({input_path:t,output_path:e})},y=require("fs-extra"),d=require("jimp"),b=class{async start({inputs:t,output_path:e,col:a,width:i,height:s}){const r=i*a,o=s*Math.ceil(t.length/a),n=new d.Jimp({width:r,height:o});for(let e=0;e<t.length;e++){const r=e%a*i,o=Math.floor(e/a)*s,p=await d.Jimp.read(t[e]);n.composite(p,r,o)}const p=await n.getBuffer("image/jpeg",{quality:70});await(0,y.writeFile)(e,p)}},j=async t=>{const e=new b;await e.start(t)};
1
+ var t,e=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,u=Object.getPrototypeOf,r=Object.prototype.hasOwnProperty,p=(t,e,u,p)=>{if(e&&"object"==typeof e||"function"==typeof e)for(let n of o(e))r.call(t,n)||n===u||i(t,n,{get:()=>e[n],enumerable:!(p=a(e,n))||p.enumerable});return t},n=(t,a,o)=>(o=null!=t?e(u(t)):{},p(!a&&t&&t.__esModule?o:i(o,"default",{value:t,enumerable:!0}),t)),s={};((t,e)=>{for(var a in e)i(t,a,{get:e[a],enumerable:!0})})(s,{Combine:()=>F,Compress:()=>j,combine:()=>M,compress:()=>b,getMetadata:()=>l}),module.exports=(t=s,p(i({},"__esModule",{value:!0}),t));var h=require("image-size/fromFile"),f=n(require("sharp"));async function l(t){try{const e=await(0,f.default)(t).metadata();return{width:e.width||0,height:e.height||0,type:e.format||"unknown"}}catch(e){try{return await(0,h.imageSizeFromFile)(t)}catch(t){}}}var c=require("fs-extra"),_=require("path"),m=n(require("heic-convert")),w=require("child_process"),g=n(require("sharp")),y=require("uuid"),d=require("@soga/fileutils"),j=class{ffmpeg_path;output_root;meta;is_heic=!1;input_path="";output_filename="";constructor({ffmpeg_path:t}){this.ffmpeg_path=t}async ffmpeg(t,e){const i=this.ffmpeg_path;return await new Promise((a,o)=>{const u=(0,w.spawn)(i,t,Object.assign({stdio:"ignore"},e));u.on("close",t=>{a(t)}),u.on("error",t=>{o(t)})})}async start({input_path:t,output_root:e,output_basename:i}){const a=async()=>{const e={state:!1,width:0,height:0,output_format:"unknown",output_filename:"",output_path:""},a=t.split(".").pop()??"jpg";return e.output_filename=`${i}.${a}`,e.output_path=(0,_.join)(this.output_root,e.output_filename),e.output_format=a,await(0,c.copy)(t,e.output_path),await(0,c.exists)(e.output_path)&&(e.state=!0),e};if(this.output_root=e,this.meta=await l(t),!this.meta)return await a();const{type:o,width:u,height:r}=this.meta,p="jpg",n={state:!1,width:u,height:r,output_format:p,output_filename:`${i}.${p}`,output_path:(0,_.join)(this.output_root,`${i}.${p}`)};if("svg"==o)n.output_format="svg",n.output_filename=`${i}.svg`,n.output_path=(0,_.join)(this.output_root,n.output_filename),await(0,c.copy)(t,n.output_path),await(0,c.exists)(n.output_path)?n.state=!0:n.state=!1;else if("heic"==o||"heif"==o){const e=`${(0,y.v4)()}.png`,i=(0,_.join)(this.output_root,e),a=await(0,c.readFile)(t),o=await(0,m.default)({buffer:a,format:"PNG",quality:1});await(0,c.writeFile)(i,o);if(await(0,c.exists)(i)){const t=await this.generateFile({input_path:i,output_root:this.output_root,output_filename:n.output_filename});try{await(0,c.remove)(i)}catch(t){}n.state=t}else n.state=!1}else n.state=await this.generateFile({input_path:t,output_root:this.output_root,output_filename:n.output_filename});return n.state?n:await a()}async generateFile({input_path:t,output_root:e,output_filename:i}){const{width:a,height:o}=this.meta,u=a/o,r={width:a,height:o};if(u>1){const t=Math.min(1080,2*Math.ceil(o/2)),e=2*Math.ceil(t*u/2);r.width=e,r.height=t}else{const t=Math.min(1920,2*Math.ceil(a/2)),e=2*Math.ceil(t/u/2);r.width=t,r.height=e}return!!await this.generateBySharp({input:t,output_filename:i,output_root:e,width:r.width,height:r.height})||await this.generateByFfmpeg({input:t,output_filename:i,output_root:e,width:r.width,height:r.height})}async generateByFfmpeg({input:t,output_root:e,output_filename:i,width:a,height:o}){try{const e=["-i",t];e.push("-vf",`scale=${a}:${o}:flags=lanczos`),e.push("-y",this.output_filename),await this.ffmpeg(e,{cwd:this.output_root}),this.is_heic&&await(0,c.remove)(this.input_path)}finally{const t=(0,_.join)(e,i);return await(0,c.exists)(t)}}async generateBySharp({input:t,output_filename:e,output_root:i,width:a,height:o}){const u=(0,_.join)(i,e);try{await(0,g.default)(t).resize({width:a,height:o,fit:"inside"}).toFile(u);if(await(0,d.getFileSize)(u)>1048576){const t=(0,_.join)(i,`temp_${e}`);await(0,c.copy)(u,t),await(0,g.default)(t).jpeg({quality:70}).toFile(u)}}finally{return await(0,c.exists)(u)}}},b=async({input_path:t,output_basename:e,output_root:i,ffmpeg_path:a})=>{const o=new j({ffmpeg_path:a});return await o.start({input_path:t,output_root:i,output_basename:e})},q=require("fs-extra"),v=require("jimp"),F=class{async start({inputs:t,output_path:e,col:i,width:a,height:o}){const u=a*i,r=o*Math.ceil(t.length/i),p=new v.Jimp({width:u,height:r});for(let e=0;e<t.length;e++){const u=e%i*a,r=Math.floor(e/i)*o,n=await v.Jimp.read(t[e]);p.composite(n,u,r)}const n=await p.getBuffer("image/jpeg",{quality:70});await(0,q.writeFile)(e,n)}},M=async t=>{const e=new F;await e.start(t)};
package/dist/main.mjs CHANGED
@@ -1 +1 @@
1
- import{imageSizeFromFile as t}from"image-size/fromFile";async function a(a){return await t(a)}import{copy as i,readFile as e,remove as s,writeFile as o}from"fs-extra";import{join as p,parse as h}from"path";import{v4 as n}from"uuid";import r from"heic-convert";import{spawn as c}from"child_process";var f=class{ffmpeg_path;output_root;meta;is_heic=!1;input_path="";output_filename="";constructor({ffmpeg_path:t}){this.ffmpeg_path=t}async ffmpeg(t,a){const i=this.ffmpeg_path;return await new Promise((e,s)=>{const o=c(i,t,Object.assign({stdio:"ignore"},a));o.on("close",t=>{e(t)}),o.on("error",t=>{s(t)})})}async start({input_path:t,output_path:s}){this.meta=await a(t);const{type:c}=this.meta,f=h(s),u=f.base,m=f.dir;if(this.output_root=m,this.output_filename=u,"svg"==c){const a=p(m,u);await i(t,a)}else if("heic"==c){this.is_heic=!0;const a=n()+".png",i=p(m,a);this.input_path=i;const s=await e(t),h=await r({buffer:s,format:"PNG",quality:1});await o(i,h)}else this.input_path=t;await this.generateFile()}async generateFile(){const t=this.input_path;if(!t)return;const a=["-i",t],{width:i,height:e}=this.meta;if(i/e>1){const t=Math.min(1080,2*Math.ceil(e/2));a.push("-vf",`scale=-2:${t}:flags=lanczos`)}else{const t=Math.min(1920,2*Math.ceil(i/2));a.push("-vf",`scale=${t}:-2:flags=lanczos`)}a.push("-y",this.output_filename),await this.ffmpeg(a,{cwd:this.output_root}),this.is_heic&&await s(this.input_path)}},u=async({input_path:t,output_path:a,ffmpeg_path:i})=>{const e=new f({ffmpeg_path:i});await e.start({input_path:t,output_path:a})};import{writeFile as m}from"fs-extra";import{Jimp as l}from"jimp";var _=class{async start({inputs:t,output_path:a,col:i,width:e,height:s}){const o=Math.ceil(t.length/i),p=new l({width:e*i,height:s*o});for(let a=0;a<t.length;a++){const o=a%i*e,h=Math.floor(a/i)*s,n=await l.read(t[a]);p.composite(n,o,h)}const h=await p.getBuffer("image/jpeg",{quality:70});await m(a,h)}},g=async t=>{const a=new _;await a.start(t)};export{_ as Combine,f as Compress,g as combine,u as compress,a as getMetadata};
1
+ import{imageSizeFromFile as t}from"image-size/fromFile";import a from"sharp";async function i(i){try{const t=await a(i).metadata();return{width:t.width||0,height:t.height||0,type:t.format||"unknown"}}catch(a){try{return await t(i)}catch(t){}}}import{copy as e,exists as o,readFile as u,remove as p,writeFile as h}from"fs-extra";import{join as r}from"path";import n from"heic-convert";import{spawn as s}from"child_process";import f from"sharp";import{v4 as m}from"uuid";import{getFileSize as _}from"@soga/fileutils";var c=class{ffmpeg_path;output_root;meta;is_heic=!1;input_path="";output_filename="";constructor({ffmpeg_path:t}){this.ffmpeg_path=t}async ffmpeg(t,a){const i=this.ffmpeg_path;return await new Promise((e,o)=>{const u=s(i,t,Object.assign({stdio:"ignore"},a));u.on("close",t=>{e(t)}),u.on("error",t=>{o(t)})})}async start({input_path:t,output_root:a,output_basename:s}){const f=async()=>{const a={state:!1,width:0,height:0,output_format:"unknown",output_filename:"",output_path:""},i=t.split(".").pop()??"jpg";return a.output_filename=`${s}.${i}`,a.output_path=r(this.output_root,a.output_filename),a.output_format=i,await e(t,a.output_path),await o(a.output_path)&&(a.state=!0),a};if(this.output_root=a,this.meta=await i(t),!this.meta)return await f();const{type:_,width:c,height:w}=this.meta,l="jpg",g={state:!1,width:c,height:w,output_format:l,output_filename:`${s}.${l}`,output_path:r(this.output_root,`${s}.${l}`)};if("svg"==_)g.output_format="svg",g.output_filename=`${s}.svg`,g.output_path=r(this.output_root,g.output_filename),await e(t,g.output_path),await o(g.output_path)?g.state=!0:g.state=!1;else if("heic"==_||"heif"==_){const a=`${m()}.png`,i=r(this.output_root,a),e=await u(t),s=await n({buffer:e,format:"PNG",quality:1});await h(i,s);if(await o(i)){const t=await this.generateFile({input_path:i,output_root:this.output_root,output_filename:g.output_filename});try{await p(i)}catch(t){}g.state=t}else g.state=!1}else g.state=await this.generateFile({input_path:t,output_root:this.output_root,output_filename:g.output_filename});return g.state?g:await f()}async generateFile({input_path:t,output_root:a,output_filename:i}){const{width:e,height:o}=this.meta,u=e/o,p={width:e,height:o};if(u>1){const t=Math.min(1080,2*Math.ceil(o/2)),a=2*Math.ceil(t*u/2);p.width=a,p.height=t}else{const t=Math.min(1920,2*Math.ceil(e/2)),a=2*Math.ceil(t/u/2);p.width=t,p.height=a}return!!await this.generateBySharp({input:t,output_filename:i,output_root:a,width:p.width,height:p.height})||await this.generateByFfmpeg({input:t,output_filename:i,output_root:a,width:p.width,height:p.height})}async generateByFfmpeg({input:t,output_root:a,output_filename:i,width:e,height:u}){try{const a=["-i",t];a.push("-vf",`scale=${e}:${u}:flags=lanczos`),a.push("-y",this.output_filename),await this.ffmpeg(a,{cwd:this.output_root}),this.is_heic&&await p(this.input_path)}finally{const t=r(a,i);return await o(t)}}async generateBySharp({input:t,output_filename:a,output_root:i,width:u,height:p}){const h=r(i,a);try{await f(t).resize({width:u,height:p,fit:"inside"}).toFile(h);if(await _(h)>1048576){const t=r(i,`temp_${a}`);await e(h,t),await f(t).jpeg({quality:70}).toFile(h)}}finally{return await o(h)}}},w=async({input_path:t,output_basename:a,output_root:i,ffmpeg_path:e})=>{const o=new c({ffmpeg_path:e});return await o.start({input_path:t,output_root:i,output_basename:a})};import{writeFile as l}from"fs-extra";import{Jimp as g}from"jimp";var y=class{async start({inputs:t,output_path:a,col:i,width:e,height:o}){const u=Math.ceil(t.length/i),p=new g({width:e*i,height:o*u});for(let a=0;a<t.length;a++){const u=a%i*e,h=Math.floor(a/i)*o,r=await g.read(t[a]);p.composite(r,u,h)}const h=await p.getBuffer("image/jpeg",{quality:70});await l(a,h)}},d=async t=>{const a=new y;await a.start(t)};export{y as Combine,c as Compress,d as combine,w as compress,i as getMetadata};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/imgutils",
3
- "version": "1.3.24",
3
+ "version": "1.3.26",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -18,7 +18,7 @@
18
18
  "tsx": "^4.20.6",
19
19
  "@types/heic-convert": "^2.1.0",
20
20
  "terser": "^5.43.1",
21
- "@soga/typescript-config": "1.3.24"
21
+ "@soga/typescript-config": "1.3.26"
22
22
  },
23
23
  "keywords": [],
24
24
  "author": "",
@@ -29,7 +29,8 @@
29
29
  "uuid": "^11.1.0",
30
30
  "heic-convert": "^2.1.0",
31
31
  "image-size": "^2.0.2",
32
- "@soga/fileutils": "1.3.24"
32
+ "sharp": "^0.34.5",
33
+ "@soga/fileutils": "1.3.26"
33
34
  },
34
35
  "scripts": {
35
36
  "demo": "tsx ./demo/demo",