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 DALL-E service. * * @return bool|WP_Error */ public function test_connection() { if (empty($this->settings['api_key'])) { return new WP_Error( 'aitm_dall_e_no_api_key', __('DALL-E API key is not configured.', 'ai-thumbnails-maker') ); } // Test with a simple request $response = $this->make_api_request('images/generations', array( 'prompt' => 'test image', 'n' => 1, 'size' => '256x256', 'quality' => 'standard', )); if (is_wp_error($response)) { return $response; } return true; } /** * Generate a thumbnail using DALL-E. * * @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_dall_e_no_api_key', __('DALL-E API key is not configured.', 'ai-thumbnails-maker') ); } // Map dimensions to DALL-E supported sizes $size = $this->map_dimensions_to_dall_e_size($width, $height); $request_data = array( 'prompt' => $prompt, 'n' => 1, 'size' => $size, 'quality' => $this->settings['quality'] ?? 'standard', 'style' => $this->settings['style'] ?? 'vivid', ); $response = $this->make_api_request('images/generations', $request_data); if (is_wp_error($response)) { return $response; } if (empty($response['data'][0]['url'])) { return new WP_Error( 'aitm_dall_e_no_image_url', __('No image URL returned from DALL-E API.', 'ai-thumbnails-maker') ); } // Download the generated image $image_url = $response['data'][0]['url']; $image_data = $this->download_image($image_url); if (is_wp_error($image_data)) { return $image_data; } // Resize if needed if ($crop || $width !== $this->get_size_width($size) || $height !== $this->get_size_height($size)) { $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 __('DALL-E 3', 'ai-thumbnails-maker'); } /** * Get the service description. * * @return string */ public function get_service_description() { return __('OpenAI DALL-E 3 image generation service with high-quality 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'), ); } /** * 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_dall_e_invalid_settings', __('API key is required for DALL-E service.', 'ai-thumbnails-maker') ); } // Test the API key $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' => __('OpenAI API Key', 'ai-thumbnails-maker'), 'description' => __('Enter your OpenAI API key. You can get one from https://platform.openai.com/api-keys', 'ai-thumbnails-maker'), 'required' => true, 'value' => $this->settings['api_key'] ?? '', ), 'quality' => array( 'type' => 'select', 'label' => __('Image Quality', 'ai-thumbnails-maker'), 'description' => __('Choose the quality of generated images.', 'ai-thumbnails-maker'), 'options' => array( 'standard' => __('Standard', 'ai-thumbnails-maker'), 'hd' => __('HD', 'ai-thumbnails-maker'), ), 'value' => $this->settings['quality'] ?? 'standard', ), 'style' => array( 'type' => 'select', 'label' => __('Image Style', 'ai-thumbnails-maker'), 'description' => __('Choose the style of generated images.', 'ai-thumbnails-maker'), 'options' => array( 'vivid' => __('Vivid', 'ai-thumbnails-maker'), 'natural' => __('Natural', 'ai-thumbnails-maker'), ), 'value' => $this->settings['style'] ?? 'vivid', ), ); } /** * Make API request to DALL-E. * * @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.openai.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_dall_e_api_error', // translators: %s is the error message from DALL-E API sprintf(__('DALL-E API error: %s', 'ai-thumbnails-maker'), $error_message), array('status' => $response_code) ); } return json_decode($response_body, true); } /** * Map dimensions to DALL-E supported size. * * @param int $width Image width. * @param int $height Image height. * @return string */ private function map_dimensions_to_dall_e_size($width, $height) { // DALL-E 3 supported sizes $sizes = array( '1024x1024' => array(1024, 1024), '1024x1792' => array(1024, 1792), '1792x1024' => array(1792, 1024), ); // Find the closest size $closest_size = '1024x1024'; $min_diff = PHP_INT_MAX; foreach ($sizes as $size => $dimensions) { $diff = abs($width - $dimensions[0]) + abs($height - $dimensions[1]); if ($diff < $min_diff) { $min_diff = $diff; $closest_size = $size; } } return $closest_size; } /** * Get width for a DALL-E size. * * @param string $size DALL-E size string. * @return int */ private function get_size_width($size) { $dimensions = explode('x', $size); return isset($dimensions[0]) ? (int) $dimensions[0] : 1024; } /** * Get height for a DALL-E size. * * @param string $size DALL-E size string. * @return int */ private function get_size_height($size) { $dimensions = explode('x', $size); return isset($dimensions[1]) ? (int) $dimensions[1] : 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_dall_e_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'], ); } }