- 新增阿里云OSS SDK核心类库文件 - 新增OSS插件配置文件及钩子监听 - 新增分类图标SVG资源并优化为轻量化格式 - 替换定位接口为天地图逆地理编码接口 - 新增OSS图片缩略处理工具方法
247 行
7.5 KiB
PHP
247 行
7.5 KiB
PHP
<?php
|
||
/**
|
||
* GeekNow (gpt-image-2.6-flare @ 0.04元) 全量保持 1024x1024 高清原图与前期完全统一规范入库
|
||
*
|
||
* @return void
|
||
*/
|
||
|
||
set_time_limit(0);
|
||
ini_set('memory_limit', '512M');
|
||
|
||
$apiKey = 'sk-60nh0N9F0qMBbrCmhEg37b0SPngQtflvwNSF2h0KfjC9AwHM';
|
||
$apiUrl = 'https://geeknow.ai/v1/images/generations';
|
||
$downloadDir = dirname(__DIR__) . '/tmp_img/downloaded';
|
||
if (!is_dir($downloadDir)) {
|
||
@mkdir($downloadDir, 0755, true);
|
||
}
|
||
|
||
// 活禽畜类映射
|
||
$liveAnimals = [
|
||
'牛肉' => 'live cattle herd',
|
||
'高山牛肉' => 'live mountain yellow cattle herd',
|
||
'牦牛肉' => 'live yak herd',
|
||
'羊肉' => 'live sheep and goats flock',
|
||
'黄羊肉' => 'live yellow goats flock',
|
||
'黑山羊' => 'live black goats flock',
|
||
'生鲜猪肉' => 'free-range live pigs group',
|
||
'散养土鸡' => 'free-range live chickens flock',
|
||
'麻鸭' => 'live ducks flock near water',
|
||
'鹅肉' => 'live geese flock in pasture',
|
||
'鸽子肉' => 'live white pigeons flock',
|
||
'驴肉' => 'live healthy donkeys',
|
||
'马肉' => 'live horses galloping on pasture'
|
||
];
|
||
|
||
$eggs = [
|
||
'鲜土鸡蛋' => 'farm-fresh brown eggs',
|
||
'土鸭蛋' => 'fresh duck eggs',
|
||
'土鹅蛋' => 'fresh goose eggs',
|
||
'鹌鹑蛋' => 'speckled quail eggs'
|
||
];
|
||
|
||
$processedMeat = [
|
||
'腊肉腊肠' => 'cured pork and Chinese sausages',
|
||
'毛肚黄喉' => 'fresh beef tripe and aorta cuts',
|
||
'牛蹄筋' => 'fresh beef tendon cuts',
|
||
'牛杂' => 'fresh beef offal cuts',
|
||
'羊肉卷' => 'sliced fresh mutton rolls'
|
||
];
|
||
|
||
// 加载中英提示词字典
|
||
$mapFile = dirname(__DIR__) . '/tmp_img/map.json';
|
||
$map = json_decode(file_get_contents($mapFile), true);
|
||
$vegeHints = $map['vege_en_hint'] ?? [];
|
||
|
||
/**
|
||
* 获取 PDO 连接 (支持自动重连)
|
||
*
|
||
* @return PDO
|
||
*/
|
||
function getPdo()
|
||
{
|
||
static $pdo = null;
|
||
try {
|
||
if ($pdo) {
|
||
$pdo->query("SELECT 1");
|
||
return $pdo;
|
||
}
|
||
} catch (\Exception $e) {
|
||
$pdo = null;
|
||
}
|
||
|
||
$pdo = new PDO('mysql:host=127.0.0.1;port=3307;dbname=fenleixinxi;charset=utf8mb4', 'fenleixinxi', 'fenleixinxi', [
|
||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||
]);
|
||
return $pdo;
|
||
}
|
||
|
||
/**
|
||
* 构建提示词
|
||
*
|
||
* @param array $cat
|
||
* @return string
|
||
*/
|
||
function buildPromptForGeekNow($cat)
|
||
{
|
||
global $liveAnimals, $eggs, $processedMeat, $vegeHints;
|
||
$name = $cat['name'];
|
||
$kind = $cat['kind'];
|
||
$noText = ', 1:1 square, close-up realistic photography, clean shot, no text, no watermark, no logo, no labels, no borders';
|
||
|
||
if ($kind === 'farm') {
|
||
$en = $vegeHints[$name] ?? $name;
|
||
return "a rustic woven bamboo basket full of fresh $en, placed on soil of $en farm, background with rows of naturally growing $en plants, natural daylight, bright fresh mint green mood$noText";
|
||
}
|
||
|
||
if (isset($liveAnimals[$name])) {
|
||
$animal = $liveAnimals[$name];
|
||
return "a herd of $animal grazing naturally on green grass pasture under blue sky and hills, natural daylight, bright fresh mint green mood, realistic photography style, wide shot, 1:1 square$noText";
|
||
}
|
||
|
||
if (isset($eggs[$name])) {
|
||
$eggEn = $eggs[$name];
|
||
return "a rustic woven bamboo basket full of $eggEn with straw and green leaf decoration, on a wooden farmhouse table, warm natural sunlight$noText";
|
||
}
|
||
|
||
if ($name === '鲜活水产') {
|
||
return "a clear natural fish pond with live swimming fishes among green waterweeds, ripples, natural daylight, fresh mint green mood$noText";
|
||
}
|
||
|
||
if (isset($processedMeat[$name])) {
|
||
$meatEn = $processedMeat[$name];
|
||
return "fresh $meatEn neatly arranged on a rustic farmhouse wooden table, natural daylight, close-up realistic photography$noText";
|
||
}
|
||
|
||
$en = $vegeHints[$name] ?? $name;
|
||
return "a rustic woven bamboo basket full of fresh $en, natural daylight, realistic photography$noText";
|
||
}
|
||
|
||
/**
|
||
* 调用 GeekNow 生成单张图片
|
||
*
|
||
* @param string $prompt
|
||
* @param string $apiKey
|
||
* @param string $apiUrl
|
||
* @return string|false
|
||
*/
|
||
function callGeekNow($prompt, $apiKey, $apiUrl)
|
||
{
|
||
$body = [
|
||
'model' => 'gpt-image-2.6-flare',
|
||
'prompt' => $prompt,
|
||
'n' => 1,
|
||
'size' => '1024x1024',
|
||
'quality' => 'auto',
|
||
'watermark' => false,
|
||
'response_format' => 'url'
|
||
];
|
||
|
||
for ($i = 0; $i < 2; $i++) {
|
||
$ch = curl_init($apiUrl);
|
||
curl_setopt_array($ch, [
|
||
CURLOPT_POST => true,
|
||
CURLOPT_POSTFIELDS => json_encode($body, JSON_UNESCAPED_UNICODE),
|
||
CURLOPT_HTTPHEADER => [
|
||
'Authorization: Bearer ' . $apiKey,
|
||
'Content-Type: application/json'
|
||
],
|
||
CURLOPT_RETURNTRANSFER => true,
|
||
CURLOPT_TIMEOUT => 90
|
||
]);
|
||
$resp = curl_exec($ch);
|
||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
curl_close($ch);
|
||
|
||
if ($code === 200) {
|
||
$json = json_decode($resp, true);
|
||
$url = $json['data'][0]['url'] ?? false;
|
||
if ($url) return $url;
|
||
}
|
||
sleep(2);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 直接下载 1024x1024 高清大图 (与之前规范 100% 统一)
|
||
*
|
||
* @param string $url
|
||
* @param string $destJpg
|
||
* @return bool
|
||
*/
|
||
function downloadHdImage($url, $destJpg)
|
||
{
|
||
$fp = fopen($destJpg, 'wb');
|
||
if (!$fp) return false;
|
||
|
||
$ch = curl_init($url);
|
||
curl_setopt_array($ch, [
|
||
CURLOPT_FILE => $fp,
|
||
CURLOPT_TIMEOUT => 60,
|
||
CURLOPT_FOLLOWLOCATION => true
|
||
]);
|
||
$ok = curl_exec($ch);
|
||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
curl_close($ch);
|
||
fclose($fp);
|
||
|
||
return ($ok && $code === 200 && filesize($destJpg) > 50000);
|
||
}
|
||
|
||
// ======================== 主执行循环 ========================
|
||
echo "========================================================\n";
|
||
echo " GeekNow (gpt-image-2.6-flare @ 0.04元) 1024x1024 高清统一入库\n";
|
||
echo "========================================================\n";
|
||
|
||
$phpBin = 'D:\Softwares\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe';
|
||
|
||
while (true) {
|
||
$pdo = getPdo();
|
||
$stmt = $pdo->query("
|
||
SELECT c.id, c.pid, c.name, c.kind
|
||
FROM fa_fc_category c
|
||
LEFT JOIN fa_attachment a ON a.storage = 'alioss' AND a.url = c.image AND c.image != ''
|
||
WHERE c.pid > 0 AND (c.image = '' OR a.id IS NULL)
|
||
ORDER BY c.pid ASC, c.sort DESC, c.id ASC
|
||
");
|
||
$pending = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
$totalPending = count($pending);
|
||
|
||
if ($totalPending === 0) {
|
||
echo "\n🎉 恭喜!全站所有二级分类已全部完成配图并入库阿里云 OSS!\n";
|
||
break;
|
||
}
|
||
|
||
echo sprintf("\n剩余待处理: %d 项\n", $totalPending);
|
||
$cat = $pending[0];
|
||
$id = $cat['id'];
|
||
$name = $cat['name'];
|
||
|
||
$prompt = buildPromptForGeekNow($cat);
|
||
echo " 正在生成 #$id $name (1024x1024高清 @ 0.04元)...";
|
||
|
||
$imgUrl = callGeekNow($prompt, $apiKey, $apiUrl);
|
||
if ($imgUrl) {
|
||
$destFile = $downloadDir . '/' . $id . '.jpg';
|
||
if (downloadHdImage($imgUrl, $destFile)) {
|
||
$sz = round(filesize($destFile) / 1024);
|
||
echo " [高清大图下载成功: {$sz}KB] => 推流OSS...";
|
||
|
||
// 立即入库 OSS
|
||
$output = [];
|
||
$returnVar = 0;
|
||
exec("\"$phpBin\" think fc:category-images", $output, $returnVar);
|
||
echo " [已入库]\n";
|
||
} else {
|
||
echo " [下载失败]\n";
|
||
}
|
||
} else {
|
||
echo " [生图失败]\n";
|
||
}
|
||
|
||
usleep(500000); // 500ms 间隔
|
||
}
|
||
|
||
echo "\n全部分类配图生成与入库完成!\n";
|