@@ -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