enemis/sonata-media-liip-imagine

将 Sonata Media 与 Liip Imagine 集成最简单的方法

安装: 648

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

v1.3.8 2023-07-21 18:01 UTC

This package is auto-updated.

Last update: 2024-09-21 20:28:49 UTC


README

概述

无需痛苦即可轻松集成 Sonata Media Bundle 和 Liip Imagine Bundle。

如果您尝试过按照此页面中描述的方法将 LiipImagineBundle 和 Sonata Media Bundle 集成,您就知道我在说什么。我决定让 Symfony 世界摆脱这种头疼。

该包通过类似于原始 Sonata Media 的特定缩略图服务来支持 LiipImagineBundle。它也与 Sonata Admin Bundle 稳定工作。因此,如果您使用 Sonata Admin Bundle,则无需为您的管理部分执行任何其他操作。您可以自由尝试。配置该包大约需要几分钟。

1 安装

$ composer require enemis/sonata-media-liip-imagine

2 启用 Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Enemis\SonataMediaLiipImagineBundle\SonataMediaLiipImagineBundle(),
        );

        // ...
    }

    // ...
}

3 Sonata Media 配置

You need to configure your Sonata Media Bundle. Most important part is to configure context, cdn, providers.
You mustn't add formats to contexts that should use Liip formats. Tell to providers to use custom thumbnail service
'sonata.media.thumbnail.liip_imagine'. ** Please do not forget this bundle allow use Liip imagine only with image provider and youtube provider.
 If you require something else please let me know or suggest pull request **
#config.yml
sonata_media:
    # if you don't use default namespace configuration
    #class:
    #    media: MyVendor\MediaBundle\Entity\Media
    #    gallery: MyVendor\MediaBundle\Entity\Gallery
    #    gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
    default_context: photo # you need to set a context
    force_disable_category: true #true, if you really want to disable the relation with category
    category_manager:  null #null or "sonata.media.manager.category.default" if classification bundle exists
    admin_format: { width: 500 , quality: 90, format: 'jpg'}
    contexts:
        photo:
             download:
                 strategy: sonata.media.security.superadmin_strategy
                 mode: http
             providers:
                - sonata.media.provider.image
             formats: ~
        video:
             download:
                 strategy: sonata.media.security.superadmin_strategy
                 mode: http
             providers:
                - sonata.media.provider.youtube
             formats: ~
    cdn:
        server:
            path: /uploads/media #define path where sonata will be store uploaded files
    filesystem:
        local:
            directory:  "%kernel.root_dir%/../web/"
            create:     false
    providers:
        image:
            allowed_extensions: ['jpg', 'png', 'jpeg']
            allowed_mime_types: ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png']
            thumbnail:  sonata.media.thumbnail.liip_imagine #if you want use Liip with this provider you have to use this thumbnail service
        youtube:
            thumbnail:  sonata.media.thumbnail.liip_imagine
            html5: true

更多详细信息,您可以在官方文档中找到。

4 VideoProvider 和 YouTube 3 API

如果您使用的是 Sonata Media Bundle 中的 YouTube 提供程序,您可能已经发现缩略图的质量并不总是很好。这是因为在 oembed API 没有接收到高分辨率缩略图,而是默认大小。我已经将 oembed 更改为 YouTube API 3,现在 YouTube 提供程序可以获取高分辨率缩略图以及 YouTube 统计数据(观看次数、点赞、点踩);在使用之前,您需要生成 Google API 密钥并在https://console.developers.google.com上启用 YouTube API 3;

# This file is auto-generated during the composer install
parameters:
    google_youtube_api_key: AIza--sCE3t-r3Y5h0BbtRDPXVDJWQOM------

5 配置 Liip Imagine 并添加过滤器集

您需要以格式 CONTEXT_FORMAT 添加过滤器集。您可以使用 Liip Imagine 提供的任何过滤器或后处理器。

#config.yml
liip_imagine:
    filter_sets:
        video_wide:
            filters:
                downscale:
                     max: [970, 500]
                watermark:
                     # path to the watermark file (prepended with "%kernel.root_dir%")
                     image: '../web/watermark.png'
                     # size of the water mark relative to the input image
                     size: 0.3
                     position: bottomright
            post_processors:
                mozjpeg: { quality: 80 }

        photo_wide:
            filters:
                downscale:
                     max: [970, 500]
                watermark:
                     # path to the watermark file (prepended with "%kernel.root_dir%")
                     image: '../web/watermark.png' 
                     # size of the water mark relative to the input image
                     size: 0.3
                     position: bottomright

        photo_preview:
            filters:
                downscale:
                     max: [250, 250]

        video_preview:
            filters:
                downscale:
                     max: [100, 250]
            post_processors:
                mozjpeg: { quality: 80 }

有关 Liip Imagine 的更多信息,请参阅更多信息

6 覆盖 Sonata 的 Media 提供程序。

通常,此包覆盖了 sonata 的提供程序(ImageProvider 和 YouTubeProvider)。如果您想覆盖某些提供程序的部分,您必须通过 Enemis\SonataMediaLiipImagineBundle\Provider\ImageProvider 扩展自己的提供程序。对于 YouTube 提供程序,则为 Enemis\SonataMediaLiipImagineBundle\Provider\YouTubeProvider,并覆盖相应的参数。Sonata Media 有两个参数用于此目的。只需覆盖它即可。

  • sonata.media.provider.image.class: Application\Sonata\MediaBundle\Provider\ImageProvider
  • sonata.media.provider.youtube.class: Application\Sonata\MediaBundle\Provider\YouTubeProvider

您可以将这些参数放入 parameters.yml、config.yml。我更喜欢从 Application\Sonata\MediaBundle 中添加它们到 admin.yml。

#admin.yml
parameters:
    sonata.media.provider.image.class: Application\Sonata\MediaBundle\Provider\ImageProvider
    sonata.media.provider.youtube.class: Application\Sonata\MediaBundle\Provider\YouTubeProvider

并且不要忘记通过 Enemis\SonataMediaLiipImagineBundle\Provider 命名空间中的提供程序扩展您的提供程序。

<?php
#Application\Sonata\MediaBundle\Provider\YouTubeProvider

namespace Application\Sonata\MediaBundle\Provider;

use Enemis\SonataMediaLiipImagineBundle\Provider\YouTubeProvider as EnemisYoutubeProvider;

class YouTubeProvider extends EnemisYoutubeProvider
{
    /**
     * Get provider reference url.
     *
     * @param MediaInterface $media
     *
     * @return string
     */
    public function getReferenceUrl(MediaInterface $media)
    {
        return sprintf('https://www.youtube.com/watch?v=%s', $media->getProviderReference());
    }
}

7 故障排除指南

此捆绑包必须在 Sonata 媒体捆绑包 \ ApplicationSonataMediaBundle 之后,否则您将收到异常:服务 "enemis.sonata_media_liip_imagine.chain_resizer" 依赖于一个不存在的服务 "sonata.media.adapter.image.default"。

如果您有任何疑问,请随时通过stadnikandreypublic@gmail.com给我发邮件。您还可以创建问题并提交拉取请求。

享受吧

Alt text