stratease/imagifly

此软件包最新版本(v0.2.7)无可用许可证信息。

PHP图像过滤器库

v0.2.7 2017-12-19 04:23 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:31:18 UTC


README

基于单一源图像,通过任意数量的过滤器效果处理图像的PHP库。受https://github.com/imsky/holder的Web API启发

使用Intervention Image库进行图像处理。

示例

<img src="/image-builder/path/to/my/file.png/225x225/overlay:2/" >

路径的前一部分是图像构建器路径前缀 /image-builder/,表示路径的其余部分将由解析器处理。

接下来是图像的路径 /path/to/my/file.png。通常,这些应该是透明图像,以便某些过滤器能够适当运行。

最后,有两个过滤器被应用于此图像。一个特殊的缩放过滤器 /225x225/,表示宽度为225像素,高度为225像素。接着是 /overlay:2/,一个叠加过滤器,指定两个原始图像并排放置。

如果我们使用了一个原始图像...

original file

...在利用ImagiFly的过滤器路径之后,生成的图像将是

parsed file

设置

查看我们的示例以了解示例设置。

需要注意的是,在后台,我们有一个基于/image-builder/...路径的服务器指令,用于路由到我们的PHP图像构建器文件。

这将为每个请求的图像提供一定程度的开销,不适用于高流量网站。我们确实提供了一定程度的缓存来避免冗余的图像处理。

我们的Apache .htaccess

RewriteEngine On
RewriteRule ^image-builder/.*$ image-builder.php [NC,L]

这会将请求重定向到我们的后端脚本,该脚本处理路由解析和图像处理。

use stratease\ImagiFly\Builder;
use stratease\ImagiFly\RequestParser;
require_once("../vendor/autoload.php");
require_once("PinkFilter.php");


$builder = new Builder(['baseDirectory' => __DIR__.'/images/', // Path where we store base images. Can be in web directory or any location
    'cache' => false, // Lets disable the cache so we can see our PinkFilter adjustments per request.
    // Our request parser. You can provide your own by implementing the RequestParserInterface
    'requestParser' => new RequestParser(['requestPath' => $_SERVER['REQUEST_URI'],
                                            'pathPrepend' => 'image-builder'])]);

// this is how we register a custom filter, in our case an awesome PinkFilter
$builder->addFilterExtension(PinkFilter::getFilterMask(), new PinkFilter());

$builder->output(); // This will compile and output the image.

// Try this path.. /image-builder/chipmunks-are-awesome.png/225x225/overlay:2/pink:unicorn/
// it'll blow your mind!

此实现还提供了一个如何设置自定义过滤器的示例。有关更多信息,请参阅FilterInterface