diff --git a/README.md b/README.md index 6c782e5..3d96e1b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Sharp.php b/src/Sharp.php index eea76bb..ce614bf 100644 --- a/src/Sharp.php +++ b/src/Sharp.php @@ -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 @@ -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 diff --git a/src/sharp.js b/src/sharp.js index f342f33..b1a6cda 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 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 = { diff --git a/tests/Unit/SharpTest.php b/tests/Unit/SharpTest.php index e52fc32..950f98b 100644 --- a/tests/Unit/SharpTest.php +++ b/tests/Unit/SharpTest.php @@ -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)); + } } \ No newline at end of file