Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
$prompt, 'n' => 1, 'size' => $width.'x'.$height, // exemplo: 853x480 'response_format' => 'url', ]; $args = [ 'body' => json_encode($data), 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 30, ]; $response = wp_remote_post($endpoint, $args); if (is_wp_error($response)) { return false; } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['data'][0]['url'])) { return $body['data'][0]['url']; } return false; } // Função para converter imagem para WebP com limite de tamanho function otimizar_webp($url_imagem, $largura_final, $altura_final, $max_size_kb = 160) { $imagem_original = file_get_contents($url_imagem); $temp_file = tempnam(sys_get_temp_dir(), 'img'); file_put_contents($temp_file, $imagem_original); $info = getimagesize($temp_file); if (!$info) { unlink($temp_file); return false; } if ($info[2] == IMAGETYPE_JPEG) { $img = imagecreatefromjpeg($temp_file); } elseif ($info[2] == IMAGETYPE_PNG) { $img = imagecreatefrompng($temp_file); } else { unlink($temp_file); return false; } // Redimensiona para largura e altura desejadas $img_redim = imagescale($img, $largura_final, $altura_final); $qualidade = 80; // qualidade inicial para compressão WebP $destino = tempnam(sys_get_temp_dir(), 'wpimg') . '.webp'; // Tenta alcançar limite de tamanho max_size_kb do { imagewebp($img_redim, $destino, $qualidade); clearstatcache(); $size_kb = filesize($destino) / 1024; $qualidade -= 5; } while ($size_kb > $max_size_kb && $qualidade > 10); imagedestroy($img); imagedestroy($img_redim); unlink($temp_file); if ($size_kb <= $max_size_kb) { return $destino; } else { unlink($destino); return false; } } // Handle form submit and image generation if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['tema_imagem'])) { $prompt = sanitize_text_field($_POST['tema_imagem']); $formato = sanitize_text_field($_POST['formato']); $url_imagem_gerada = gerar_imagem_ia($prompt, $formato); if ($url_imagem_gerada) { if ($formato == '9:16') { $largura = 480; $altura = 853; } else { $largura = 853; $altura = 480; } $imagem_webp = otimizar_webp($url_imagem_gerada, $largura, $altura, 160); if ($imagem_webp) { // Exibe imagem na página $imagem_webp_url = 'data:image/webp;base64,' . base64_encode(file_get_contents($imagem_webp)); unlink($imagem_webp); } else { $erro = "Não foi possível otimizar a imagem para WebP dentro do limite de tamanho."; } } else { $erro = "Erro ao gerar a imagem pela IA."; } } ?> Gerador de Imagem IA WebP

Gerar Imagem IA







Imagem Gerada:

Imagem gerada por IA

42 Visualizações