Stars: 121
Forks: 46
Pull Requests: 5
Issues: 21
Watchers: 8
Last Updated: 2023-08-14 19:28:58
Add text or image Watermark on image and PDF files
License: MIT License
Languages: PHP, Dockerfile
Add text or image Watermark on image and PDF using PHP and ImageMagick.
PHP ImageMagick extension is not required.
Add as a dependency with composer
$ composer require ajaxray/php-watermark
Or download latest version as a Zip file.
For PHP versions >= 5.6
to <8
use v0.1.2
$ composer require ajaxray/php-watermark:v0.1.2
Also you should check older readme file for PHP version <8
<?php
// Initiate with source image or pdf
$watermark = new Watermark('/path/to/source.jpg');
// Customize some options (See list of supported options below)
$watermark->withText('ajaxray.com')
->setFontSize(48)
->setRotate(30)
->setOpacity(.4)
->write('path/to/output.jpg');
// Watermark with Image
$watermark->withImage('path/to/logo.png')
->setPosition(Watermark::POSITION_BOTTOM_RIGHT)
->setStyle(Watermark::STYLE_IMG_DISSOLVE)
->write('path/to/output.jpg');
If output file name is skipped for Watermark::write()
function, the source file will be overridden.
The table below shows customization options and their support matrix.
Listed functions should be called on an object of Ajaxray\PHPWatermark\Watermark
.
Checkmark column titles means the following -
⌛ = coming soon!
Function | Value | Txt-Img | Img-Img | Txt-PDF | Img-PDF |
---|---|---|---|---|---|
setFont('Arial') |
string; Font Name | ✅ | ✅ | ||
setFontSize(36) |
int; Font size | ✅ | ✅ | ||
setOpacity(.4) |
float; between 0 (opaque) to 1 (transparent) | ✅ | ✅ | ✅ | ✅ |
setRotate(245) |
int; between 0 to 360 | ✅ | ✅ | ||
setPosition($position) |
int; One of Watermark::POSITION_* constants |
✅ | ✅ | ✅ | ✅ |
setOffset(50, 100) |
int, int; X and Y offset relative to position | ✅ | ✅ | ✅ | ✅ |
setStyle($style) |
int; One of Watermark::STYLE_* constants |
⌛ | ✅ | ⌛ | ⌛ |
setTiled() |
boolean; (default true ) |
✅ | ✅ | ⌛ | ⌛ |
setTileSize(200, 150) |
int, int; Width and Height of each tile | ✅ | ⌛ |
BTW, all the samples linked above are the results of these examples. You may generate them yourself just by running example scripts from command line -
$ php vendor/ajaxray/php-watermark/examples/example_img.php
$ php vendor/ajaxray/php-watermark/examples/example_pdf.php
Then you should get the result files in vendor/ajaxray/php-watermark/examples/img
and vendor/ajaxray/php-watermark/examples/pdf
directories.
If anything unexpected happened, try to debug the issue.
ini_set('display_errors', 1);
error_reporting(E_ALL);
Watermark::withText()
and Watermark::withImage()
methods.Watermark::setDebug()
method which will make Watermark
object to return imagemagick command instead of executing it.
Then, you may run the output manually to check if there is any error in underlying imagemagick
commands.convert -list font
on command promptSTYLE_IMG_*
constants are for Image watermarks and Watermark::STYLE_TEXT_*
are for text.Watermark::STYLE_TEXT_BEVEL
) is expected to be visible on any background.
Use other text styles only on selective backgrounds.When using the imagemagick + ghostscript extraction and joining pdf pages, it has a few drawbacks including file-size issue. Many developers were asking for a solution about the file size and PDF quality since releasing of this library. So, I've created a command line tool for PDF watermarking that will work without converting pages into images. As a result, you'll get better PDF quality and dramatically smaller file size.
https://github.com/ajaxray/markpdf
Please note that, it's not a PHP library. So you've to use it using exec, shell_exec or Symfony Process Component.
"This is the Book about which there is no doubt, a guidance for those conscious of Allah" - Al-Quran