Skip to content
Closed
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ Sharp::run(
);
```

Using constructor options (e.g., setting density for SVG/PDF files):
```php
Sharp::run(
[
'input' => [
'is_raw' => false,
'data' => $svgPath,
'options' => ['density' => 300] // Constructor options
],
'output' => ['is_raw' => false, 'file' => $pngPath],
],
[
'toFormat' => ['format' => 'png']
]
);
```

The `options` parameter in the input array accepts any valid Sharp constructor options. See the [Sharp constructor documentation](https://sharp.pixelplumbing.com/api-constructor/) for available options.

Refer to the test cases in `tests/Unit/SharpTest.php` for more examples.

## Contributing
Expand Down
13 changes: 11 additions & 2 deletions src/Sharp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Sharp
{
/**
* @param array $io {input: {is_raw: bool, data: string, ?ext: string}, output: {is_raw: bool, ?file: string}}
* @param array $io {input: {is_raw: bool, data: string, ?ext: string, ?options: object}, output: {is_raw: bool, ?file: string}}
* @param array $params {toFormat: {format: string, options: object}, {resize: {width: int, height: int, options: object}}}
* @return string
* @throws Exception
Expand All @@ -38,7 +38,16 @@ public static function run(array $io, array $params)
$file = $io['input']['data'];
}

$process = new Process(['node', __DIR__ . '/sharp.js', $file, json_encode($params)]);
// Extract constructor options if provided
$constructorOptions = isset($io['input']['options']) ? json_encode($io['input']['options']) : null;

// Build process arguments
$processArgs = ['node', __DIR__ . '/sharp.js', $file, json_encode($params)];
if ($constructorOptions !== null) {
$processArgs[] = $constructorOptions;
}

$process = new Process($processArgs);
$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 if provided
let constructorOptions = {};
if (constructorOptionsJson) {
try {
constructorOptions = JSON.parse(constructorOptionsJson);
} catch (e) {
throw new Error('Invalid constructor options 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
29 changes: 29 additions & 0 deletions tests/Unit/SharpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,33 @@ public function testCanRemoveAlphaChannel()

$this->assertFileExists($outputPath);
}

public function testCanUseConstructorOptions()
{
$outputPath = $this->testOutputDir . '/with-density.png';

$io = [
'input' => [
'is_raw' => false,
'data' => $this->testSvgFile,
'options' => ['density' => 300]
],
'output' => [
'is_raw' => false,
'file' => $outputPath
]
];

$params = [
'toFormat' => ['format' => 'png']
];

Sharp::run($io, $params);

$this->assertFileExists($outputPath);

// Verify the image has higher density by checking file size
// Higher density should result in larger file size
$this->assertGreaterThan(0, filesize($outputPath));
}
}