Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background color setting for the output image #239

Merged
merged 4 commits into from
Sep 14, 2024
Merged
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
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']);