Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Sharp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/sharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/SharpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}