From aa0292c6a58347b907c2f154f58f6ac0e4d09765 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 05:11:15 +0000 Subject: [PATCH 1/2] Initial plan From c199f0018892ed1575b3f737cdfe111b1edbccef Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 05:18:30 +0000 Subject: [PATCH 2/2] Add optional constructorOptions parameter to Sharp::run() method Co-authored-by: kiatng <1106470+kiatng@users.noreply.github.com> --- README.md | 17 ++++++++++++++++- src/Sharp.php | 9 +++++++-- src/sharp.js | 16 +++++++++++++--- tests/Unit/SharpTest.php | 21 +++++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6c782e5..306891c 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,10 @@ This last command will create a `node_modules` directory, `package-lock.json` an - Sharp npm package ## Usage -There is only one static method `run` in the `Sharp` class. It takes two arguments: +There is only one static method `run` in the `Sharp` class. It takes three arguments: - `$io`: Input and output parameters - `$params`: Parameters for the image processing +- `$constructorOptions` (optional): Sharp constructor options (e.g., `density`, `failOn`, `limitInputPixels`) Refer to the [Sharp documentation](https://sharp.pixelplumbing.com/) on the specifications of the parameters. @@ -77,6 +78,20 @@ Sharp::run( ); ``` +Example with constructor options (e.g., setting DPI for SVG rendering): +```php +Sharp::run( + [ + 'input' => ['is_raw' => false, 'data' => $svgPath], + 'output' => ['is_raw' => false, 'file' => $pngPath], + ], + [ + 'resize' => ['width' => 300, 'height' => 200] + ], + ['density' => 300] // Set DPI to 300 for higher quality SVG rendering +); +``` + Refer to the test cases in `tests/Unit/SharpTest.php` for more examples. ## Contributing diff --git a/src/Sharp.php b/src/Sharp.php index eea76bb..0b38ff2 100644 --- a/src/Sharp.php +++ b/src/Sharp.php @@ -17,10 +17,11 @@ class Sharp /** * @param array $io {input: {is_raw: bool, data: string, ?ext: string}, output: {is_raw: bool, ?file: string}} * @param array $params {toFormat: {format: string, options: object}, {resize: {width: int, height: int, options: object}}} + * @param array $constructorOptions Optional Sharp constructor options (e.g., density, failOn, limitInputPixels) * @return string * @throws Exception */ - public static function run(array $io, array $params) + public static function run(array $io, array $params, array $constructorOptions = []) { try { if ($io['input']['is_raw']) { @@ -38,7 +39,11 @@ public static function run(array $io, array $params) $file = $io['input']['data']; } - $process = new Process(['node', __DIR__ . '/sharp.js', $file, json_encode($params)]); + $args = ['node', __DIR__ . '/sharp.js', $file, json_encode($params)]; + if (!empty($constructorOptions)) { + $args[] = json_encode($constructorOptions); + } + $process = new Process($args); $process->run(); // Cleanup temporary file diff --git a/src/sharp.js b/src/sharp.js index f342f33..ec1022d 100644 --- a/src/sharp.js +++ b/src/sharp.js @@ -16,7 +16,7 @@ const sharp = require('sharp'); throw new Error('Missing required arguments: file and apis'); } - const [file, apisJson] = arguments; + const [file, apisJson, constructorOptionsJson] = arguments; // Validate JSON apis let apis; @@ -26,8 +26,18 @@ const sharp = require('sharp'); throw new Error('Invalid apis JSON format'); } - // Initialize sharp with input file - let image = sharp(file); + // Parse constructor options (optional) + let constructorOptions = {}; + if (constructorOptionsJson) { + try { + constructorOptions = JSON.parse(constructorOptionsJson); + } catch (e) { + throw new Error('Invalid constructorOptions JSON format'); + } + } + + // Initialize sharp with input file and optional constructor options + let image = sharp(file, constructorOptions); // Define supported APIs map, see https://sharp.pixelplumbing.com/ const supportedApis = { diff --git a/tests/Unit/SharpTest.php b/tests/Unit/SharpTest.php index e52fc32..3e7b60a 100644 --- a/tests/Unit/SharpTest.php +++ b/tests/Unit/SharpTest.php @@ -111,4 +111,25 @@ public function testCanRemoveAlphaChannel() $this->assertFileExists($outputPath); } + + public function testCanUseConstructorOptions() + { + $outputPath = $this->testOutputDir . '/with-density.png'; + + $io = [ + 'input' => ['is_raw' => false, 'data' => $this->testSvgFile], + 'output' => ['is_raw' => false, 'file' => $outputPath] + ]; + + $params = [ + 'resize' => ['width' => 200, 'height' => 200] + ]; + + // Use density option to set DPI for SVG rendering + $constructorOptions = ['density' => 300]; + + Sharp::run($io, $params, $constructorOptions); + + $this->assertFileExists($outputPath); + } } \ No newline at end of file