ashleydawson/glide-bundle

Glide 图像处理在 Symfony2 项目中

安装数: 22,415

依赖关系: 0

建议者: 0

安全性: 0

星标: 6

关注者: 2

分支: 4

公开问题: 1

类型:symfony-bundle

5.0.0 2019-07-04 08:43 UTC

This package is auto-updated.

Last update: 2024-09-04 19:50:39 UTC


README

Build Status

Glide HTTP 图像处理库添加到 Symfony 2 项目。

简介

Glide 是一个图像处理/操作/缓存的库。它特别适合在 Flysystem 文件系统中修改和存储图像。它利用 Intervention 图像管理器来执行繁重的工作。通过 HTTP 接口公开图像 API,允许您在应用程序中嵌入图像。例如

<!-- Embed version of a particular image, truncating the width to 300 pixels -->
<img src="/route/to/image/controller/my-image.jpg?w=300" />

有关更多信息以及更好的解释,请阅读 官方 Glide 文档

此捆绑包将 Glide 库的所有方面整合到 Symfony 2 服务容器中 - 在 Symfony 2 项目中添加与 Glide 库相关联的功能和助手。

安装

您可以通过 Composer 安装 Glide Bundle。为此,只需在您的 composer.json 文件中添加包,如下所示

{
    "require": {
        "ashleydawson/glide-bundle": "~1.0"
    }
}

运行 composer update 以安装包。然后您需要在您的 app/AppKernel.php 中注册捆绑包

$bundles = array(
    // ...
    new AshleyDawson\GlideBundle\AshleyDawsonGlideBundle(),
);

基本用法

最简单的例子实际上是使用 glide 服务器在一个控制器中处理图像。

<?php

namespace Acme\AcmeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;

class MyController extends Controller
{
    /**
     * @Route("/images/{image_name}.jpg")
     */
    public function showImageAction(Request $request)
    {
        // Filesystems for source and cache
        $sourceFilesystem = new Filesystem(new Local('/path/to/source/dir'));
        $cacheFilesystem = new Filesystem(new Local('/path/to/cache/dir'));
    
        // Create a Glide server
        $glideServer = $this
            ->get('ashleydawson.glide.server_factory')
            ->create($sourceFilesystem, $cacheFilesystem)
        ;
        
        // Return the processed image response
        return $glideServer->getImageResponse($request->get('image_name'), $request->query->all());
    }
}

上面的例子将创建一个使用本地文件系统作为源和缓存的 glide 服务器。然后操作返回一个使用图像名(在源文件系统中)和包含操作参数的请求构建的图像响应。

注意:您可能想将缓存文件系统指向 Symfony 缓存,app/cache

自定义操作

操作是转换图像的服务。Glide 附带了一系列操作,提供大小、效果、输出等转换。

如果您想注册自己的自定义操作,只需创建一个,并将其标记为在 Symfony 2 服务容器中的操作集合。如下所示

<?php

namespace Acme\AcmeBundle\Glide\Manipulator;

use League\Glide\Api\Manipulator\ManipulatorInterface;
use Intervention\Image\Image;
use Symfony\Component\HttpFoundation\Request;

class MyAwesomeManipulator implements ManipulatorInterface
{
    /**
     * {@inheritdoc}
     */
    public function run(Request $request, Image $image)
    {
        if ($request->has('awesome')) {
            // Do something awesome to the image here...    
        }
    
        return $image;
    }
}

然后,在服务容器中,只需将您的新操作标记为 glide 操作集合的一部分。

在 YAML 中

services:

    acme.glide.manipulator.my_awesome_manipulator:
        class: Acme\AcmeBundle\Glide\Manipulator\MyAwesomeManipulator
        tags:
            - { name: ashleydawson.glide.manipulators }

或者,在 XML 中

<services>
    <service id="acme.glide.manipulator.my_awesome_manipulator" class="Acme\AcmeBundle\Glide\Manipulator\MyAwesomeManipulator">
        <tag name="ashleydawson.glide.manipulators" />
    </service>
</services>

好的,现在我们可以在图像上使用这个操作了

<img src="/route/to/image/controller/my-image.jpg?awesome=foo" />