ajaxray/php-watermark
Composer 安装命令:
composer require ajaxray/php-watermark
包简介
Add text or image watermark on images.
README 文档
README
Add text or image Watermark on image and PDF using PHP and ImageMagick.
Pre-requisite
- PHP (version 5.6 or higher)
- ImageMagick
- ghostscript (only for PDF watermarking)
PHP ImageMagick extension is not required.
Installation
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
How to use?
<?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.
Customization options
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 -
- Txt-Img: Watermarking with text on Image (sample, sample-tiled)
- Img-Img: Watermarking with Image on Image (sample)
- Txt-PDF: Watermarking with text on PDF (sample)
- Img-PDF: Watermarking with Image on PDF (sample)
⌛ = 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.
Something unexpected? Debug! 🐞🔫
If anything unexpected happened, try to debug the issue.
- First step is to check if PHP is configured to display errors. Alternatively you may add these lines at the top of your script.
ini_set('display_errors', 1); error_reporting(E_ALL);
- A common reason of not getting expected result is mistakes in filepaths. You may try logging / printing source and destination file paths.
- Check the permission of the parent directory of destination path. Destination directory indicates -
- The file path mentioned in the second argument of
Watermark::withText()andWatermark::withImage()methods. - Parent of the source file itself if no separate destination mentioned in above methods.
- The file path mentioned in the second argument of
- There is
Watermark::setDebug()method which will makeWatermarkobject to return imagemagick command instead of executing it. Then, you may run the output manually to check if there is any error in underlyingimagemagickcommands.
Notes:
- To see the list of supported font names in your system, run
convert -list fonton command prompt - Remember to set appropriate output file extension (e,g, .pdf for pdf files)
- If possible, use absolute path for files to avoid various mistakes.
STYLE_IMG_*constants are for Image watermarks andWatermark::STYLE_TEXT_*are for text.- Default text style (
Watermark::STYLE_TEXT_BEVEL) is expected to be visible on any background. Use other text styles only on selective backgrounds. - UnitTest are executed and all green against PHP 5.6 and PHP 7.1 using PHPUnit 5.7.5
- I'v tested all intended functionality with ImageMagick 7.0.4-6 Q16 x86_64 and GPL Ghostscript 9.20 installed.
Important Update for PDF watermarking:
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
ajaxray/php-watermark 适用场景与选型建议
ajaxray/php-watermark 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 248.19k 次下载、GitHub Stars 达 141, 最近一次更新时间为 2017 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「watermark」 「image-manipulation」 「imagemagick-watermark」 「imagemagick-wrapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ajaxray/php-watermark 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ajaxray/php-watermark 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ajaxray/php-watermark 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Imagine (Wrapper) Component for Laravel
Powerful PHP class using GD library to work easily with images including layer notion (like Photoshop or GIMP)
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Powerful PHP class using GD library to work easily with images including layer notion (like Photoshop or GIMP)
ImageMagick wrapper
统计信息
- 总下载量: 248.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 148
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-03