vsavritsky / sonata-media-liip-imagine
将 sonata media 与 liip imagine 集成的最简单方式
Requires
- php: >=7.1
- liip/imagine-bundle: ~2
- sonata-project/media-bundle: ~3.4
- symfony/framework-bundle: ~2.3||~3.0
Requires (Dev)
- symfony/expression-language: ~2.4||~3.0
- symfony/phpunit-bridge: ^3.3
This package is not auto-updated.
Last update: 2024-09-15 12:08:23 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 密钥并启用 YouTube API 3,具体操作请参考 https://console.developers.google.com;
- https://console.developers.google.com/projectcreate - 创建新项目
- 点击“添加服务”和“API”,然后选择 YouTube Data API v3 http://prntscr.com/m9sgzs
- 点击“启用”
- 创建用于使用此 API 的凭证 http://prntscr.com/m9sipt
- 复制生成的密钥并点击“完成” http://prntscr.com/m9sjdr
- 将生成的密钥放入您的 parameters.yml.dist
# 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 }
6 覆盖 Sonata 的媒体提供程序。
通常,此扩展覆盖了 sonata 的提供程序(ImageProvider 和 YouTubeProvider)。如果您想覆盖某些提供程序的部分,您必须通过 Enemis\SonataMediaLiipImagineBundle\Provider\ImageProvider 扩展自己的提供程序,或通过 Enemis\SonataMediaLiipImagineBundle\Provider\YouTubeProvider 扩展 YouTube 提供程序,并覆盖相应的参数。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()); } }
如果您有任何问题,请随时通过以下邮箱联系我:stadnikandreypublic@gmail.com。同时,您也可以创建问题并提交拉取请求。