Skip to content

Commit

Permalink
Watermark objects. Color for watermark text (Closes #1866)
Browse files Browse the repository at this point in the history
  • Loading branch information
finwe committed May 29, 2023
1 parent 11b474e commit 1d71124
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ New features
* Set font-size to `auto` in textarea and input in active forms to resize the font-size (@ChrisB9, #1721)
* PHP 8.2 support in mPDF 8.1.3
* Added support for `psr/log` v3 without dropping v2. (@markdorison, @apotek, @greg-1-anderson, #1857)
* Watermark text can now be colored using \Mpdf\Watermark DTO. \Mpdf\WatermarkImage DTO for images. (#1876)

Bugfixes
--------
Expand Down
33 changes: 32 additions & 1 deletion src/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ class Mpdf implements \Psr\Log\LoggerAwareInterface

private $preambleWritten = false;

private $watermarkTextObject;
private $watermarkImageObject;

/**
* @var string
*/
Expand Down Expand Up @@ -10573,7 +10576,8 @@ function watermark($texte, $angle = 45, $fontsize = 96, $alpha = 0.2)

$this->SetAlpha($alpha);

$this->SetTColor($this->colorConverter->convert(0, $this->PDFAXwarnings));
$color = $this->watermarkTextObject ? $this->watermarkTextObject->getColor() : 0;
$this->SetTColor($this->colorConverter->convert($color, $this->PDFAXwarnings));

$szfont = $fontsize;
$loop = 0;
Expand Down Expand Up @@ -10603,6 +10607,7 @@ function watermark($texte, $angle = 45, $fontsize = 96, $alpha = 0.2)
$this->Rotate($angle, $wx, $wy);
$this->Text($wx, $wy, $texte, $OTLdata, $textvar);
$this->Rotate(0);

$this->SetTColor($this->colorConverter->convert(0, $this->PDFAXwarnings));

$this->SetAlpha(1);
Expand Down Expand Up @@ -13067,17 +13072,43 @@ function SetFooter($Farray = [], $side = '')

function SetWatermarkText($txt = '', $alpha = -1)
{
if ($txt instanceof \Mpdf\Watermark) {
$this->showWatermarkText = true;
$this->watermarkTextObject = $txt;
$this->watermarkText = $txt->getText();
$this->watermarkTextAlpha = $txt->getAlpha();
$this->watermarkAngle = $txt->getAngle();
$this->watermark_font = $txt->getFont() === null ? $txt->getFont() : $this->watermark_font;
$this->watermark_size = $txt->getSize();

return;
}

if ($alpha >= 0) {
$this->watermarkTextAlpha = $alpha;
}

$this->watermarkText = $txt;
}

function SetWatermarkImage($src, $alpha = -1, $size = 'D', $pos = 'F')
{
if ($src instanceof \Mpdf\WatermarkImage) {
$this->showWatermarkImage = true;
$this->watermarkImage = $src->getPath();
$this->watermark_size = $src->getSize();
$this->watermark_pos = $src->getPosition();
$this->watermarkImageAlpha = $src->getAlpha();
$this->watermarkImgBehind = $src->isBehindContent();
$this->watermarkImgAlphaBlend = $src->getAlphaBlend();

return;
}

if ($alpha >= 0) {
$this->watermarkImageAlpha = $alpha;
}

$this->watermarkImage = $src;
$this->watermark_size = $size;
$this->watermark_pos = $pos;
Expand Down
66 changes: 66 additions & 0 deletions src/Watermark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Mpdf;

class Watermark
{

/** @var string */
private $text;

/** @var int */
private $size;

/** @var int */
private $angle;

/** @var mixed */
private $color;

/** @var float */
private $alpha;

/** @var string */
private $font;

public function __construct($text, $size = 96, $angle = 45, $color = 0, $alpha = 0.2, $font = null)
{
$this->text = $text;
$this->size = $size;
$this->angle = $angle;
$this->color = $color;
$this->alpha = $alpha;
$this->font = $font;
}

public function getText()
{
return $this->text;
}

public function getSize()
{
return $this->size;
}

public function getAngle()
{
return $this->angle;
}

public function getColor()
{
return $this->color;
}

public function getAlpha()
{
return $this->alpha;
}

public function getFont()
{
return $this->font;
}

}
72 changes: 72 additions & 0 deletions src/WatermarkImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Mpdf;

class WatermarkImage
{

const SIZE_DEFAULT = 'D';
const SIZE_FIT_PAGE = 'P';
const SIZE_FIT_FRAME = 'F';
const POSITION_CENTER_PAGE = 'P';
const POSITION_CENTER_FRAME = 'F';

/** @var string */
private $path;

/** @var mixed */
private $size;

/** @var mixed */
private $position;

/** @var float */
private $alpha;

/** @var bool */
private $behindContent;

/** @var string */
private $alphaBlend;

public function __construct($path, $size = self::SIZE_DEFAULT, $position = self::POSITION_CENTER_PAGE, $alpha = -1, $behindContent = false, $alphaBlend = 'Normal')
{
$this->path = $path;
$this->size = $size;
$this->position = $position;
$this->alpha = $alpha;
$this->behindContent = $behindContent;
$this->alphaBlend = $alphaBlend;
}

public function getPath()
{
return $this->path;
}

public function getSize()
{
return $this->size;
}

public function getPosition()
{
return $this->position;
}

public function getAlpha()
{
return $this->alpha;
}

public function isBehindContent()
{
return $this->behindContent;
}

public function getAlphaBlend()
{
return $this->alphaBlend;
}

}
26 changes: 26 additions & 0 deletions tests/Issues/Issue1866Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Issues;

class Issue1866Test extends \Mpdf\BaseMpdfTest
{

public function testWatermark()
{
$this->mpdf->SetWatermarkText(new \Mpdf\Watermark('Watermark text', 100, 90, '#996633', 0.4));

$this->mpdf->WriteHtml('Example text');

$this->assertStringStartsWith('%PDF-', $this->mpdf->OutputBinaryData());
}

public function testWatermarkImage()
{
$this->mpdf->SetWatermarkImage(new \Mpdf\WatermarkImage('../data/img/issue1609.png', 100, 90, '#996633', 0.4));

$this->mpdf->WriteHtml('Example text');

$this->assertStringStartsWith('%PDF-', $this->mpdf->OutputBinaryData());
}

}

0 comments on commit 1d71124

Please sign in to comment.