0) { $t = " "; while ($t != "\n") { if (fseek($handle, $pos, SEEK_END) == -1) { $beginning = true; break; } $t = fgetc($handle); $pos--; } $linecounter--; if ($beginning) { rewind($handle); } $text[$lines - $linecounter - 1] = fgets($handle); if ($beginning) break; } fclose($handle); return array_reverse($text); } // Image Testing content ob_start(); // Handle Form Submit $image_url = ''; $response_error = ''; $save_path = ''; $generated_image_data = null; // Handle saving prompt settings if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_prompt_nonce']) && wp_verify_nonce($_POST['save_prompt_nonce'], 'save_prompt')) { $prompt_template = sanitize_textarea_field(wp_unslash($_POST['prompt_template'])); update_option('igny8_image_prompt_template', $prompt_template); $settings_saved = true; } // Handle image generation if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate_image_nonce']) && wp_verify_nonce($_POST['generate_image_nonce'], 'generate_image')) { $title = sanitize_text_field(wp_unslash($_POST['title'])); $desc = sanitize_textarea_field(wp_unslash($_POST['desc'])); $image_type = sanitize_text_field(wp_unslash($_POST['image_type'])); $image_provider = sanitize_text_field(wp_unslash($_POST['image_provider'] ?? 'openai')); $negative_prompt = sanitize_textarea_field(wp_unslash($_POST['negative_prompt'] ?? 'text, watermark, logo, overlay, title, caption, writing on walls, writing on objects, UI, infographic elements, post title')); $image_width = intval($_POST['image_width'] ?? 1024); $image_height = intval($_POST['image_height'] ?? 1024); $image_format = sanitize_text_field(wp_unslash($_POST['image_format'] ?? 'jpg')); // Get custom prompt template or use default $prompt_template = wp_unslash(get_option('igny8_image_prompt_template', 'Generate a {image_type} image for a blog post titled "{title}". Description: {description}')); // Replace placeholders in prompt template $prompt = str_replace( ['{image_type}', '{title}', '{description}'], [$image_type, $title, $desc], $prompt_template ); // Get API keys $openai_key = get_option('igny8_api_key', ''); $runware_key = get_option('igny8_runware_api_key', ''); // Check if the required API key is configured $required_key = ($image_provider === 'runware') ? $runware_key : $openai_key; $service_name = ($image_provider === 'runware') ? 'Runware' : 'OpenAI'; if (empty($required_key)) { $response_error = $service_name . ' API key not configured. Please set your API key in the settings.'; } else { // Debug: Log the request details error_log('Igny8 Image Generation - Starting request'); error_log('Igny8 Image Generation - Prompt: ' . $prompt); error_log('Igny8 Image Generation - Provider: ' . $image_provider); error_log('Igny8 Image Generation - API Key: ' . substr($required_key, 0, 10) . '...'); try { if ($image_provider === 'runware') { // Runware API Call error_log('Igny8 Image Generation - Using Runware service'); // Prepare Runware API payload $payload = [ [ 'taskType' => 'authentication', 'apiKey' => $runware_key ], [ 'taskType' => 'imageInference', 'taskUUID' => wp_generate_uuid4(), 'positivePrompt' => $prompt, 'negativePrompt' => $negative_prompt, 'model' => 'runware:97@1', 'width' => $image_width, 'height' => $image_height, 'steps' => 30, 'CFGScale' => 7.5, 'numberResults' => 1, 'outputFormat' => $image_format ] ]; // Make API request $response = wp_remote_post('https://api.runware.ai/v1', [ 'headers' => ['Content-Type' => 'application/json'], 'body' => json_encode($payload), 'timeout' => 60 ]); if (is_wp_error($response)) { error_log('Igny8 Image Generation - Runware Error: ' . $response->get_error_message()); $response_error = 'Runware API Error: ' . $response->get_error_message(); } else { $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if (isset($response_data['data'][0]['imageURL'])) { // Download and save the image $image_url = $response_data['data'][0]['imageURL']; $filename = sanitize_file_name($title) . '_' . time() . '.' . $image_format; // Create uploads directory $upload_dir = wp_upload_dir(); $igny8_dir = $upload_dir['basedir'] . '/igny8-ai-images/'; if (!file_exists($igny8_dir)) { wp_mkdir_p($igny8_dir); } // Download image $image_response = wp_remote_get($image_url); if (is_wp_error($image_response)) { $response_error = 'Failed to download Runware image: ' . $image_response->get_error_message(); } else { $image_data = wp_remote_retrieve_body($image_response); $file_path = $igny8_dir . $filename; $saved = file_put_contents($file_path, $image_data); if ($saved === false) { $response_error = 'Failed to save Runware image file'; } else { $response_success = true; $response_image_url = str_replace(ABSPATH, home_url('/'), $file_path); $response_message = 'Image generated successfully using Runware!'; // Set generated image data for consistent display $generated_image_data = [ 'model' => 'runware:97@1', 'provider' => 'runware', 'negative_prompt' => $negative_prompt ]; error_log('Igny8 Image Generation - Runware image saved to: ' . $file_path); } } } elseif (isset($response_data['errors'][0]['message'])) { $response_error = 'Runware API Error: ' . $response_data['errors'][0]['message']; } else { $response_error = 'Unknown response from Runware API'; } } } else { // OpenAI DALL-E 3 API Call error_log('Igny8 Image Generation - Using OpenAI DALL-E 3 service'); $data = [ 'model' => 'dall-e-3', 'prompt' => $prompt, 'n' => 1, 'size' => $image_width . 'x' . $image_height ]; // Add negative prompt if supported (DALL-E 3 doesn't support negative prompts, but we'll log it) if (!empty($negative_prompt)) { error_log('Igny8 Image Generation - Negative prompt provided but DALL-E 3 does not support negative prompts: ' . $negative_prompt); } $args = [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $openai_key, 'OpenAI-Beta' => 'assistants=v2' ], 'body' => json_encode($data), 'timeout' => 60, 'sslverify' => true ]; error_log('Igny8 Image Generation - Making API request to OpenAI'); $api_response = wp_remote_post('https://api.openai.com/v1/images/generations', $args); // Debug: Log response details if (is_wp_error($api_response)) { error_log('Igny8 Image Generation - WP Error: ' . $api_response->get_error_message()); error_log('Igny8 Image Generation - Error Code: ' . $api_response->get_error_code()); $response_error = 'WordPress HTTP Error: ' . $api_response->get_error_message(); } else { $response_code = wp_remote_retrieve_response_code($api_response); $response_body = wp_remote_retrieve_body($api_response); error_log('Igny8 Image Generation - Response Code: ' . $response_code); error_log('Igny8 Image Generation - Response Body: ' . substr($response_body, 0, 500)); if ($response_code === 200) { $body = json_decode($response_body, true); if (isset($body['data'][0]['url'])) { $image_url = $body['data'][0]['url']; $generated_image_data = $body['data'][0]; error_log('Igny8 Image Generation - Image URL received: ' . $image_url); // Log successful image generation global $wpdb; $wpdb->insert($wpdb->prefix . 'igny8_logs', [ 'level' => 'info', 'message' => 'Image generation successful', 'context' => wp_json_encode([ 'model' => 'dall-e-3', 'prompt_length' => strlen($prompt), 'image_size' => '1024x1024', 'total_cost' => 0.040, // DALL-E 3 standard cost 'title' => $title, 'image_type' => $image_type ]), 'source' => 'openai_image', 'status' => 'success', 'api_id' => $body['data'][0]['id'] ?? null, 'user_id' => get_current_user_id(), 'created_at' => current_time('mysql') ], ['%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s']); // Try to save to plugin directory first, fallback to WordPress uploads $plugin_upload_dir = plugin_dir_path(__FILE__) . '../../assets/ai-images/'; $wp_upload_dir = wp_upload_dir(); $wp_upload_path = $wp_upload_dir['basedir'] . '/igny8-ai-images/'; $slug = sanitize_title($title); $upload_dir = $plugin_upload_dir; $folder = $upload_dir . $slug . '/'; $save_location = 'plugin'; // Check if plugin directory is writable if (!file_exists($plugin_upload_dir)) { if (wp_mkdir_p($plugin_upload_dir)) { error_log('Igny8 Image Generation - Created plugin directory: ' . $plugin_upload_dir); } else { error_log('Igny8 Image Generation - Failed to create plugin directory: ' . $plugin_upload_dir); // Fallback to WordPress uploads directory $upload_dir = $wp_upload_path; $folder = $upload_dir . $slug . '/'; $save_location = 'wordpress'; error_log('Igny8 Image Generation - Using WordPress uploads as fallback: ' . $wp_upload_path); } } if (file_exists($plugin_upload_dir) && !is_writable($plugin_upload_dir)) { // Try to fix permissions chmod($plugin_upload_dir, 0755); error_log('Igny8 Image Generation - Attempted to fix plugin directory permissions: ' . $plugin_upload_dir); if (!is_writable($plugin_upload_dir)) { // Fallback to WordPress uploads directory $upload_dir = $wp_upload_path; $folder = $upload_dir . $slug . '/'; $save_location = 'wordpress'; error_log('Igny8 Image Generation - Plugin directory not writable, using WordPress uploads: ' . $wp_upload_path); } } // Ensure target directory exists if (!file_exists($folder)) { wp_mkdir_p($folder); error_log('Igny8 Image Generation - Created directory: ' . $folder); } // Final check if directory is writable if (file_exists($folder) && !is_writable($folder)) { // Try to fix permissions one more time chmod($folder, 0755); error_log('Igny8 Image Generation - Attempted to fix final permissions for: ' . $folder); if (!is_writable($folder)) { $response_error = 'Directory is not writable. Please check file permissions for: ' . $folder; error_log('Igny8 Image Generation - Directory still not writable: ' . $folder); error_log('Igny8 Image Generation - Directory permissions: ' . substr(sprintf('%o', fileperms($folder)), -4)); } } elseif (!file_exists($folder)) { $response_error = 'Directory does not exist and could not be created: ' . $folder; error_log('Igny8 Image Generation - Directory does not exist: ' . $folder); } if (empty($response_error)) { // Download image data $image_data = file_get_contents($image_url); if ($image_data === false) { $response_error = 'Failed to download image from URL: ' . $image_url; error_log('Igny8 Image Generation - Failed to download image from: ' . $image_url); } else { $filename = $folder . 'image.png'; // Try to write the file $bytes_written = file_put_contents($filename, $image_data); if ($bytes_written === false) { $response_error = 'Failed to save image file. Check file permissions.'; error_log('Igny8 Image Generation - Failed to write file: ' . $filename); error_log('Igny8 Image Generation - Directory permissions: ' . substr(sprintf('%o', fileperms($folder)), -4)); error_log('Igny8 Image Generation - Directory writable: ' . (is_writable($folder) ? 'yes' : 'no')); } else { // Determine the correct path for display if ($save_location === 'wordpress') { $display_path = '/wp-content/uploads/igny8-ai-images/' . $slug . '/image.png'; $log_path = $display_path; } else { $display_path = '/assets/ai-images/' . $slug . '/image.png'; $log_path = $display_path; } $save_path = 'Saved to: ' . $display_path . ' (' . $bytes_written . ' bytes)'; error_log('Igny8 Image Generation - Image saved successfully: ' . $filename . ' (' . $bytes_written . ' bytes)'); error_log('Igny8 Image Generation - Save location: ' . $save_location); // Update log with file path $wpdb->update( $wpdb->prefix . 'igny8_logs', [ 'context' => wp_json_encode([ 'model' => 'dall-e-3', 'prompt_length' => strlen($prompt), 'image_size' => '1024x1024', 'total_cost' => 0.040, 'title' => $title, 'image_type' => $image_type, 'file_path' => $log_path, 'file_size' => $bytes_written, 'save_location' => $save_location ]) ], ['api_id' => $body['data'][0]['id'] ?? null], ['%s'], ['%s'] ); } } } } else { $response_error = 'Image URL not returned from API. Response: ' . substr($response_body, 0, 200); if (isset($body['error']['message'])) { $response_error .= ' Error: ' . $body['error']['message']; } // Log failed image generation global $wpdb; $wpdb->insert($wpdb->prefix . 'igny8_logs', [ 'level' => 'error', 'message' => 'Image generation failed - no URL returned', 'context' => wp_json_encode([ 'model' => 'dall-e-3', 'prompt_length' => strlen($prompt), 'image_size' => '1024x1024', 'error_response' => substr($response_body, 0, 500), 'title' => $title, 'image_type' => $image_type ]), 'source' => 'openai_image', 'status' => 'error', 'user_id' => get_current_user_id(), 'created_at' => current_time('mysql') ], ['%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s']); } } else { $response_error = 'API returned error code ' . $response_code . '. Response: ' . substr($response_body, 0, 200); // Log failed image generation global $wpdb; $wpdb->insert($wpdb->prefix . 'igny8_logs', [ 'level' => 'error', 'message' => 'Image generation failed - HTTP error', 'context' => wp_json_encode([ 'model' => 'dall-e-3', 'prompt_length' => strlen($prompt), 'image_size' => '1024x1024', 'http_code' => $response_code, 'error_response' => substr($response_body, 0, 500), 'title' => $title, 'image_type' => $image_type ]), 'source' => 'openai_image', 'status' => 'error', 'user_id' => get_current_user_id(), 'created_at' => current_time('mysql') ], ['%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s']); } } } // End of OpenAI else block } catch (Exception $e) { error_log('Igny8 Image Generation - Exception: ' . $e->getMessage()); $response_error = 'Exception occurred: ' . $e->getMessage(); // Log exception global $wpdb; $wpdb->insert($wpdb->prefix . 'igny8_logs', [ 'level' => 'error', 'message' => 'Image generation failed - exception', 'context' => wp_json_encode([ 'model' => 'dall-e-3', 'prompt_length' => strlen($prompt), 'image_size' => '1024x1024', 'exception_message' => $e->getMessage(), 'title' => $title, 'image_type' => $image_type ]), 'source' => 'openai_image', 'status' => 'error', 'user_id' => get_current_user_id(), 'created_at' => current_time('mysql') ], ['%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s']); } } } ?>

