Skip to content

Commit bdb0922

Browse files
authored
Merge pull request #239 from waithawoo/add-background-color-setting
Add background color setting for the output image
2 parents 9f56474 + f9d92df commit bdb0922

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ $pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::Merge);
138138
$pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::None);
139139
```
140140

141+
Set the background color of the output image:
142+
143+
```php
144+
$pdf->backgroundColor('white') // simple text for 'white' color
145+
->save($pathToWhereImageShouldBeStored);
146+
147+
$pdf->backgroundColor('#fff') // code for 'white' color
148+
->save($pathToWhereImageShouldBeStored);
149+
150+
$pdf->backgroundColor('rgb(255,255,255)') // rgb for 'white' color
151+
->save($pathToWhereImageShouldBeStored);
152+
```
153+
141154
## Issues regarding Ghostscript
142155

143156
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.

src/Pdf.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use Spatie\PdfToImage\Exceptions\InvalidSize;
1313
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
1414
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
15+
use ImagickPixel;
1516

1617
class Pdf
1718
{
1819
protected string $filename;
1920

2021
protected int $resolution = 144;
2122

23+
protected ?string $backgroundColor = null;
24+
2225
protected OutputFormat $outputFormat = OutputFormat::Jpg;
2326

2427
protected array $pages = [1];
@@ -61,6 +64,16 @@ public function resolution(int $dpiResolution): static
6164
return $this;
6265
}
6366

67+
/**
68+
* Set the background color e.g. 'white', '#fff', 'rgb(255, 255, 255)'
69+
*/
70+
public function backgroundColor(string $backgroundColorCode): static
71+
{
72+
$this->backgroundColor = $backgroundColorCode;
73+
74+
return $this;
75+
}
76+
6477
/**
6578
* Sets the output format of the generated image.
6679
* Default is OutputFormat::Jpg.
@@ -234,6 +247,11 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick
234247

235248
$this->imagick->readImage(sprintf('%s[%s]', $this->filename, $pageNumber - 1));
236249

250+
if (! empty($this->backgroundColor)) {
251+
$this->imagick->setImageBackgroundColor(new ImagickPixel($this->backgroundColor));
252+
$this->imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
253+
}
254+
237255
if ($this->resizeWidth !== null) {
238256
$this->imagick->resizeImage($this->resizeWidth, $this->resizeHeight ?? 0, Imagick::FILTER_POINT, 0);
239257
}

tests/Unit/PdfTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@
4848

4949
expect($imagick1)->toBeInstanceOf(Imagick::class);
5050
});
51+
52+
it('can set the background color', function ($backgroundColor) {
53+
$image = (new Pdf($this->testFile))
54+
->backgroundColor($backgroundColor)
55+
->selectPage(1)
56+
->getImageData('page-1.jpg', 1)
57+
->getImageBackgroundColor()
58+
->getColorAsString();
59+
60+
$expectedSRGBValueForWhiteColor = 'srgb(255,255,255)';
61+
expect($image)->toEqual($expectedSRGBValueForWhiteColor);
62+
})->with(['srgb(255,255,255)', 'rgb(255,255,255)', 'white', '#fff']);

0 commit comments

Comments
 (0)