wpmind/includes/API/Services/ImageService.php
LinuxJoy b0b5032907 refactor: PublicAPI 拆分为 Facade + 6 个 Service 类
将 PublicAPI.php (2124行/76KB) 拆分为 Facade 模式架构:
- PublicAPI.php: 瘦 Facade (398行),保留单例、递归保护、状态方法
- Services/AbstractService.php: 共享基础设施 (provider解析/failover/缓存)
- Services/ChatService.php: chat + stream + SDK路由 + HTTP请求
- Services/TextProcessingService.php: translate + summarize + moderate
- Services/StructuredOutputService.php: structured + batch + schema验证
- Services/EmbeddingService.php: embed
- Services/AudioService.php: transcribe + speech
- Services/ImageService.php: generate_image (委托ImageRouter)

所有 15 个公共方法签名不变,wpmind_*() 全局函数兼容,
递归保护留在 Facade 层,Service 内部互调不触发递归检查。
已通过 7 项回归测试验证。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-07 12:32:36 +08:00

66 lines
1.4 KiB
PHP

<?php
/**
* Image Service
*
* 处理图像生成
*
* @package WPMind
* @subpackage API\Services
* @since 3.7.0
*/
declare(strict_types=1);
namespace WPMind\API\Services;
use WP_Error;
/**
* Image Service
*
* @since 3.7.0
*/
class ImageService extends AbstractService {
/**
* 生成图像
*
* @param string $prompt 图像描述
* @param array $options 选项
* @return array|WP_Error
*/
public function generate_image(string $prompt, array $options = []) {
$defaults = [
'context' => 'image_generation',
'size' => '1024x1024',
'quality' => 'standard',
'style' => 'natural',
'provider' => 'auto',
'return_format' => 'url',
];
$options = wp_parse_args($options, $defaults);
$context = $options['context'];
do_action('wpmind_before_request', 'image', compact('prompt', 'options'), $context);
if (class_exists('\\WPMind\\Providers\\Image\\ImageRouter')) {
$router = \WPMind\Providers\Image\ImageRouter::instance();
$result = $router->generate($prompt, $options);
} else {
return new WP_Error(
'wpmind_image_not_available',
__('图像生成服务不可用', 'wpmind')
);
}
if (is_wp_error($result)) {
do_action('wpmind_error', $result, 'image', compact('prompt', 'options'));
return $result;
}
do_action('wpmind_after_request', 'image', $result, compact('prompt', 'options'), []);
return $result;
}
}