()
| 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}:-`]; |
no test coverage detected