load_settings(); } /** * Load service settings. */ private function load_settings() { $settings = get_option('aitm_settings', array()); $this->settings = isset($settings['ai_services'][$this->service_name]) ? $settings['ai_services'][$this->service_name] : array(); } /** * Test the connection to the Midjourney service. * * @return bool|WP_Error */ public function test_connection() { if (empty($this->settings['api_key'])) { return new WP_Error( 'aitm_midjourney_no_api_key', __('Midjourney API key is not configured.', 'ai-thumbnails-maker') ); } if (empty($this->settings['server_id'])) { return new WP_Error( 'aitm_midjourney_no_server_id', __('Discord server ID is not configured.', 'ai-thumbnails-maker') ); } if (empty($this->settings['channel_id'])) { return new WP_Error( 'aitm_midjourney_no_channel_id', __('Discord channel ID is not configured.', 'ai-thumbnails-maker') ); } // Test with a simple request $response = $this->make_api_request('imagine', array( 'prompt' => 'test image', 'aspect_ratio' => '1:1', )); if (is_wp_error($response)) { return $response; } return true; } /** * Generate a thumbnail using Midjourney. * * @param string $prompt Image generation prompt. * @param int $width Image width. * @param int $height Image height. * @param bool $crop Whether to crop the image. * @return array|WP_Error */ public function generate_thumbnail($prompt, $width, $height, $crop = false) { if (empty($this->settings['api_key'])) { return new WP_Error( 'aitm_midjourney_no_api_key', __('Midjourney API key is not configured.', 'ai-thumbnails-maker') ); } // Map dimensions to Midjourney aspect ratio $aspect_ratio = $this->map_dimensions_to_aspect_ratio($width, $height); $request_data = array( 'prompt' => $prompt, 'aspect_ratio' => $aspect_ratio, 'quality' => $this->settings['quality'] ?? '1', 'style' => $this->settings['style'] ?? 'raw', ); $response = $this->make_api_request('imagine', $request_data); if (is_wp_error($response)) { return $response; } if (empty($response['task_id'])) { return new WP_Error( 'aitm_midjourney_no_task_id', __('No task ID returned from Midjourney API.', 'ai-thumbnails-maker') ); } // Poll for completion $result = $this->poll_for_completion($response['task_id']); if (is_wp_error($result)) { return $result; } if (empty($result['image_url'])) { return new WP_Error( 'aitm_midjourney_no_image_url', __('No image URL returned from Midjourney API.', 'ai-thumbnails-maker') ); } // Download the generated image $image_data = $this->download_image($result['image_url']); if (is_wp_error($image_data)) { return $image_data; } // Resize if needed if ($crop || $width !== $this->get_aspect_ratio_width($aspect_ratio) || $height !== $this->get_aspect_ratio_height($aspect_ratio)) { $image_data = $this->resize_image($image_data, $width, $height, $crop); if (is_wp_error($image_data)) { return $image_data; } } return array( 'content' => $image_data['content'], 'width' => $width, 'height' => $height, 'mime_type' => $image_data['mime_type'], 'service' => $this->service_name, ); } /** * Get the service name. * * @return string */ public function get_service_name() { return __('Midjourney', 'ai-thumbnails-maker'); } /** * Get the service description. * * @return string */ public function get_service_description() { return __('Midjourney image generation service with artistic and creative results.', 'ai-thumbnails-maker'); } /** * Get required settings for this service. * * @return array */ public function get_required_settings() { return array( 'api_key' => __('API Key', 'ai-thumbnails-maker'), 'server_id' => __('Discord Server ID', 'ai-thumbnails-maker'), 'channel_id' => __('Discord Channel ID', 'ai-thumbnails-maker'), ); } /** * Validate service settings. * * @param array $settings Settings to validate. * @return bool|WP_Error */ public function validate_settings($settings) { if (empty($settings['api_key'])) { return new WP_Error( 'aitm_midjourney_invalid_settings', __('API key is required for Midjourney service.', 'ai-thumbnails-maker') ); } if (empty($settings['server_id'])) { return new WP_Error( 'aitm_midjourney_invalid_settings', __('Discord server ID is required for Midjourney service.', 'ai-thumbnails-maker') ); } if (empty($settings['channel_id'])) { return new WP_Error( 'aitm_midjourney_invalid_settings', __('Discord channel ID is required for Midjourney service.', 'ai-thumbnails-maker') ); } // Test the API $original_settings = $this->settings; $this->settings = $settings; $test_result = $this->test_connection(); $this->settings = $original_settings; if (is_wp_error($test_result)) { return $test_result; } return true; } /** * Get service configuration form fields. * * @return array */ public function get_config_form_fields() { return array( 'api_key' => array( 'type' => 'password', 'label' => __('Midjourney API Key', 'ai-thumbnails-maker'), 'description' => __('Enter your Midjourney API key.', 'ai-thumbnails-maker'), 'required' => true, 'value' => $this->settings['api_key'] ?? '', ), 'server_id' => array( 'type' => 'text', 'label' => __('Discord Server ID', 'ai-thumbnails-maker'), 'description' => __('Enter the Discord server ID where Midjourney bot is active.', 'ai-thumbnails-maker'), 'required' => true, 'value' => $this->settings['server_id'] ?? '', ), 'channel_id' => array( 'type' => 'text', 'label' => __('Discord Channel ID', 'ai-thumbnails-maker'), 'description' => __('Enter the Discord channel ID where Midjourney bot is active.', 'ai-thumbnails-maker'), 'required' => true, 'value' => $this->settings['channel_id'] ?? '', ), 'quality' => array( 'type' => 'select', 'label' => __('Image Quality', 'ai-thumbnails-maker'), 'description' => __('Choose the quality of generated images.', 'ai-thumbnails-maker'), 'options' => array( '0.5' => __('Half Quality', 'ai-thumbnails-maker'), '1' => __('Standard Quality', 'ai-thumbnails-maker'), '2' => __('High Quality', 'ai-thumbnails-maker'), ), 'value' => $this->settings['quality'] ?? '1', ), 'style' => array( 'type' => 'select', 'label' => __('Image Style', 'ai-thumbnails-maker'), 'description' => __('Choose the style of generated images.', 'ai-thumbnails-maker'), 'options' => array( 'raw' => __('Raw', 'ai-thumbnails-maker'), 'cute' => __('Cute', 'ai-thumbnails-maker'), 'expressive' => __('Expressive', 'ai-thumbnails-maker'), 'original' => __('Original', 'ai-thumbnails-maker'), 'scenic' => __('Scenic', 'ai-thumbnails-maker'), ), 'value' => $this->settings['style'] ?? 'raw', ), ); } /** * Make API request to Midjourney. * * @param string $endpoint API endpoint. * @param array $data Request data. * @return array|WP_Error */ private function make_api_request($endpoint, $data) { $api_url = 'https://api.midjourney.com/v1/' . $endpoint; $args = array( 'method' => 'POST', 'headers' => array( 'Authorization' => 'Bearer ' . $this->settings['api_key'], 'Content-Type' => 'application/json', ), 'body' => wp_json_encode($data), 'timeout' => 60, ); $response = wp_remote_request($api_url, $args); if (is_wp_error($response)) { return $response; } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); if ($response_code !== 200) { $error_data = json_decode($response_body, true); $error_message = isset($error_data['error']['message']) ? $error_data['error']['message'] : __('Unknown API error.', 'ai-thumbnails-maker'); return new WP_Error( 'aitm_midjourney_api_error', // translators: %s is the error message from Midjourney API sprintf(__('Midjourney API error: %s', 'ai-thumbnails-maker'), $error_message), array('status' => $response_code) ); } return json_decode($response_body, true); } /** * Poll for task completion. * * @param string $task_id Task ID. * @return array|WP_Error */ private function poll_for_completion($task_id) { $max_attempts = 30; // 5 minutes with 10-second intervals $attempt = 0; while ($attempt < $max_attempts) { $response = $this->make_api_request('task/' . $task_id, array()); if (is_wp_error($response)) { return $response; } if ($response['status'] === 'completed') { return $response; } if ($response['status'] === 'failed') { return new WP_Error( 'aitm_midjourney_task_failed', __('Midjourney task failed to complete.', 'ai-thumbnails-maker') ); } sleep(10); // Wait 10 seconds before next poll $attempt++; } return new WP_Error( 'aitm_midjourney_timeout', __('Midjourney task timed out.', 'ai-thumbnails-maker') ); } /** * Map dimensions to Midjourney aspect ratio. * * @param int $width Image width. * @param int $height Image height. * @return string */ private function map_dimensions_to_aspect_ratio($width, $height) { $ratio = $width / $height; // Midjourney supported aspect ratios $ratios = array( '1:1' => 1.0, '16:9' => 16/9, '9:16' => 9/16, '4:3' => 4/3, '3:4' => 3/4, '2:3' => 2/3, '3:2' => 3/2, ); $closest_ratio = '1:1'; $min_diff = PHP_INT_MAX; foreach ($ratios as $ratio_name => $ratio_value) { $diff = abs($ratio - $ratio_value); if ($diff < $min_diff) { $min_diff = $diff; $closest_ratio = $ratio_name; } } return $closest_ratio; } /** * Get width for an aspect ratio. * * @param string $aspect_ratio Aspect ratio string. * @return int */ private function get_aspect_ratio_width($aspect_ratio) { $dimensions = explode(':', $aspect_ratio); return isset($dimensions[0]) ? (int) $dimensions[0] * 256 : 1024; } /** * Get height for an aspect ratio. * * @param string $aspect_ratio Aspect ratio string. * @return int */ private function get_aspect_ratio_height($aspect_ratio) { $dimensions = explode(':', $aspect_ratio); return isset($dimensions[1]) ? (int) $dimensions[1] * 256 : 1024; } /** * Download image from URL. * * @param string $url Image URL. * @return array|WP_Error */ private function download_image($url) { $response = wp_remote_get($url, array('timeout' => 30)); if (is_wp_error($response)) { return $response; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { return new WP_Error( 'aitm_midjourney_download_failed', __('Failed to download generated image.', 'ai-thumbnails-maker') ); } $content = wp_remote_retrieve_body($response); $content_type = wp_remote_retrieve_header($response, 'content-type'); return array( 'content' => $content, 'mime_type' => $content_type ?: 'image/png', ); } /** * Resize image to exact dimensions. * * @param array $image_data Image data. * @param int $width Target width. * @param int $height Target height. * @param bool $crop Whether to crop. * @return array|WP_Error */ private function resize_image($image_data, $width, $height, $crop) { // Create a temporary file $temp_file = wp_tempnam(); file_put_contents($temp_file, $image_data['content']); // Load image editor $editor = wp_get_image_editor($temp_file); if (is_wp_error($editor)) { wp_delete_file($temp_file); return $editor; } // Resize image $resize_result = $editor->resize($width, $height, $crop); if (is_wp_error($resize_result)) { wp_delete_file($temp_file); return $resize_result; } // Save resized image $save_result = $editor->save(); if (is_wp_error($save_result)) { wp_delete_file($temp_file); return $save_result; } // Read resized content $resized_content = file_get_contents($save_result['path']); // Clean up temporary files wp_delete_file($temp_file); wp_delete_file($save_result['path']); return array( 'content' => $resized_content, 'mime_type' => $image_data['mime_type'], ); } }