PHP单元 | PHP-CS-修复程序 | 覆盖范围 | 下载 | 发布 |
---|---|---|---|---|
该捆绑包为基于 Symfony 的项目提供了图像处理抽象工具包。
过滤器集:使用任何 Symfony 支持的配置语言(例如 YML 和 XML),您可以创建指定转换例程的过滤器集定义。这些定义包括一组过滤器和后处理器,以及其他可选参数。
过滤器:使用过滤器应用图像变换。捆绑包提供了一组内置过滤器,用于实现最常见的转换;示例包括缩略图、比例、裁剪、翻转、条带和水印。要进行更多高级转换,您可以轻松创建自己的自定义过滤器。
后处理器:对生成的二进制图像文件(从过滤器创建)的修改由后处理器处理。示例包括 JPEG Optim、Moz JPEG、Opti PNG 和 PNG Quant。就像过滤器一样,您可以轻松创建自己的自定义后处理器。
假设您定义了一个my_thumb
过滤器集,可以将其配置为执行任意数量的不同转换。最简单的调用是将图像的路径通过管道传输到提供的imagine_filter
Twig 过滤器。
< img src = " {{ asset( ' /relative/path/to/image.jpg ' ) | imagine_filter( ' my_thumb ' ) }} " />
该项目随贡献者行为准则一起发布。参与该项目即表示您同意遵守其条款。
感谢许多贡献者为这个项目奉献了时间和代码。
该包使用独立的 PHP Imagine 库进行图像转换。
该包是从 AvalancheImagineBundle 分叉出来的,目的是使代码更具可扩展性。请参阅 AvalancheImagineBundle#25,了解有关此分叉推理的更多信息。
使用这个包与所有 Symfony 包类似。必须执行以下步骤
详细的设置说明可以在文档的安装章节中找到。
有关所有可用配置选项的详细信息可以在文档的配置章节中找到。
一般来说,该捆绑包的工作原理是将过滤器集应用于模板内部的图像。您的过滤器集在应用程序的配置文件(通常是app/config/config.yml
)中定义,并由过滤器、后处理器和其他可选参数的集合组成。
稍后我们将了解有关后处理器和其他可用参数的更多信息,但现在让我们重点关注如何定义由几个过滤器组成的简单过滤器集。
在开始之前,需要进行少量配置以确保我们的数据加载器和缓存解析器正确运行。在配置文件中使用以下样板。
# app/config/config.yml
liip_imagine :
# configure resolvers
resolvers :
# setup the default resolver
default :
# use the default web path
web_path : ~
# your filter sets are defined here
filter_sets :
# use the default cache configuration
cache : ~
基本配置到位后,我们将从一个满足常见用例的示例开始:创建缩略图。假设我们希望对生成的缩略图应用以下转换:
添加到上面的样板中,我们需要定义一个过滤器集(我们将其命名为my_thumb
),并配置两个过滤器: thumbnail
和background
过滤器。
# app/config/config.yml
liip_imagine :
resolvers :
default :
web_path : ~
filter_sets :
cache : ~
# the name of the "filter set"
my_thumb :
# adjust the image quality to 75%
quality : 75
# list of transformations to apply (the "filters")
filters :
# create a thumbnail: set size to 120x90 and use the "outbound" mode
# to crop the image when the size ratio of the input differs
thumbnail : { size : [120, 90], mode : outbound }
# create a 2px black border: center the thumbnail on a black background
# 4px larger to create a 2px border around the final image
background : { size : [124, 94], position : center, color : '#000000' }
您现在已经创建了一个名为my_thumb
的过滤器集,用于执行缩略图转换。 thumbnail
过滤器将图像调整为所需的宽度和高度(在本例中为 120x90px),并且其mode: outbound
选项会导致在输入比例不同时裁剪生成的图像。 background
过滤器通过创建尺寸为 124x94px 的黑色画布并将缩略图放置在其中心来产生 2px 的黑色边框。
注意:过滤器组可以定义任意数量的过滤器。简单的转换可能只需要一个过滤器,而复杂的转换可以拥有为其定义的无限数量的过滤器。
还有许多其他过滤器,但现在您可以在模板中立即使用新定义的my_thumb
过滤器集。
对于基于 Twig 的模板,请使用:
< img src = " {{ asset( ' /relative/path/to/image.jpg ' ) | imagine_filter( ' my_thumb ' ) }} " />
或者,对于基于 PHP 的模板,请使用:
<img src=" <?php $ this ['imagine']->filter('/relative/path/to/image.jpg', 'my_thumb') ?> " />
在幕后,当提供第一个页面请求时,捆绑包会动态地将过滤器应用于图像。然后,缓存转换后的图像以供后续请求使用。最终的缓存图像路径类似于/media/cache/my_thumb/relative/path/to/image.jpg
。
注意: *使用dev
环境,您可能会发现图像无法通过模板助手正确呈现。这通常是由于在应用程序配置中启用了intercept_redirect
导致的。为了确保图像渲染,强烈建议禁用此选项:
# app/config/config_dev.yml
web_profiler :
intercept_redirects : false
有时,您可能定义了一个可以满足 99% 使用场景的过滤器。您可以选择通过向模板助手传递一个选项数组来在运行时更改过滤器的行为,而不是为 1% 的错误情况定义新的过滤器。
对于基于 Twig 的模板,请使用:
{% set runtimeConfig = { " thumbnail " : { " size " : [ 50 , 50 ] }} %}
< img src = " {{ asset( ' /relative/path/to/image.jpg ' ) | imagine_filter( ' my_thumb ' , runtimeConfig ) }} " />
或者,对于基于 PHP 的模板,请使用:
<?php
$ runtimeConfig = array (
" thumbnail " => array (
" size " => array ( 50 , 50 )
)
);
?>
<img src=" <?php $ this [ ' imagine ' ]-> filter ( ' /relative/path/to/image.jpg ' , ' my_thumb ' , $ runtimeConfig ) ?> " />
有时您需要解析该包返回的图像路径以获取过滤后的图像。这可以使用 Symfony 的控制台二进制文件或在控制器或其他代码中以编程方式轻松实现。
您可以使用控制台命令liip:imagine:cache:resolve
解析图像 URL。唯一需要的参数是一个或多个相对图像路径(必须用空格分隔)。
$ php bin/console liip:imagine:cache:resolve relative/path/to/image1.jpg relative/path/to/image2.jpg
此外,您可以使用--filter
选项指定要解析的过滤器(如果省略--filter
选项,将解析所有可用的过滤器)。
$ php bin/console liip:imagine:cache:resolve relative/path/to/image1.jpg --filter=my_thumb
您可以使用liip_imagine.cache.manager
服务的getBrowserPath
方法解析代码中的图像 URL。假设您已经将服务分配给名为$imagineCacheManager
变量,您将运行:
$ imagineCacheManager -> getBrowserPath ( ' /relative/path/to/image.jpg ' , ' my_thumb ' );
通常,您需要在控制器中执行此操作。假设您的控制器继承自基本 Symfony 控制器,您可以利用继承的get
方法来请求liip_imagine.cache.manager
服务,您可以在相对图像路径上调用getBrowserPath
来获取其解析位置。
/** @var CacheManager */
$ imagineCacheManager = $ this -> get ( ' liip_imagine.cache.manager ' );
/** @var string */
$ resolvedPath = $ imagineCacheManager -> getBrowserPath ( ' /relative/path/to/image.jpg ' , ' my_thumb ' );
该捆绑包提供了一组内置过滤器,您也可以轻松定义自己的过滤器。请参考我们文档中的过滤器章节。
如果您需要在控制器中使用定义的“过滤器集”,您可以从服务容器中获取此捆绑包的 FilterService 来为您完成繁重的工作。
<?php
class MyController extends Controller
{
public function indexAction ()
{
/** @var FilterService */
$ imagine = $ this
-> container
-> get ( ' liip_imagine.service.filter ' );
// 1) Simple filter, OR
$ resourcePath = $ imagine -> getUrlOfFilteredImage ( ' uploads/foo.jpg ' , ' my_thumb ' );
// 2) Runtime configuration
$ runtimeConfig = [
' thumbnail ' => [
' size ' => [ 200 , 200 ]
],
];
$ resourcePath = $ imagine -> getUrlOfFilteredImageWithRuntimeFilters (
' uploads/foo.jpg ' ,
' my_thumb ' ,
$ runtimeConfig
);
// ..
}
}
?>
默认情况下,Symfony 的web/
目录被注册为数据根以从中加载资源。对于许多安装来说,这已经足够了,但有时您可能需要从其他位置加载图像。为此,您必须在配置中设置data_root
参数(通常位于app/config/config.yml
)。
liip_imagine :
loaders :
default :
filesystem :
data_root : /path/to/source/images/dir
从版本1.7.2
开始,您可以注册多个数据根路径,文件定位器将在每个数据根路径中搜索所请求的文件。
liip_imagine :
loaders :
default :
filesystem :
data_root :
- /path/foo
- /path/bar
从版本1.7.3
开始,您要求将所有已注册捆绑包中的公共资源路径自动注册为数据根。这允许您从驻留在加载的捆绑包中的Resources/public
文件夹加载资源。要启用此功能,请将bundle_resources.enabled
配置选项设置为true
。
liip_imagine :
loaders :
default :
filesystem :
bundle_resources :
enabled : true
如果您想要注册部分Resource/public
文件夹,但不是全部,您可以通过将您不想注册的捆绑包列入黑名单或将您想要注册的捆绑包列入白名单来实现。例如,要将捆绑包“FooBundle”和“BarBundle”列入黑名单(而不是注册),您可以使用以下配置。
liip_imagine :
loaders :
default :
filesystem :
bundle_resources :
enabled : true
access_control_type : blacklist
access_control_list :
- FooBundle
- BarBundle
或者,如果您想将捆绑包“FooBundle”和“BarBundle”列入白名单(仅注册),您可以使用以下配置。
liip_imagine :
loaders :
default :
filesystem :
bundle_resources :
enabled : true
access_control_type : whitelist
access_control_list :
- FooBundle
- BarBundle
您的网络服务器必须能够读取图像位置。在支持setfacl
的系统(如Linux/BSD)上,使用
HTTPDUSER= ` ps axo user,comm | grep -E ' [a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx ' | grep -v root | head -1 | cut -d -f1 `
sudo setfacl -R -m u: " $HTTPDUSER " :rwX -m u: ` whoami ` :rwX /path/to/source/images/dir
sudo setfacl -dR -m u: " $HTTPDUSER " :rwX -m u: ` whoami ` :rwX /path/to/source/images/dir
有关与 macOS 和其他环境兼容的命令,请参阅 Symfony 权限文档。
您需要通过将以下内容添加到 Apache VHost 配置来授予 Apache 读取权限
< VirtualHost *:80>
<!-- Rest of directives like DocumentRoot or ServerName -->
Alias /FavouriteAlias /path/to/source/images/dir
< Directory " /path/to/source/images/dir " >
AllowOverride None
Allow from All
</ Directory >
</ VirtualHost >
或者,您可以将该指令放置在项目中的单独文件中,并将其包含在 Apache VHost 配置中。例如,您可以创建文件app/config/apache/photos.xml
并将以下内容添加到您的 VHost 文件中
< VirtualHost *:80>
<!-- Rest of directives like DocumentRoot or ServerName -->
Include "/path/to/your/project/app/config/apache/photos.xml"
</ VirtualHost >
此方法将文件与其余代码一起保留,使您可以轻松更改它或创建不同的依赖于环境的配置文件。
正确配置 Apache 后,具有以下绝对路径/path/to/source/images/dir/logo.png
的图像的相对路径必须是/FavouriteAlias/logo.png
。
有关此捆绑包功能的更多详细信息,请参阅文档。