= 2 && $v[0] === $v[-1] && ($v[0] === '"' || $v[0] === "'")) $v = substr($v, 1, -1); $o[trim($k)] = $v; } return $o; } function _img(string $key, string $prompt, string $dest): string { $prompt .= ' Photorealistic. No text, words, letters, logos, or watermarks.'; $payload = json_encode(['contents' => [['parts' => [['text' => $prompt]]]], 'generationConfig' => ['responseModalities' => ['TEXT', 'IMAGE'], 'imageConfig' => ['aspectRatio' => '16:9', 'imageSize' => '2K']]]); $lastResp = ''; for ($a = 1; $a <= 2; $a++) { $ctx = stream_context_create(['http' => ['method' => 'POST', 'header' => "Content-Type: application/json\r\nx-goog-api-key: {$key}\r\n", 'content' => $payload, 'timeout' => 120, 'ignore_errors' => true]]); $r = @file_get_contents('https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-preview-image-generation:generateContent', false, $ctx); if ($r === false) { sleep(2); continue; } $lastResp = substr((string)$r, 0, 500); $d = json_decode($r, true); foreach (($d['candidates'] ?? []) as $c) { foreach (($c['content']['parts'] ?? []) as $p) { if (isset($p['inlineData']['data'])) { $bin = base64_decode($p['inlineData']['data'], true); if ($bin && strlen($bin) > 1000) { file_put_contents($dest, $bin); chmod($dest, 0664); return 'ok:' . strlen($bin); } } } } sleep(2); } return 'error:' . $lastResp; } set_time_limit(600); header('Content-Type: application/json'); $env = _env(dirname(__DIR__, 2) . '/.env'); $key = $env['GEMINI_API_KEY'] ?? ''; if (!$key) { echo json_encode(['error' => 'no GEMINI_API_KEY in .env']); exit; } $dir = dirname(__DIR__, 2) . '/assets/images/guides/thanksgiving-in-destin/'; if (!is_dir($dir)) mkdir($dir, 0755, true); $imgs = [ 'hero' => 'Aerial view of Destin Florida emerald Gulf coastline on a warm sunny late November afternoon, just a few families scattered across the wide uncrowded white sand beach, calm turquoise water, golden autumn afternoon light, peaceful and serene off-season beach', 'beach-weather' => 'Couple in light sweaters and jeans walking barefoot together on a nearly empty white sand beach in Destin Florida on a sunny late November day, calm emerald green Gulf water, warm golden low-angle light, a few seagulls, peaceful fall coastal beach atmosphere', 'whats-open' => 'Interior of a warm inviting waterfront seafood restaurant in Destin Florida with subtle fall harvest decorations and warm amber pendant lighting, families and couples enjoying fresh Gulf seafood and glasses of wine, large windows overlooking the harbor, rustic wooden coastal decor', 'dinner' => 'Large extended multigenerational family gathered around a long beautifully set Thanksgiving dinner table inside a spacious bright Florida beach vacation rental house, a golden roasted turkey as the centerpiece with colorful side dishes, warm candlelight, turquoise Gulf water visible through large windows, everyone smiling and relaxed', 'activities' => 'Group of happy adults and kids standing on a fishing charter boat in the calm emerald Gulf of Mexico near Destin Florida on a warm clear November morning, holding up large redfish and grouper catches, beautiful blue-green water, sunshine, a few pelicans nearby', 'rental' => 'Beautiful modern vacation rental beach house in Miramar Beach Florida photographed on a warm sunny November afternoon, sparkling private pool with clear blue water surrounded by lush tropical landscaping, comfortable outdoor lounge chairs and a dining table on the covered patio, golden sunlight filtering through palm trees', ]; $n = $_GET['img'] ?? ''; if ($n !== '' && isset($imgs[$n])) { $dest = $dir . $n . '.jpg'; if (file_exists($dest) && filesize($dest) > 10000) { echo json_encode(['result' => 'skipped', 'img' => $n]); exit; } echo json_encode(['result' => _img($key, $imgs[$n], $dest), 'img' => $n]); exit; } $res = []; foreach ($imgs as $nm => $p) { $dest = $dir . $nm . '.jpg'; if (file_exists($dest) && filesize($dest) > 10000) { $res[$nm] = 'skipped'; continue; } $res[$nm] = _img($key, $p, $dest); } echo json_encode(['results' => $res]);