AI Image Generation Test

Test OpenAI DALL·E 3 image generation with your content data

Generate AI-powered images for your content using OpenAI's DALL·E 3. Enter your post details below to create custom images based on your title, description, keywords, and cluster information.

Generate Image

Configure image generation parameters

Describe the visual elements, style, mood, and composition you want in the image.
Specify elements to avoid in the generated image (text, watermarks, logos, etc.).
Choose the AI image generation service to use.
Choose the image dimensions for your generated image.
Choose the image file format for your generated image.

Generated Image

AI-generated image results

Generated Image

Image Details

  • Size: pixels
  • Format:
  • Model:
  • Revised Prompt:
  • Negative Prompt:

Generation Failed

No image generated yet. Fill out the form and click "Generate Image" to create your first AI image.

Prompt Settings

Customize your image generation prompt template

Prompt template saved successfully!
Available placeholders:
{title} - Post title
{description} - Prompt description
{image_type} - Selected image type

API Configuration

OpenAI API settings and requirements

OpenAI API Key

Your OpenAI API key is configured' : 'not set'; ?>

Configure API Key

Image Service

(1024x1024 resolution)

Image Storage

Generated images are saved to /assets/ai-images/[slug]/image.png

Usage Limits

Subject to your OpenAI account limits and billing

Debug Information

Technical details and troubleshooting

Server Information

  • PHP Version:
  • WordPress Version:
  • cURL Available:
  • SSL Support:
  • Image Save Directory:
  • Image Dir Writable:
  • WordPress Uploads:
  • WP Uploads Writable:

API Configuration

  • API Key Status:
  • API Key Length: characters
  • Prompt Template: characters

Recent Errors

'; foreach ($recent_errors as $error) { if (strpos($error, 'Igny8 Image Generation') !== false) { echo esc_html($error) . "\n"; } } echo ''; } else { echo '

No recent Igny8 errors found.

'; } } else { echo '

Error log not accessible.

'; } ?>

Test Connection

'thinker', 'subpage' => 'image-testing', 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('igny8_image_testing_settings') ]); ?>