100KB 且不等于 176626 * * 用法: php tmp_img/gen_images.php */ $Error = 0; $OK = 0; $SKIP = 0; $PLACEHOLDER_SIZE = 176626; // API 占位图固定大小 $MIN_REAL_SIZE = 100000; // 真实图至少 100KB $root = __DIR__ . '/..'; $srcFile = __DIR__ . '/categories.txt'; $mapFile = __DIR__ . '/map.json'; $outDir = __DIR__ . '/downloaded'; $idxFile = __DIR__ . '/gen_index.json'; @mkdir($outDir, 0755, true); // ---- 加载 map.json ---- $map = json_decode(file_get_contents($mapFile), true); $hint = $map['vege_en_hint'] ?? []; $animals = $map['live_animal_map'] ?? []; $eggs = $map['egg_list'] ?? []; $waters = $map['water_list'] ?? []; $processed = []; if (file_exists($idxFile)) { $raw = file_get_contents($idxFile); if ($raw && $raw !== '{}') { $decoded = json_decode($raw, true); if (is_array($decoded)) $processed = $decoded; } } $NO_TEXT = ', no text, no watermark, no logo, no signature, no artist tag, no caption, no brand mark, no subtitle, no embedded letters, no corner badge, no AI generated label, no copyright mark, no overlay text'; function vegeEn($name) { global $hint; return $hint[$name] ?? $name; } function liveAnimalEn($name) { global $animals; return $animals[$name] ?? null; } function buildPrompt($name, $kind) { global $NO_TEXT, $eggs, $waters; if ($kind === 'farm') { $en = vegeEn($name); return "a bamboo basket full of fresh $en, placed on the soil of a $en farm or orchard field, background with rows of naturally growing $en plants or trees, natural daylight, bright fresh mint green mood, realistic photography style, close-up composition, 1:1 square$NO_TEXT"; } // general: 肉蛋家禽 $en = liveAnimalEn($name); if ($en) { return "a herd of $en grazing or standing naturally on a green grass pasture or farmhouse courtyard, background with distant hills and blue sky, natural daylight, bright fresh mint green mood, realistic photography style, wide shot, 1:1 square composition$NO_TEXT"; } if (in_array($name, $eggs)) { $en = vegeEn($name); return "a bamboo basket full of fresh $en with some green plants and straw decoration, placed on a rustic wooden table in a sunny farmhouse courtyard, natural daylight, warm pastoral mood with fresh mint green undertone, realistic photography, 1:1 square$NO_TEXT"; } if (in_array($name, $waters)) { return "a natural fish pond with various live fish swimming in clear blue water, surrounded by green grass and reed, natural daylight, fresh mint green mood, realistic photography, 1:1 square composition$NO_TEXT"; } // 默认也用 farm prompt $en = vegeEn($name); return "a bamboo basket full of fresh $en, natural daylight, bright fresh mint green mood, realistic photography style, 1:1 square$NO_TEXT"; } /** * 下载图片(带占位图重试) */ function downloadImage($prompt, $outFile) { global $PLACEHOLDER_SIZE, $MIN_REAL_SIZE; $url = 'https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image'; $body = json_encode(['prompt' => $prompt, 'image_size' => 'square_hd']); $delays = [20, 40, 60, 80, 100, 120]; foreach ($delays as $attempt => $waitSec) { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_RETURNTRANSFER => false, CURLOPT_FILE => fopen($outFile, 'wb'), CURLOPT_TIMEOUT => 180, // 单次请求最多 3 分钟 CURLOPT_FOLLOWLOCATION => true, ]); $ok = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $err = curl_error($ch); curl_close($ch); if (!$ok || $httpCode !== 200) { echo " [HTTP $httpCode] err=$err, wait {$waitSec}s retry...\n"; sleep($waitSec); continue; } $sz = filesize($outFile); if ($sz === false || $sz < $MIN_REAL_SIZE) { echo " [too small $sz], wait {$waitSec}s retry...\n"; sleep($waitSec); continue; } if ($sz === $PLACEHOLDER_SIZE) { echo " [placeholder], wait {$waitSec}s retry...\n"; sleep($waitSec); continue; } // 真实图! return $sz; } return false; } // ---- 解析 categories.txt ---- $lines = file($srcFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $cats = []; foreach ($lines as $line) { if (!preg_match('/^\d+\t/', $line)) continue; $parts = explode("\t", $line); if (count($parts) < 4) continue; $id = (int)$parts[0]; $pid = (int)$parts[1]; if ($pid === 0) continue; $cats[$id] = [ 'id' => $id, 'pid' => $pid, 'name' => trim($parts[2]), 'kind' => trim($parts[3]), ]; } $total = count($cats); echo "TOTAL_SECONDARY=$total\n"; // ---- 主循环 ---- foreach ($cats as $id => $cat) { $key = (string)$id; $outFile = "$outDir/$id.jpg"; // 1) 已在 idx 且文件是真实图 → 跳过 $skipGood = false; if (isset($processed[$key]) && file_exists($outFile)) { $sz = filesize($outFile); if ($sz > $MIN_REAL_SIZE && $sz !== $PLACEHOLDER_SIZE) { $SKIP++; continue; } } // 2) 若文件是占位图 → 删除重下 if (file_exists($outFile)) { $sz = filesize($outFile); if ($sz === $PLACEHOLDER_SIZE || $sz < $MIN_REAL_SIZE) { @unlink($outFile); } else { // 已经是真实图 $SKIP++; continue; } } $prompt = buildPrompt($cat['name'], $cat['kind']); echo "GEN #$id {$cat['name']} ({$cat['kind']})...\n"; $sz = downloadImage($prompt, $outFile); if ($sz === false) { echo " FAIL after all retries\n"; $Error++; } else { echo " OK size=$sz\n"; $processed[$key] = [ 'name' => $cat['name'], 'pid' => $cat['pid'], 'file' => $outFile, 'size' => $sz, ]; file_put_contents($idxFile, json_encode($processed, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); $OK++; if (($OK + $SKIP) % 20 === 0) { echo "PROGRESS ok=$OK skip=$SKIP fail=$Error total=$total\n"; } } usleep(200000); // 200ms 间隔 } echo "\n=== DONE ===\n"; echo "OK=$OK SKIP=$SKIP FAIL=$Error TOTAL=$total\n"; // 最终校验 $all = glob("$outDir/*.jpg"); $placeholderCount = 0; foreach ($all as $f) { if (filesize($f) === $PLACEHOLDER_SIZE) $placeholderCount++; } echo "最终校验: 总文件=" . count($all) . " 占位图=$placeholderCount\n";