Skip to content

Commit 633f5d4

Browse files
authored
Image generation recovery (#36)
* removing the model selection bug * adding the media manager class * returning the image generation functionality
1 parent 21ab49d commit 633f5d4

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/MCP/Client.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ static function () {
110110
file_put_contents( $filename, $image_blob->get_binary_data() );
111111

112112
$image_url = $filename;
113+
$image_id = \WP_CLI\AiCommand\MediaManager::upload_to_media_library($image_url);
113114
}
114115

115116
break;
@@ -120,6 +121,8 @@ static function () {
120121
// TODO: Save as file or so.
121122
break;
122123
}
124+
125+
return $image_id || 'no image found';
123126
}
124127

125128
// See https://github.com/felixarntz/ai-services/blob/main/docs/Accessing-AI-Services-in-PHP.md for further processing.
@@ -188,17 +191,17 @@ static function () {
188191

189192
\WP_CLI::debug( 'Making request...' . print_r( $contents, true ), 'ai' );
190193

191-
if ( $service->get_service_slug() === 'openai' ) {
192-
$model = 'gpt-4o';
193-
} else {
194-
$model = 'gemini-2.0-flash';
195-
}
194+
// if ( $service->get_service_slug() === 'openai' ) {
195+
// $model = 'gpt-4o';
196+
// } else {
197+
// $model = 'gemini-2.0-flash';
198+
// }
196199

197200
$candidates = $service
198201
->get_model(
199202
[
200203
'feature' => 'text-generation',
201-
'model' => $model,
204+
// 'model' => $model,
202205
'tools' => $tools,
203206
'capabilities' => [
204207
AI_Capability::MULTIMODAL_INPUT,

src/MediaManager.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace WP_CLI\AiCommand;
4+
5+
class MediaManager {
6+
7+
public static function upload_to_media_library($media_path) {
8+
// Get WordPress upload directory information
9+
$upload_dir = wp_upload_dir();
10+
11+
// Get the file name from the path
12+
$file_name = basename($media_path);
13+
14+
// Copy file to the upload directory
15+
$new_file_path = $upload_dir['path'] . '/' . $file_name;
16+
copy($media_path, $new_file_path);
17+
18+
// Prepare attachment data
19+
$wp_filetype = wp_check_filetype($file_name, null);
20+
$attachment = array(
21+
'post_mime_type' => $wp_filetype['type'],
22+
'post_title' => sanitize_file_name($file_name),
23+
'post_content' => '',
24+
'post_status' => 'inherit'
25+
);
26+
27+
// Insert the attachment
28+
$attach_id = wp_insert_attachment($attachment, $new_file_path);
29+
30+
// Generate attachment metadata
31+
require_once(ABSPATH . 'wp-admin/includes/image.php');
32+
$attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path);
33+
wp_update_attachment_metadata($attach_id, $attach_data);
34+
35+
return $attach_id;
36+
37+
}
38+
}

0 commit comments

Comments
 (0)