定制 heavymetalavo/craft-aialttext 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

heavymetalavo/craft-aialttext

Composer 安装命令:

composer require heavymetalavo/craft-aialttext

包简介

Generate suitable alt text for CraftCMS Asset Images

README 文档

README

Generate alt text for CraftCMS Asset Images using the Anthropic or OpenAI API.

🚨 NEW Additional AI Provider support for Anthropic is now available.

Plugin Store | GitHub Repository

Video demo

ai-alt-text-demo-v1-720p.mov

📋 Requirements

This plugin requires:

  • Craft CMS 5.0.0 or later
  • PHP 8.2 or later
  • An Anthropic API key or an OpenAI API key

📥 Installation

You can install this plugin from the Plugin Store or with Composer.

📦 With Composer

Open your terminal and run the following commands:

# tell Composer to load the plugin
composer require heavymetalavo/craft-aialttext
# or
ddev composer require heavymetalavo/craft-aialttext

Then:

# tell Craft to install the plugin
./craft plugin/install ai-alt-text
# or
ddev craft plugin/install ai-alt-text

🤖 Setup API Keys

Anthropic

  1. Visit https://console.anthropic.com/ and sign up.
  2. Navigate to Settings → API Keys.
  3. Create a new API key and save it to an environment variable (.env).
  4. Ensure you have credits in your account under Billing.

OpenAI

  1. Visit https://platform.openai.com/ and sign up in the top-right.
  2. Revisit the API platform home page again
  3. Click the ⚙️ icon in the top-right
  4. Left menu > Organization > API keys > + Create new secret key (top-right)
  5. Create a name and assign to a suitable project
  6. Permissions > Restricted > set "Model capabilities" to "Write" and "Responses API" to "Write"
  7. Save API key to an env var, you wont get to see it again!
  8. Make sure you have a credit balance! Left menu > Organization > Billing, loading $5 is probably going to get you quite far, disabling auto recharge might be safer though that's up to you!

🚀 How to use

  1. Check the plugin settings are suitable for your project (and your API key is added)
  2. Ensure your volumes have the native alt field assigned to the field layout
  3. Ensure your templates are updated to use the alt field, you could consider a fallback asset.alt ?: asset.title if that what was used before
  4. Then generate some AI Alt text by performing one of the following actions:
    1. Triggering a bulk action in the bulk actions table
    2. For individual or a group of specific assets find them in the Assets manager section clicking the checkbox on a row, clicking the cog icon to reveal the Element actions menu and select Generate AI Alt Text
    3. When viewing a single asset's page, open the action menu and select Generate AI Alt Text
    4. Upload a new asset (if the upload setting is enabled)
  5. The plugin will queue jobs to generate alt text for each selected asset

The Bulk Actions table in the AI Alt Text settings page

The CraftCMS assets manager with two assets selected and the 'Generate AI Alt Text' option visible in the active element actions menu

The active actions menu when viewing a single asset shows the 'Generate AI Alt Text' option

Example twig:

{% set asset = craft.assets.one() %}
<img src="{{ asset.url }}" alt="{{ asset.alt ?: asset.title }}">

🖥️ Console Commands

Important: These commands will create queue jobs which when run will generate the alt text. By default, all commands process all sites unless --site-id is specified.

Available Commands

Command Description
ai-alt-text/generate/stats Show alt text coverage statistics
ai-alt-text/generate/missing Queue jobs for assets without alt text (recommended)
ai-alt-text/generate/all Queue jobs for ALL assets (⚠️ overwrites existing alt text)
ai-alt-text/generate/single <id> [siteId] Queue job for a specific asset ID (optionally for one site)

Options

Option Alias Description Default
--site-id=<id> -s Process only specific site (if not set, processes all sites) *
--batch-size=<n> -b Assets per batch (memory efficiency) 500
--verbose -v Show detailed progress false
--force -f Skip confirmations false

Examples

# Check coverage across all sites
./craft ai-alt-text/generate/stats

# Queue missing alt text for all sites
./craft ai-alt-text/generate/missing

# Queue for specific site with verbose output
./craft ai-alt-text/generate/missing --site-id=2 --verbose

# Queue for single asset
./craft ai-alt-text/generate/single 123

# Queue for single asset on a specific site
./craft ai-alt-text/generate/single 123 2

⚙️ Plugin settings

After installation, configure the plugin at Settings → AI Alt Text:

📊 Settings overview

Setting Description
AI Provider Choose between OpenAI or Anthropic.
OpenAI/Anthropic API Key Your provider's API key.
Model The AI model to use (e.g., gpt-5-nano or claude-haiku-4-5).
Detail Level How detailed the image analysis should be (controls resolution/scaling).
OpenAI Reasoning Effort The reasoning effort level for OpenAI reasoning models.
Prompt The instructions sent to the AI provider as the system / instruction message (example below). Supports {asset.property} and {site.property} variables, plus {site.languageName} for the language's display name (e.g. English (United Kingdom)).
Propagate Whether the asset should be saved across all of its supported sites, if enabled it could save the same initial alt text value across all sites.
Generate for new image assets (on upload) Automatically generate alt text when new assets are created.
Process SVGs Attempt to generate alt text for SVG files when they are uploaded or batched processed.
Save translated results for each site Save translated results to translatable fields for each site.

🧠 Model Options

All vision models should work, these small models seem to hit the sweetspot between quality & cost:

  • claude-haiku-4-5 - For Anthropic: "The fastest model with near-frontier intelligence"
  • gpt-5-nano - For OpenAI: "Fastest, most cost-efficient version of GPT-5"

To find out which models are capable of vision, check the models page, click into a model's detail page (e.g., gpt-5-nano) and look for "Input: Text, image" in the features columns at the top.

💬 Default prompt

