Skip to content

Commit 63386e9

Browse files
authored
Merge pull request #3 from kiatng/copilot/add-optional-run-parameter
Add optional constructorOptions parameter to Sharp::run()
2 parents 4440329 + c199f00 commit 63386e9

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ This last command will create a `node_modules` directory, `package-lock.json` an
3535
- Sharp npm package
3636

3737
## Usage
38-
There is only one static method `run` in the `Sharp` class. It takes two arguments:
38+
There is only one static method `run` in the `Sharp` class. It takes three arguments:
3939
- `$io`: Input and output parameters
4040
- `$params`: Parameters for the image processing
41+
- `$constructorOptions` (optional): Sharp constructor options (e.g., `density`, `failOn`, `limitInputPixels`)
4142

4243
Refer to the [Sharp documentation](https://sharp.pixelplumbing.com/) on the specifications of the parameters.
4344

@@ -77,6 +78,20 @@ Sharp::run(
7778
);
7879
```
7980

81+
Example with constructor options (e.g., setting DPI for SVG rendering):
82+
```php
83+
Sharp::run(
84+
[
85+
'input' => ['is_raw' => false, 'data' => $svgPath],
86+
'output' => ['is_raw' => false, 'file' => $pngPath],
87+
],
88+
[
89+
'resize' => ['width' => 300, 'height' => 200]
90+
],
91+
['density' => 300] // Set DPI to 300 for higher quality SVG rendering
92+
);
93+
```
94+
8095
Refer to the test cases in `tests/Unit/SharpTest.php` for more examples.
8196

8297
## Contributing

src/Sharp.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class Sharp
1717
/**
1818
* @param array $io {input: {is_raw: bool, data: string, ?ext: string}, output: {is_raw: bool, ?file: string}}
1919
* @param array $params {toFormat: {format: string, options: object}, {resize: {width: int, height: int, options: object}}}
20+
* @param array $constructorOptions Optional Sharp constructor options (e.g., density, failOn, limitInputPixels)
2021
* @return string
2122
* @throws Exception
2223
*/
23-
public static function run(array $io, array $params)
24+
public static function run(array $io, array $params, array $constructorOptions = [])
2425
{
2526
try {
2627
if ($io['input']['is_raw']) {
@@ -38,7 +39,11 @@ public static function run(array $io, array $params)
3839
$file = $io['input']['data'];
3940
}
4041

41-
$process = new Process(['node', __DIR__ . '/sharp.js', $file, json_encode($params)]);
42+
$args = ['node', __DIR__ . '/sharp.js', $file, json_encode($params)];
43+
if (!empty($constructorOptions)) {
44+
$args[] = json_encode($constructorOptions);
45+
}
46+
$process = new Process($args);
4247
$process->run();
4348

4449
// Cleanup temporary file

src/sharp.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const sharp = require('sharp');
1616
throw new Error('Missing required arguments: file and apis');
1717
}
1818

19-
const [file, apisJson] = arguments;
19+
const [file, apisJson, constructorOptionsJson] = arguments;
2020

2121
// Validate JSON apis
2222
let apis;
@@ -26,8 +26,18 @@ const sharp = require('sharp');
2626
throw new Error('Invalid apis JSON format');
2727
}
2828

29-
// Initialize sharp with input file
30-
let image = sharp(file);
29+
// Parse constructor options (optional)
30+
let constructorOptions = {};
31+
if (constructorOptionsJson) {
32+
try {
33+
constructorOptions = JSON.parse(constructorOptionsJson);
34+
} catch (e) {
35+
throw new Error('Invalid constructorOptions JSON format');
36+
}
37+
}
38+
39+
// Initialize sharp with input file and optional constructor options
40+
let image = sharp(file, constructorOptions);
3141

3242
// Define supported APIs map, see https://sharp.pixelplumbing.com/
3343
const supportedApis = {

tests/Unit/SharpTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,25 @@ public function testCanRemoveAlphaChannel()
111111

112112
$this->assertFileExists($outputPath);
113113
}
114+
115+
public function testCanUseConstructorOptions()
116+
{
117+
$outputPath = $this->testOutputDir . '/with-density.png';
118+
119+
$io = [
120+
'input' => ['is_raw' => false, 'data' => $this->testSvgFile],
121+
'output' => ['is_raw' => false, 'file' => $outputPath]
122+
];
123+
124+
$params = [
125+
'resize' => ['width' => 200, 'height' => 200]
126+
];
127+
128+
// Use density option to set DPI for SVG rendering
129+
$constructorOptions = ['density' => 300];
130+
131+
Sharp::run($io, $params, $constructorOptions);
132+
133+
$this->assertFileExists($outputPath);
134+
}
114135
}

0 commit comments

Comments
 (0)