Skip to content

Commit

Permalink
Merge pull request #239 from waithawoo/add-background-color-setting
Browse files Browse the repository at this point in the history
Add background color setting for the output image
  • Loading branch information
patinthehat authored Sep 14, 2024
2 parents 9f56474 + f9d92df commit bdb0922
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use Spatie\PdfToImage\Exceptions\InvalidSize;
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
use ImagickPixel;

class Pdf
{
protected string $filename;

protected int $resolution = 144;

protected ?string $backgroundColor = null;

protected OutputFormat $outputFormat = OutputFormat::Jpg;

protected array $pages = [1];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -234,6 +247,11 @@ public function getImageData(string $pathToImage, int $pageNumber): Imagick

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

if (! empty($this->backgroundColor)) {
$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);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

0 comments on commit bdb0922

Please sign in to comment.