intellex/stamper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

intellex/stamper

Composer 安装命令:

composer require intellex/stamper

包简介

Stamps the images with opaque or semi-transparent overlays

README 文档

README

Very useful when you want to keep you original images inaccessible and serve their watermarked version only.

  • Supports both single stamps and tiled watermarks.
  • Integrated support for cache, with seamless implementation.
  • Easy transformations such as: resize, rotate and opacity.

Setup

In order to fully utilize this library, a web server setup is necessary. Web server should recognize all requests going out for images and than redirect them to your proxy script.

Here is an example of nginx configuration:

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;
	}
	
	...
}

Examples

Check out /tests/proxy directory for fully working snippets.

Get the stamp or watermark
<?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!
Apply it to an image
<?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

Create a simple file that will be used as a proxy

Load the target image from either JPEG or PNG images.

<?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();
Full example
<?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();

To do

  1. Speed up the method for changing the opacity.
  2. Make it easier to stamp in the standard positions (ie: center, top, bottom-right, etc...).
  3. Be able to define the number of tiles, so that the watermark is automatically scalled.
  4. More tests.

Licence

MIT License

Copyright (c) 2019 Intellex

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Credits

Script has been written by the Intellex team.

intellex/stamper 适用场景与选型建议

intellex/stamper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 intellex/stamper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 intellex/stamper 我们能提供哪些服务?
定制开发 / 二次开发

基于 intellex/stamper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-12