This is sent to the provider as the system / instruction message (Anthropic system, OpenAI instructions); the image is sent in the user turn with a short trigger. {site.languageName} resolves to the language's display name and {site.language} to its BCP 47 language tag (e.g. en-GB), so the default pairs them — edit the parenthesised format freely.

Describe the image provided (roughly 150 characters). The output MUST be suitable for use directly as an HTML alt attribute value. Consider transparency within the image if supported by the file type, e.g. don't suggest it has a dark background if it is transparent. When describing a person do not assume their gender. Do not add a prefix of any kind (e.g. "#", "alt text:", "An image of", "A photo of"). Do not wrap the output in quotes. Output in the language: {site.languageName} (BCP 47: {site.language})

🔍 Image detail options

OpenAI:

  • low - Fast, low-cost (512px x 512px) (default)
  • high - Standard high-fidelity understanding
  • original - Large, dense, spatially sensitive images (gpt-5.4+)
  • auto - Let the model choose

Anthropic:

  • low - 500px x 500px (recommended default)
  • medium - 1000px x 1000px
  • high - 1568px x 1568px

🧠 OpenAI Reasoning Effort options

Controls how much time the model spends "thinking" before generating a response (only applicable to OpenAI reasoning models like o* (e.g. o1) and gpt-5*).

  • none
  • minimal
  • low
  • medium
  • high
  • xhigh

For more information, refer to the OpenAI and Anthropic documentation.

🏷️ Field requirements

This plugin requires a native CraftCMS field for alt text with the handle alt to be added to all asset volumes where you want to generate alt text. The plugin will use this field to store the generated alt text.

To add this field:

  1. Go to Settings → Assets → Volume name → + Add → search for the alt field and click → save
  2. Scroll to Field Layout section
  3. Click the + Add button
  4. Search for the alt field and click
  5. Save changes to the volume
  6. Update your templates to use the new alt field

Provider-Specific Limits

  • Automatic Scaling: The plugin automatically detects when an image exceeds provider limits and applies transforms (resizing or quality reduction) before sending the payload.
  • Supported file types: Both AI providers support: png, jpeg, jpg, webp, gif (non-animated)
  • SVG Support: SVGs are rasterized to PNG (preserving transparency) before being sent to the AI (where transformSvgs is enabled).
  • AVIF, HEIC & HEIF Support: These image types are converted to PNG before being sent to an AI provider. This requires an image driver (ImageMagick built with AVIF/HEIF/HEIC support) that can decode them on environments without that support these assets are skipped.
  • Animated GIFs: Only the first frame is processed.
  • Private Assets: Assets on private volumes without public URLs can be sent as base64 encoded strings. Retrieving an image transform's file contents is not currently supported by CraftCMS. Meaning that for file types not supported by an AI provider (e.g. an SVG files) it is not possible to generate alt text for these private assets.
  • Servd/Cloud: Support for specialized asset bundles (like Servd) depends on the environment's ability to handle raster transforms.
  • Consider AI Provider level boundaries: e.g. OpenAI: "No watermarks or logos - No NSFW content - Clear enough for a human to understand"

Oddities, Logic & Limitations

  • The OpenAI API has image input requirements which over the lifespan of this plugin have changed without notice, the requirements are not always enforced, e.g. base64 encoded images above the required image dimensions have been accepted by the API.
  • Craft CMS uses the Imagine library for image processing, which can only transform select image formats. Support for these formats may vary depending on what ImageMagick drivers are available in your environment. If a source image is in a supported format, the plugin will generate a transform in one of the supported file types to send to the OpenAI API.
  • Where an unsupported file type is requested the plugin will attempt an image transform to a jpg to be sent instead
  • The plugin checks a file's mimetype to see if it's valid, or if it needs a format conversion before sending to the API
  • If an asset's dimensions are larger than the dimensions required by the API an image transform is sent instead
  • If an asset has no URL (private) and requires a transform (e.g. if the original asset is an unsupported mime type, or, the dimensions are too large), Craft may be unable to retrieve the transform's file contents for base64 encoding. In that case the plugin will not fall back to sending the original source file unless its MIME type is natively accepted by the AI provider.
  • Where an alternative image transformer is used, e.g. when an application is hosted on Servd and assets are processed through their asset platform this may not support svg -> raster transforms

🛠️ Troubleshooting

  • If the plugin returns errors about API authentication, verify your API key.
  • For "bad request" errors, ensure your selected model supports vision capabilities.
  • Alt text generation is processed through Craft's queue system for bulk operations, so check the queue if generation seems to be taking a long time.
  • Any errors should be logged, check your queue.log files!
  • Check the asset has a title! If somehow an asset exists without one craft will not be able to resave it.

⚠️ Disclaimer

We've taken some steps to try prevent unexpected costs with default plugin settings (e.g. detail: low and model: gpt-5-nano) though we take no responsibility for excessive API token usage that may result from mistakes, bugs, or security vulnerabilities within this plugin so use at your own risk.

If you are concerned about unexpected charges we recommend:

  • Set up rate limits and spending caps at the API account level in your OpenAI account settings
  • Start with smaller batches when using bulk generation until you're comfortable with the costs
  • Consider using the default low setting, which significantly reduces token usage
  • Monitor your OpenAI API usage regularly

📈 Example usage statistics

When testing with OpenAI using the default settings (gpt-4o-mini model, low detail level):

Metric Value
March budget $0.03 / $120
Total tokens used 163,713
Total requests 29

🙋 Support

If you encounter any issues or have questions, please submit them on GitHub.

Credits

The eye icon used in this project is from SVG Repo and is available under the CC0 1.0 Universal (Public Domain) license.

heavymetalavo/craft-aialttext 适用场景与选型建议

heavymetalavo/craft-aialttext 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 838 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2025-03-25