in2code/imager 问题修复 & 功能扩展

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

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

in2code/imager

Composer 安装命令:

composer require in2code/imager

包简介

Adding images from Gemini with Nano Banana

README 文档

README

Table of Contents

Introduction

This allows editors to generate AI-generated images directly in the TYPO3 backend. This works using Google Gemini (with Nano Banana).

Example photo from Gemini: documentation_exampleimage1.png

Example backend integration: documentation_backend_textmedia.png

Example graphic in frontend: documentation_frontend.png

Google Gemini with Nano Banana

Installation

composer req in2code/imager

After that, you have to set some initial configuration in Extension Manager configuration:

Title Default value Description
promptPlaceholder LLL:EXT:imager/Resources/Private/Language/Backend/locallang.xlf:prompt.placeholder LLL path to a label for placeholder for prompt field in backend
promptValue LLL:EXT:imager/Resources/Private/Language/Backend/locallang.xlf:prompt.value LLL path for a default value for prompt field in backend
promptPrefix - Prefix text that should be always added to the prompt at the beginning
combinedIdentifier 1:/_imager/ Define where to store new ai generated images
aspectRatio 16:9 Default ratio for new ai images. Must be one of this values: 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
geminiModel - Gemini model. If empty the default model gemini-3-pro-image:generateContent is used. Add a specific model or use the GOOGLE_GEMINI_MODEL environment variable instead
apiKey - Google Gemini API key. You can let this value empty and simply use ENV_VAR "GOOGLE_API_KEY" instead if you want to use CI pipelines for this setting

Note: It's recommended to use ENV vars for in2code/imager instead of saving the API-Key in Extension Manager configuration

GOOGLE_API_KEY=your_api_key_from_google
GOOGLE_GEMINI_MODEL=gemini-3.1-flash-image:generateContent

Extendability

There are some events in EXT:imager that can be used to

  • Decide to hide the button in backend (\In2code\Imager\Events\ButtonAllowedEvent::class)
  • Manipulate or overrule the template of the rendered button in backend (\In2code\Imager\Events\TemplateButtonEvent::class)
  • Manipulte the URL and request values before sending to Gemini (\In2code\Imager\Events\BeforeRequestEvent::class)

Custom LLM Integration (like DALL-E, Stable Diffusion, Midjourney, etc.)

Imager uses a factory pattern to allow custom LLM providers. By default, it uses Google Gemini, but you can easily integrate other AI services (OpenAI DALL-E, Stable Diffusion, Midjourney, etc.).

Implementing a Custom LLM Repository

  1. Create a custom repository class implementing RepositoryInterface - see example for OpenAI DALL-E:
<?php

declare(strict_types=1);

namespace Vendor\MyExtension\Domain\Repository\Llm;

use In2code\Imager\Domain\Repository\Llm\AbstractRepository;
use In2code\Imager\Domain\Repository\Llm\RepositoryInterface;
use In2code\Imager\Exception\ApiException;
use In2code\Imager\Exception\ConfigurationException;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Resource\StorageRepository;

class DallERepository extends AbstractRepository implements RepositoryInterface
{
    private string $apiKey = '';
    private string $apiUrl = 'https://api.openai.com/v1/images/generations';

    public function __construct(
        protected StorageRepository $storageRepository,
        protected ResourceFactory $resourceFactory,
        protected RequestFactory $requestFactory,
    ) {
        parent::__construct($storageRepository, $resourceFactory, $requestFactory);
        $this->apiKey = getenv('OPENAI_API_KEY') ?: '';
    }

    public function checkApiKey(): void
    {
        if ($this->apiKey === '') {
            throw new ConfigurationException('OpenAI API key not configured', 1735646100);
        }
    }

    public function getApiUrl(): string
    {
        return $this->apiUrl;
    }

    public function getImage(string $prompt): File
    {
        $this->checkApiKey();
        $imageData = $this->generateImageWithDallE($prompt);
        return $this->saveImageToStorage($imageData, $prompt);
    }

    protected function generateImageWithDallE(string $prompt): string
    {
        $payload = [
            'model' => 'dall-e-3',
            'prompt' => $prompt,
            'n' => 1,
            'size' => '1024x1024',
            'response_format' => 'b64_json',
        ];

        $options = [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->apiKey,
                'Content-Type' => 'application/json',
            ],
            'body' => json_encode($payload),
        ];

        $response = $this->requestFactory->request($this->getApiUrl(), $this->requestMethod, $options);

        if ($response->getStatusCode() !== 200) {
            throw new ApiException(
                'Failed to generate image with DALL-E: ' . $response->getBody()->getContents(),
                1735646101
            );
        }

        $responseData = json_decode($response->getBody()->getContents(), true);

        if (isset($responseData['data'][0]['b64_json']) === false) {
            throw new ApiException('Invalid DALL-E API response structure', 1735646102);
        }

        $this->mimeType = 'image/png';
        return base64_decode($responseData['data'][0]['b64_json']);
    }
}
  1. Register your custom repository in ext_localconf.php:
<?php
defined('TYPO3') || die();

// Register custom LLM repository
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['imager']['llmRepositoryClass']
    = \Vendor\MyExtension\Domain\Repository\Llm\DallERepository::class;
  1. Don't forget to:
    • Register your Repository in your Services.yaml
    • Set the required API key (e.g., OPENAI_API_KEY environment variable)
    • Flush TYPO3 caches after registration

Hint: The AbstractRepository base class provides methods for file storage, folder management, and temporary file handling, so you only need to implement the image generation logic specific to your LLM provider.

Changelog and breaking changes

Version Date State Description
2.1.0 2026-07-05 Feature Make model overwritable via extension configuration or ENV variable
2.0.1 2026-02-02 Task Add funding section to composer.json file
2.0.0 2026-01-01 Feature Support overruling of LlmRepository with a custom repository
1.4.0 2025-12-07 Feature Support TYPO3 14
1.3.0 2025-12-04 Feature Add ddev as local environment
1.2.0 2025-11-29 Feature Add event to manipulate the rendered button in the backend
1.1.0 2025-11-27 Task Add extension icon
1.0.0 2025-11-27 Task Initial release of in2code/imager

Contribution with ddev

This repository provides a DDEV-backed development environment. If DDEV is installed, simply run the following commands to quickly set up a local environment with example usages:

  • ddev start
  • ddev initialize

Backend Login:

Username: admin
Password: admin

Installation hint:

  1. Install ddev before, see: https://ddev.readthedocs.io/en/stable/#installation
  2. Install git-lfs before, see: https://git-lfs.github.com/
  3. You can add the gemini API key in .ddev/.env:
GOOGLE_API_KEY=your_api_key_from_google

in2code/imager 适用场景与选型建议

in2code/imager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 193 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 in2code/imager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2025-11-27