mostlyserious/craft-imgix-picture 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mostlyserious/craft-imgix-picture

Composer 安装命令:

composer require mostlyserious/craft-imgix-picture

包简介

A Twig helper to generate picture tags from a set of imgix transforms.

README 文档

README

A Twig helper to generate responsive, optimized <picture> tags from a set of image transforms. It is intended for use with imgix, but can be used with Craft's native transforms also.

Requirements

This plugin requires Craft CMS 4.9.0 or later, and PHP 8.0.2 or later.

It assumes that you have at least one Asset Volume and File System configured (Typically an Amazon S3 bucket) and a corresponding imgix source that references that Asset Volume's File System.

Installation

You can install this plugin with Composer.

With Composer

Open your terminal and run the following commands:

# go to the project directory
cd /path/to/my-project.test

# tell Composer to load the plugin
composer require mostlyserious/craft-imgix-picture

# tell Craft to install the plugin
./craft plugin/install imgix-picture

Getting Started

Create an imgix account with at least one source and an API Key.

The API key requires Purge permissions, and it is used only to purge assets on Asset events, such as replace.

Add your imgix url and API key as environment variables to your .env file.

IMGIX_API_KEY="aXzY....."
IMGIX_SOURCE_URL="https://example.imgix.net"

Configuration

Create a config file at /config/imgix-picture.php.

Configure an imgix source for each Asset Volume by adding each volume to the volumes array using the volume handle as the key.

<?php

return [
    'imgixApiKey' => getenv('IMGIX_API_KEY'),
    'volumes' => [
        /** Settings for the "Uploads" volume */
        'uploads' => [
            'imgixSourceUrl' => getenv('IMGIX_SOURCE_URL'),
            // 'useNativeTransforms' => false /* optional */
            // 'altTextHandle' => 'alternativeText' /* optional */
        ],
        /** Settings for the other volumes */
        // 'volumeHandle' => [
        // ...
        // ],
    ],
    // 'defaultParameters' => [] /* optional */
    // 'fallBackImageSrc' => '/static-assets/default-image-missing-photo.png' /* optional */
];

If no Volume handle is found, Craft's native transforms are used. You can also force a volume to use native transforms by setting the useNativeTransforms config for that volume to true.

Legacy Configuration

Early versions of the plugin used a single imgix source that was applied to all volumes. It is recommended to migrate to a per-volume configuration, as described above; but the following flat configuration is still supported using the imgixUrl key. This is equivalent to having one Volume configured:

<?php

return [
    'imgixApiKey' => getenv('IMGIX_API_KEY'),
    'imgixUrl' => getenv('IMGIX_URL'),
    // 'useNativeTransforms' => false /* optional */
    // 'altTextHandle' => 'alternativeText' /* optional */
    // 'defaultParameters' => [] /* optional */
    // 'fallBackImageSrc' => '/static-assets/default-image-missing-photo.png' /* optional */
];

Settings Explained

  • altTextHandle Alternative Text: This plugin assumes that Craft's native alt text field has been added to the desired Asset Volume's field layout. If you are using a different field for alternative text, you can override this by providing a different field handle in your config file.
  • defaultParameters Default Parameters: The plugin uses initial imgix parameters tuned for auto format and modest quality, but you can override these with with your own. ex. [ 'q' => 100 ]
  • useNativeTransforms Craft Transforms: set to true to bypass imgix and use Craft Transforms instead.
  • fallBackImageSrc Fallback Image: Imgix has it's own default image that you can set for a source. Apart from that, if you wish to configure an image to display when an Asset is not provided to the picture function you can set that here.

Usage

Use the picture function to generate a <picture> tag from an Asset.

{{ picture(
    assetField.one(),
    [
        {
            fit: 'crop',
            width: 120,
            height: 120
        },
        {
            breakpoint: 1024,
            fit: 'crop',
            width: 500,
            height: 500
        },
    ],
    {
        class: [
            'block',
            'w-full h-auto aspect-square',
            'object-cover',
            'lg:absolute lg:inset-0',
            'lg:h-full lg:aspect-auto',
        ],
    }
) }}

The picture() function

This function is called with 3 arguments:

  1. The first argument must be a Craft Asset object.
  2. The second is an array of transforms.
    • a non-standard breakpoint property is a min-width pixel value that adds a min-width media query to <source> elements inside the picture, ex: media="(min-width: 1024px)".
    • Focal Point parameters are added automatically if the Asset has a focal point.
    • Transforms may contain any parameter in the imgix Rendering API. Recommended default attributes are set by the plugin as a baseline, but will be overidden with any you provide here.
    /* Defaults are */
    [
        'auto' => 'format,compress',
        'q' => 35,
        'fit' => 'max',
    ];
    • If only one transform is passed in, an <img> tag will be rendered instead of a picture tag.
  3. The third parameter is an object representing html attributes you wish to add to the rendered element. Images are lazy loading by default, but you can override this, for example:
{
    fetchpriority: 'high',
}

Other Functions

  • singleSrc() create a single transformed url suitable for a src="" attribute
  • downloadUrl() creates a force download link to an imgix asset with the dl param
  • imgixAttrs returns the imgix attributes without a picture tag
  • getMaxDimensions returns the max dimensions of an asset within the defined parameters

Future Improvements (no timeline yet)

  • [] Ensure that a media attribute is not output if the result is a single img tag
  • [] Add support for use as a twig filter |picture()
  • [] Add support for manual media queries
  • [] Provide examples of how to use with the Element API

mostlyserious/craft-imgix-picture 适用场景与选型建议

mostlyserious/craft-imgix-picture 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 139 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「picture」 「Craft」 「imgix」 「craftcms」 「craft-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mostlyserious/craft-imgix-picture 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mostlyserious/craft-imgix-picture 我们能提供哪些服务?
定制开发 / 二次开发

基于 mostlyserious/craft-imgix-picture 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 139
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2024-07-29