跳到内容

后置处理器

编辑此页

我们已经知道 过滤器 执行图像转换。这可能会让你想知道后置处理器是如何融入运行时的。为了帮助说明过滤器和后置处理器之间的区别,重要的是强调以下几点。

  • 过滤器修改的是图像
  • 后置处理器修改的是图像二进制数据

在所有过滤器运行后,结果是一个图像二进制数据。然后,它被提供给所有配置的后置处理器,由它们处理,并从它们返回。

提示

后置处理器可以安全地链接,即使它们操作不同的 mime 类型。这使得它们非常适合图像特定的优化技术。

内置处理器

默认情况下,提供了一些内置的后置处理器。

图片优化器

图像优化器分类的后置处理器旨在减小最终图像文件的大小,从而提高应用程序资源的加载性能。

自定义处理器

你可以定义自己的后置处理器来执行任何所需的图像二进制操作。后置处理器需要实现 PostProcessorInterface

1
2
3
4
interface PostProcessorInterface
{
    public function process(BinaryInterface $binary);
}

正如 PostProcessorInterface 中定义的那样,唯一必需的方法是一个名为 process 的方法,它被提供一个 BinaryInterface 的实例作为其唯一的参数,并随后返回一个 BinaryInterface 的实例。

提示

你可以选择在你的后置处理器中实现 ConfigurablePostProcessorInterface,以使其可配置。

以下是创建你自己的调用可执行文件的后置处理器的模板。你必须将 EXECUTABLE_PATH 类常量设置为所需可执行文件的绝对路径。你可能还想将 ['image/png'] 更改为你的自定义后置处理器支持的 mime 类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace App\Service;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Imagine\Filter\PostProcessorInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;

class MyCustomPostProcessor implements PostProcessorInterface
{
    public const EXECUTABLE_PATH = '/path/to/your/executable';

    /**
     * @param BinaryInterface $binary
     *
     * @return BinaryInterface
     */
    public function process(BinaryInterface $binary)
    {
        // ensure the passed binary is a png
        if (!in_array(strtolower($binary->getMimeType()), ['image/png'])) {
            return $binary;
        }

        // create a temporary input file
        if (false === $input = tempnam($path = sys_get_temp_dir(), 'custom_')) {
            throw new \Exception(sprintf('Error created tmp file in "%s".', $path));
        }

        // populate temporary file with passed file contents
        file_put_contents($input, $binary->getContent());

        // create a process builder, add the input file as argument
        $pb = new ProcessBuilder([self::EXECUTABLE_PATH]);
        $pb->add($input);

        // get a process instance and run it
        $process = $pb->getProcess();
        $process->run();

        // error out if command returned non-zero
        if (0 !== $process->getExitCode()) {
            unlink($input);
            throw new ProcessFailedException($process);
        }

        // retrieve the result
        $result = new Binary(
            file_get_contents($input),
            $binary->getMimeType(),
            $binary->getFormat()
        );

        // remove temporary file
        unlink($input);

        // return the result
        return $result;
    }
}

注册它:自动注册

默认情况下,你的后置处理器将自动注册,因为它实现了 PostProcessorInterface

你将能够通过使用其完全限定类名在你的配置中引用和使用你的自定义后置处理器。

注册它:手动注册

如果你想给它一个不同的名称,你需要配置一个服务,使用你的自定义后置处理器并用 liip_imagine.filter.post_processor 标记它。

要使用名称 my_custom_post_processor 注册 App\Service\MyCustomPostProcessor,你将使用以下配置。

1
2
3
4
5
6
# app/config/services.yml

app.post_processor.my_custom_post_processor:
    class: App\Service\MyCustomPostProcessor
    tags:
        - { name: 'liip_imagine.filter.post_processor', post_processor: 'my_custom_post_processor' }

现在你的自定义后置处理器可以在过滤器集中被引用,使用通过上面的 post_processor 标签属性分配的名称(在本例中为 my_custom_post_processor)。

1
2
3
4
5
6
7
# app/config/config.yml

liip_imagine:
    filter_sets:
        my_special_style:
            post_processors:
                my_custom_post_processor: { }
这项工作,包括代码示例,是在 Creative Commons BY-SA 3.0 许可下获得许可的。
目录
    版本