intellex / stamper
使用不透明或半透明覆盖层为图像盖章
v1.1.0
2019-05-12 23:25 UTC
Requires
- ext-gd: *
- intellex/filesystem: ^1.0
Requires (Dev)
- intellex/debugger: ^1.0
This package is auto-updated.
Last update: 2024-09-13 11:37:49 UTC
README
当您想保持原始图像不可访问并仅提供其带水印版本时非常有用。
- 支持 单个盖章 和 拼贴水印。
- 集成了对 缓存 的支持,实现无缝集成。
- 易于进行如: 调整大小、旋转 和 透明度 等转换。
设置
为了充分利用这个库,需要一个网络服务器设置。网络服务器应识别所有针对图像的请求,然后将它们重定向到您的代理脚本。
以下是nginx配置示例
server { ... # This should be the first location section in your config location ~* /images/.*\.(jpe?g|png)(?|$) { fastcgi_pass unix:/run/php/php7.1-fpm.sock; try_files /Proxy/watermark.php =409; include fastcgi.conf; } ... }
示例
查看 /tests/proxy
目录中的完整工作片段。
获取盖章或水印
<?php // Load stamp or watermark from from any PNG file $stamp = new Stamp('/path/to/your/stamp.png'); // Feel free to play around with transformations $stamp->getSource()->resize(200, 100); // Resize to 200 x 100 $stamp->getSource()->rotate(180); // Rotate by 180 degrees $stamp->getSource()->setOpacity(0.5); // Make it 50% translucent // You can achieve the same effect using Transformation class $transformation = new Transformation( 200, 100, // Width and height 180, // Rotation 0.5 // Opacity ); $stamp->getSource()->apply($transformation); // All of the transformation can be done on the Image class in the same way!
将其应用于图像
<?php // Load the target image from either JPEG or PNG images $image = Image::fromFile('/path/to/your/target-image.jpeg'); // Execute $image->stamp($stamp, 100, 100); // Single stamp starting on position 100 x 100 $image->watermark($stamp); // Tiled watermark across the whole image
创建一个简单的文件作为代理使用
从JPEG或PNG图像加载目标图像。
<?php require '../vendor/autoload.php'; /** * Class StampImage ads a stamp to the image. */ class StampImage extends \Intellex\ImageStamp\Proxy { /** @inheritdoc */ protected function defineCache() { return new \Intellex\Filesystem\File('/path/to/where/this/response/will/be/cached.jpeg'); } /** @inheritdoc */ protected function defineCacheTimeToLive() { return 3600; // Cache for 1 hour } /** @inheritdoc */ protected function handle() { // TODO the magic here return 'THE DATA'; // Automatically cached in the file defined in defineCache() } } new StampImage();
完整示例
<?php require '../vendor/autoload.php'; class StampImage extends Proxy { /** @inheritdoc */ protected function defineCache() { return new File('/path/to/where/this/response/will/be/cached.jpeg'); } /** @inheritdoc */ protected function defineCacheTimeToLive() { return 3600; // Cache for 1 hour } /** @inheritdoc */ protected function handle() { // Skip favicon.ico request if($this->getRequestPath() === '/favicon.ico') { return null; } // Load image, load stamp and write to output file $image = Image::fromFile('/path/to/your/target-image.jpeg'); $stamp = new Stamp('/path/to/your/target-image.jpeg', new Transformation(400, 400)); $image->stamp($stamp, 280, 120); // Send the result header('Content-Type: ' . $image->getMimeType()); return $image->getBinary(); } } new StampImage();
待办事项
- 加快更改透明度的方法。
- 使在标准位置(例如:中心、顶部、右下角等)盖章更容易。
- 能够定义瓦片数量,以便水印自动缩放。
- 更多测试。
许可
MIT许可证
版权所有(c)2019 Intellex
在此,任何人免费获得此软件及其相关文档副本(“软件”)的副本,都可以在不限制的情况下处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并允许获得软件的人这样做,但受以下条件的约束:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任(无论基于合同、侵权或其他方式)承担责任,无论这些索赔、损害或其他责任是否源于、源于或与软件或其使用或其他交易有关。
鸣谢
脚本由 Intellex 团队编写。