From d37c9b1fc08a31be88290d9cd32c11760171837c Mon Sep 17 00:00:00 2001 From: waithawoo Date: Wed, 11 Sep 2024 14:44:08 +0700 Subject: [PATCH 1/4] add backgroundColor method for the output image --- src/Pdf.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Pdf.php b/src/Pdf.php index a3ba2f2..92452e2 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -12,6 +12,7 @@ use Spatie\PdfToImage\Exceptions\InvalidSize; use Spatie\PdfToImage\Exceptions\PageDoesNotExist; use Spatie\PdfToImage\Exceptions\PdfDoesNotExist; +use ImagickPixel; class Pdf { @@ -19,6 +20,8 @@ class Pdf protected int $resolution = 144; + protected ?string $backgroundColor = null; + protected OutputFormat $outputFormat = OutputFormat::Jpg; protected array $pages = [1]; @@ -61,6 +64,16 @@ public function resolution(int $dpiResolution): static return $this; } + /** + * Set the background color e.g. 'white', '#fff', 'rgb(255, 255, 255)' + */ + public function backgroundColor(string $backgroundColorCode): static + { + $this->backgroundColor = $backgroundColorCode; + + return $this; + } + /** * Sets the output format of the generated image. * Default is OutputFormat::Jpg. @@ -234,6 +247,11 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick $this->imagick->readImage(sprintf('%s[%s]', $this->filename, $pageNumber - 1)); + if ($this->backgroundColor !== null) { + $this->imagick->setImageBackgroundColor(new ImagickPixel($this->backgroundColor)); + $this->imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); + } + if ($this->resizeWidth !== null) { $this->imagick->resizeImage($this->resizeWidth, $this->resizeHeight ?? 0, Imagick::FILTER_POINT, 0); } From afe039eea195b8bd76503ec99998429dbf6e3049 Mon Sep 17 00:00:00 2001 From: waithawoo Date: Wed, 11 Sep 2024 14:44:28 +0700 Subject: [PATCH 2/4] add unit tests --- tests/Unit/PdfTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Unit/PdfTest.php b/tests/Unit/PdfTest.php index 2459be2..05df5c9 100644 --- a/tests/Unit/PdfTest.php +++ b/tests/Unit/PdfTest.php @@ -48,3 +48,15 @@ expect($imagick1)->toBeInstanceOf(Imagick::class); }); + +it('can set the background color', function ($backgroundColor) { + $image = (new Pdf($this->testFile)) + ->backgroundColor($backgroundColor) + ->selectPage(1) + ->getImageData('page-1.jpg', 1) + ->getImageBackgroundColor() + ->getColorAsString(); + + $expectedSRGBValueForWhiteColor = 'srgb(255,255,255)'; + expect($image)->toEqual($expectedSRGBValueForWhiteColor); +})->with(['srgb(255,255,255)', 'rgb(255,255,255)', 'white', '#fff']); From 7bb8d4d090f6f21c5b68a9d6761f1b0179887be8 Mon Sep 17 00:00:00 2001 From: waithawoo Date: Wed, 11 Sep 2024 14:44:41 +0700 Subject: [PATCH 3/4] update docs --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 6e80138..6c39ee5 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,19 @@ $pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::Merge); $pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::None); ``` +Set the background color of the output image: + +```php +$pdf->backgroundColor('white') // simple text for 'white' color + ->save($pathToWhereImageShouldBeStored); + +$pdf->backgroundColor('#fff') // code for 'white' color + ->save($pathToWhereImageShouldBeStored); + +$pdf->backgroundColor('rgb(255,255,255)') // rgb for 'white' color + ->save($pathToWhereImageShouldBeStored); +``` + ## Issues regarding Ghostscript This package uses Ghostscript through Imagick. For this to work Ghostscripts `gs` command should be accessible from the PHP process. For the PHP CLI process (e.g. Laravel's asynchronous jobs, commands, etc...) this is usually already the case. From f9d92df2ac4489fac9aef8698fa41f84121dfb77 Mon Sep 17 00:00:00 2001 From: waithawoo Date: Sat, 14 Sep 2024 16:21:59 +0700 Subject: [PATCH 4/4] modify checking setting value --- src/Pdf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pdf.php b/src/Pdf.php index 92452e2..5b3920d 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -247,7 +247,7 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick $this->imagick->readImage(sprintf('%s[%s]', $this->filename, $pageNumber - 1)); - if ($this->backgroundColor !== null) { + if (! empty($this->backgroundColor)) { $this->imagick->setImageBackgroundColor(new ImagickPixel($this->backgroundColor)); $this->imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); }