camelot/image-asset

基于GD处理图片缩略图的图像管理库和捆绑包

安装: 30

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 1

公开问题: 0

类型:symfony-bundle

dev-master 2019-11-13 03:36 UTC

This package is auto-updated.

Last update: 2024-09-18 21:29:22 UTC


README

安装

使用Symfony Flex的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require camelot/image-asset

不使用Symfony Flex的应用程序

步骤 1:下载捆绑包

打开命令行控制台,进入您的项目目录并执行以下命令以下载此捆绑包的最新稳定版本

$ composer require camelot/image-asset

此命令需要您全局安装Composer,具体请参阅Composer文档中的安装章节

步骤 2:启用捆绑包

然后,通过将其添加到项目中 config/bundles.php 文件中注册的捆绑包列表来启用捆绑包

// config/bundles.php

return [
    // ...
    Camelot\ImageAsset\Bridge\Symfony\CamelotImageAssetBundle::class => ['all' => true],
];

配置

默认配置

# config/packages/camelot_image_asset.yaml
camelot_image_asset:
    image_dirs:
        - '%kernel.project_dir%/public/images'
    static_path: '%kernel.project_dir%/public/thumbs'
    routing:
        mount_point: /thumbs
        image:
            controller: Camelot\ImageAsset\Controller\ImageController
            path: '/{width}x{height}/{action}/{file}'
        image_alias:
            controller: Camelot\ImageAsset\Controller\ImageAliasController
            path: '/{alias}/{file}'
    default_image:
        path: image-default.png
        filesystem: camelot.image.filesystem.bundle
    default_image_size:
        width: 1024
        height: 768
    error_image:
        path: image-error.png
        filesystem: camelot.image.filesystem.bundle
    cache_time: null
    limit_upscaling: true
    only_aliases: false
    aliases: ~

别名

# config/packages/camelot_image_asset.yaml
camelot_image_asset:
    # ...

    aliases:
        my_alias:
            image_size:
                width: 1024
                height: 768
            action: ~ # One of "border"; "crop"; "fit"; "resize"
        other_alias:
            image_size:
                width:  1900
                height: 1200
            action: ~ # One of "border"; "crop"; "fit"; "resize"

路由

# config/routes/camelot_image_asset.yaml
camelot_image_asset:
    resource: .
    type: image_asset
    prefix: /

用法

Twig

    {% set uri = '/images/image.png' %}

    {# This will be cropped to the default width & height #}
    <img src="{{ thumbnail(uri, my_alias) }}">

    {# Same, but path resolved by Symfony's asset() Twig function #}
    {% set package_name = 'my_symfony_asset_package_name' %} # See config/packages/assets.yaml
    <img src="{{ thumbnail(asset(uri, package_name), my_alias) }}">

NGINX优化

location ~* /thumbs/(.*)$ {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~* ^.+\.(?:gif|jpe?g|jpeg|jpg|png|svg|svgz)$ {
    access_log          off;
    log_not_found       off;
    expires             max;
    add_header          Access-Control-Allow-Origin "*";
    add_header          Cache-Control "public, mustrevalidate, proxy-revalidate";
    add_header          Pragma public;
}