tomatom/picture-bundle

Symfony 扩展包

安装: 45

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 4

分支: 0

开放问题: 0

类型:symfony-bundle

1.4 2017-11-02 16:17 UTC

This package is auto-updated.

Last update: 2024-09-07 16:53:09 UTC


README

picture

Symfony 扩展包,简化 <picture> 标签的使用 :)

定义断点并使用你的资源作为图片源,无需烦人的图片转换和冗长的代码块

依赖关系

  • vipsthumbnail 来自 libvips

  • symfony/framework-standard-edition ">=2.8|~3.3"

安装

  • 你需要 vipsthumbnail!,(通过 sudo apt install libvips-tools 安装,或者你可以 编译自己的或者在这里下载)

  • 使用 Symfony 框架创建项目

  • composer require tomatom/picture-bundle "1.4"

  • 将扩展包添加到 AppKernel.php:

new TomAtom\PictureBundle\TomAtomPictureBundle(),
  • 更新数据库模式、安装资源并清除缓存 ... 就完成了!

  • 可选parameters.yml(.dist) 中添加参数(定义自己的断点等)

# these are defaults
parameters:
    tt_picture_breakpoints: [575, 768, 991, 1199, 1690, 1920]
    tt_picture_converted_dir: '%kernel.project_dir%/web/tt_picture'
    tt_picture_jpeg_quality: 65

用法

  • 在模板中调用(支持 jpg, png 和 gif)
{# as function #}
{{ picture(asset('path/to/asset.jpg')) }}

{# as filter #}
{{ asset('path/to/asset.jpg') | picture }}

<picture> 将在首次渲染时生成并转换图片

  • 你可以为每个图片定义自定义断点和/或 jpeg 质量,如果需要的话
{# as function with custom breakpoints defined per image #}
{{ picture(asset('path/to/asset.jpg'), [300, 600, 1200]) }}

{# as function with custom jpeg quality defined per image #}
{{ picture(asset('path/to/asset.jpg'), null, 99) }}


{# as filter with custom breakpoints defined per image #}
{{ asset('path/to/asset.jpg') | picture([300, 600, 1200]) }}

{# as filter with custom jpeg quality defined per image #}
{{ asset('path/to/asset.jpg') | picture(null, 99) }}


{# use with SonataMedia images #}
{{ mediaUrl(entity.media, 'reference') | picture }}
  • 你也可以在 PHP 代码中检索转换后的图片(如果你需要从 API 等处提供图片),如下所示
// asset url - same as in Twig template
$originalAssetUrl = $this->container->get('assets.packages')->getUrl('bundles/app/images/test.jpg');
// get PictureHelper from container
$pictureHelper = $this->container->get('tomatom_picture.util.picture_helper');

// get array of all converted images for asset
$allConveretedImages = $pictureHelper->getAllConverted($originalAssetUrl);

// or specific image for breakpoint
$convertedAssetUrl = $pictureHelper->getAssetUrl($originalAssetUrl, 1920);
$convertedFilePath = $pictureHelper->getFilePath($originalAssetUrl, 1920);

// convert images if not exists and get array of converted images (for global breakpoints)
$allConveretedImages = $pictureHelper->convert($originalAssetUrl);
// convert images if not exists and get array of converted images (custom breakpoints and jpeg quality)
$allConveretedImages = $pictureHelper->convert($originalAssetUrl, [300, 600, 900], 70);

待办事项

  • jpeg 质量作为参数
  • 模板中的 jpeg 质量作为参数
  • 模板中的断点作为参数
  • 批量转换的命令
  • 转换除了资源之外的图片(例如来自 'web/uploads' 等)
  • 在控制器/命令/服务中检索转换后的图片的辅助器
  • 用于 SonataMedia 图片路径的辅助器