Skip to content

Commit b6d53b4

Browse files
Copilotswissspidy
andauthored
Add image input support to wp ai generate text (#21)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent e2cb8bf commit b6d53b4

2 files changed

Lines changed: 222 additions & 2 deletions

File tree

‎features/generate.feature‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ Feature: Generate AI content
5858
new SupportedOption(OptionEnum::candidateCount()),
5959
new SupportedOption(OptionEnum::outputMimeType(), ['image/png']),
6060
new SupportedOption(OptionEnum::outputFileType(), [FileTypeEnum::inline()]),
61-
new SupportedOption(OptionEnum::inputModalities(), [[ModalityEnum::text()]]),
61+
new SupportedOption(OptionEnum::inputModalities(), [
62+
[ModalityEnum::text()],
63+
[ModalityEnum::image()],
64+
[ModalityEnum::text(), ModalityEnum::image()],
65+
]),
6266
new SupportedOption(
6367
OptionEnum::outputModalities(),
6468
[
@@ -274,3 +278,82 @@ Feature: Generate AI content
274278
"""
275279
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=
276280
"""
281+
282+
@require-wp-7.0
283+
Scenario: Generates image with a local image file
284+
Given a test-image.png file:
285+
"""
286+
fake-png-data
287+
"""
288+
289+
When I run `wp ai generate image "Edit this image" --image=test-image.png`
290+
Then STDOUT should be:
291+
"""
292+
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=
293+
"""
294+
295+
@require-wp-7.0
296+
Scenario: Generates text with a local image file
297+
Given a test-image.png file:
298+
"""
299+
fake-png-data
300+
"""
301+
302+
When I run `wp ai generate text "Describe this image" --image=test-image.png`
303+
Then STDOUT should be:
304+
"""
305+
This is mock-generated text
306+
"""
307+
308+
@require-wp-7.0
309+
Scenario: Generate text with image input fails for non-existent local file
310+
When I try `wp ai generate text "Describe this" --image=/tmp/nonexistent-image-12345.png`
311+
Then the return code should be 1
312+
And STDERR should contain:
313+
"""
314+
Image file not found
315+
"""
316+
317+
@require-wp-7.0
318+
Scenario: Generate text with image input fails for non-existent attachment
319+
When I try `wp ai generate text "Describe this" --image=99999`
320+
Then the return code should be 1
321+
And STDERR should contain:
322+
"""
323+
Attachment with ID 99999 not found
324+
"""
325+
326+
@require-wp-7.0
327+
Scenario: Generate text with image input fails for non-image local file
328+
Given a test-document.txt file:
329+
"""
330+
hello world
331+
"""
332+
333+
When I try `wp ai generate text "Describe this" --image=test-document.txt`
334+
Then the return code should be 1
335+
And STDERR should contain:
336+
"""
337+
File does not appear to be an image
338+
"""
339+
340+
@require-wp-7.0
341+
Scenario: Generate text with image input fails for non-attachment post
342+
When I run `wp post create --post_title="Test Post" --post_type=post --post_status=publish --porcelain`
343+
Then save STDOUT as {POST_ID}
344+
345+
When I try `wp ai generate text "Describe this" --image={POST_ID}`
346+
Then the return code should be 1
347+
And STDERR should contain:
348+
"""
349+
is not an attachment
350+
"""
351+
352+
@require-wp-7.0
353+
Scenario: Generate text with image input treats 0 as a file path
354+
When I try `wp ai generate text "Describe this image" --image=0`
355+
Then the return code should be 1
356+
And STDERR should contain:
357+
"""
358+
Image file not found: 0
359+
"""

‎src/AI_Command.php‎

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class AI_Command extends WP_CLI_Command {
7777
* [--system-instruction=<instruction>]
7878
* : System instruction to guide the AI's behavior.
7979
*
80+
* [--image=<image>]
81+
* : An image to use as input for text or image generation. Can be a local file path, a URL, a data URI, or a WordPress attachment ID.
82+
*
8083
* [--destination-file=<file>]
8184
* : For image generation, path to save the generated image.
8285
*
@@ -109,11 +112,20 @@ class AI_Command extends WP_CLI_Command {
109112
* # Generate with system instruction
110113
* $ wp ai generate text "Explain AI" --system-instruction="Explain as if to a 5-year-old"
111114
*
115+
* # Generate text from a prompt with an image
116+
* $ wp ai generate text "Describe this image" --image=photo.jpg
117+
*
118+
* # Generate alt text for an attachment
119+
* $ wp ai generate text "Generate alt text for this image" --image=42
120+
*
121+
* # Generate text with an image URL
122+
* $ wp ai generate text "What is in this image?" --image=https://example.com/photo.jpg
123+
*
112124
* # Generate image
113125
* $ wp ai generate image "A minimalist WordPress logo" --destination-file=wp-logo.png
114126
*
115127
* @param array{0: string, 1: string} $args Positional arguments.
116-
* @param array{model: string, provider: string, temperature: float, 'top-p': float, 'top-k': int, 'max-tokens': int, 'system-instruction': string, 'destination-file': string, stdout: bool, format: string} $assoc_args Associative arguments.
128+
* @param array{model: string, provider: string, temperature: float, 'top-p': float, 'top-k': int, 'max-tokens': int, 'system-instruction': string, image?: string, 'destination-file': string, stdout: bool, format: string} $assoc_args Associative arguments.
117129
* @return void
118130
*/
119131
public function generate( $args, $assoc_args ) {
@@ -186,8 +198,14 @@ public function generate( $args, $assoc_args ) {
186198
}
187199

188200
if ( 'text' === $type ) {
201+
if ( isset( $assoc_args['image'] ) ) {
202+
$builder = $this->with_image_input( $builder, $assoc_args['image'] );
203+
}
189204
$this->generate_text( $builder, $assoc_args );
190205
} elseif ( 'image' === $type ) {
206+
if ( isset( $assoc_args['image'] ) ) {
207+
$builder = $this->with_image_input( $builder, $assoc_args['image'] );
208+
}
191209
$this->generate_image( $builder, $assoc_args );
192210
}
193211
} catch ( \Exception $e ) {
@@ -504,4 +522,123 @@ private function generate_image( $builder, $assoc_args ) {
504522
WP_CLI::line( (string) $image_file->getDataUri() );
505523
}
506524
}
525+
526+
/**
527+
* Adds image input to the prompt builder.
528+
*
529+
* Accepts a local file path, a URL (http/https), a data URI, or a WordPress attachment ID.
530+
*
531+
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
532+
* @param string $image_input Local file path, URL, data URI, or attachment ID.
533+
* @return mixed The updated prompt builder.
534+
*
535+
* @phpstan-ignore class.notFound
536+
*/
537+
private function with_image_input( $builder, $image_input ) {
538+
// Attachment ID (positive integer string).
539+
if ( ctype_digit( $image_input ) && (int) $image_input > 0 ) {
540+
return $this->with_attachment_input( $builder, (int) $image_input );
541+
}
542+
543+
// Data URI or remote URL — pass directly to with_file().
544+
if (
545+
0 === strpos( $image_input, 'data:' ) ||
546+
0 === strpos( $image_input, 'http://' ) ||
547+
0 === strpos( $image_input, 'https://' )
548+
) {
549+
// @phpstan-ignore class.notFound
550+
return $builder->with_file( $image_input );
551+
}
552+
553+
// Local file path.
554+
return $this->with_local_file_input( $builder, $image_input );
555+
}
556+
557+
/**
558+
* Adds a WordPress attachment as image input to the prompt builder.
559+
*
560+
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
561+
* @param int $attachment_id The attachment ID.
562+
* @return mixed The updated prompt builder.
563+
*
564+
* @phpstan-ignore class.notFound
565+
*/
566+
private function with_attachment_input( $builder, $attachment_id ) {
567+
$attachment = get_post( $attachment_id );
568+
569+
if ( ! $attachment ) {
570+
WP_CLI::error( "Attachment with ID {$attachment_id} not found." );
571+
}
572+
573+
if ( 'attachment' !== $attachment->post_type ) {
574+
WP_CLI::error( "Post with ID {$attachment_id} is not an attachment." );
575+
}
576+
577+
if ( ! wp_attachment_is_image( $attachment_id ) ) {
578+
WP_CLI::error( "Attachment {$attachment_id} is not an image." );
579+
}
580+
581+
$file_path = get_attached_file( $attachment_id );
582+
583+
if ( $file_path && file_exists( $file_path ) ) {
584+
$mime_type = get_post_mime_type( $attachment_id );
585+
if ( ! $mime_type ) {
586+
WP_CLI::error( "Could not determine MIME type for attachment {$attachment_id}." );
587+
}
588+
589+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
590+
$contents = file_get_contents( $file_path );
591+
if ( false === $contents ) {
592+
WP_CLI::error( "Could not read image file for attachment {$attachment_id}." );
593+
}
594+
595+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
596+
$data_uri = 'data:' . $mime_type . ';base64,' . base64_encode( $contents );
597+
598+
// @phpstan-ignore class.notFound
599+
return $builder->with_file( $data_uri );
600+
}
601+
602+
// Fall back to the attachment URL.
603+
$image_src = wp_get_attachment_image_src( $attachment_id, 'full' );
604+
if ( $image_src && ! empty( $image_src[0] ) ) {
605+
// @phpstan-ignore class.notFound
606+
return $builder->with_file( $image_src[0] );
607+
}
608+
609+
WP_CLI::error( "Could not retrieve image for attachment {$attachment_id}." );
610+
}
611+
612+
/**
613+
* Adds a local image file as input to the prompt builder.
614+
*
615+
* @param \WordPress\AI_Client\Builders\Prompt_Builder $builder The prompt builder.
616+
* @param string $file_path The local file path.
617+
* @return mixed The updated prompt builder.
618+
*
619+
* @phpstan-ignore class.notFound
620+
*/
621+
private function with_local_file_input( $builder, $file_path ) {
622+
if ( ! file_exists( $file_path ) ) {
623+
WP_CLI::error( "Image file not found: {$file_path}" );
624+
}
625+
626+
$filetype = wp_check_filetype( $file_path );
627+
$mime_type = $filetype['type'];
628+
if ( ! $mime_type || 0 !== strpos( $mime_type, 'image/' ) ) {
629+
WP_CLI::error( "File does not appear to be an image: {$file_path}" );
630+
}
631+
632+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
633+
$contents = file_get_contents( $file_path );
634+
if ( false === $contents ) {
635+
WP_CLI::error( "Could not read image file: {$file_path}" );
636+
}
637+
638+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
639+
$data_uri = 'data:' . $mime_type . ';base64,' . base64_encode( $contents );
640+
641+
// @phpstan-ignore class.notFound
642+
return $builder->with_file( $data_uri );
643+
}
507644
}

0 commit comments

Comments
 (0)