MCPcopy
hub / github.com/mobile-next/mobile-mcp / toBufferWithSips

Method toBufferWithSips

src/image-utils.ts:66–102  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

64 }
65
66 private toBufferWithSips(): Buffer {
67 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "image-"));
68 const inputFile = path.join(tempDir, "input");
69 const outputFile = path.join(tempDir, `output.${this.newFormat === "jpg" ? "jpg" : "png"}`);
70
71 try {
72 fs.writeFileSync(inputFile, this.buffer);
73
74 const args = ["-s", "format", this.newFormat === "jpg" ? "jpeg" : "png"];
75 if (this.newFormat === "jpg") {
76 args.push("-s", "formatOptions", this.qualityToSips(this.jpegOptions.quality));
77 }
78
79 args.push("-Z", `${this.newWidth}`);
80 args.push("--out", outputFile);
81 args.push(inputFile);
82
83 trace(`Running sips command: /usr/bin/sips ${args.join(" ")}`);
84 const proc = spawnSync("/usr/bin/sips", args, {
85 maxBuffer: 8 * 1024 * 1024
86 });
87
88 if (proc.status !== 0) {
89 throw new Error(`Sips failed with status ${proc.status}`);
90 }
91
92 const outputBuffer = fs.readFileSync(outputFile);
93 trace("Sips returned buffer of size: " + outputBuffer.length);
94 return outputBuffer;
95 } finally {
96 try {
97 fs.rmSync(tempDir, { recursive: true, force: true });
98 } catch (error) {
99 // Ignore cleanup errors
100 }
101 }
102 }
103
104 private toBufferWithImageMagick(): Buffer {
105 const magickArgs = ["-", "-resize", `${this.newWidth}x`, "-quality", `${this.jpegOptions.quality}`, `${this.newFormat}:-`];

Callers 1

toBufferMethod · 0.95

Calls 2

qualityToSipsMethod · 0.95
traceFunction · 0.90

Tested by

no test coverage detected