- 新增阿里云OSS SDK核心类库文件 - 新增OSS插件配置文件及钩子监听 - 新增分类图标SVG资源并优化为轻量化格式 - 替换定位接口为天地图逆地理编码接口 - 新增OSS图片缩略处理工具方法
324 行
11 KiB
PHP
324 行
11 KiB
PHP
<?php
|
||
/**
|
||
* GeekNow (gpt-image-2.6-flare @ 0.04元) 5并发高速生图与阿里云OSS批量入库脚本
|
||
*
|
||
* 特性:
|
||
* 1. 使用 curl_multi 实现 5 并发异步请求,生图效率提升 5 倍;
|
||
* 2. 保持全站 1024x1024 高清大图标准(约 2.5MB~3MB,写实摄影、无水印);
|
||
* 3. 下载完成后批量调用 think fc:category-images 推送至阿里云 OSS 并更新 fa_fc_category 与 fa_attachment;
|
||
* 4. 自动捕获失败并重试(最多 3 次),确保 100% 完整交付。
|
||
*
|
||
* @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";
|
||
}
|
||
|
||
/**
|
||
* 创建单个 curl 句柄用于 GeekNow 生图
|
||
*
|
||
* @param string $prompt
|
||
* @param string $apiKey
|
||
* @param string $apiUrl
|
||
* @return resource
|
||
*/
|
||
function createGeekNowHandle($prompt, $apiKey, $apiUrl)
|
||
{
|
||
$body = [
|
||
'model' => 'gpt-image-2.6-flare',
|
||
'prompt' => $prompt,
|
||
'n' => 1,
|
||
'size' => '1024x1024',
|
||
'quality' => 'auto',
|
||
'watermark' => false,
|
||
'response_format' => 'url'
|
||
];
|
||
|
||
$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,
|
||
CURLOPT_CONNECTTIMEOUT => 15
|
||
]);
|
||
return $ch;
|
||
}
|
||
|
||
/**
|
||
* 同步下载高清图片
|
||
*
|
||
* @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);
|
||
}
|
||
|
||
/**
|
||
* 触发批量推送入库
|
||
*
|
||
* @return void
|
||
*/
|
||
function flushToOss()
|
||
{
|
||
$phpBin = 'D:\Softwares\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe';
|
||
$output = [];
|
||
$returnVar = 0;
|
||
exec("\"$phpBin\" think fc:category-images", $output, $returnVar);
|
||
}
|
||
|
||
// ======================== 主调度流程 (5 并发) ========================
|
||
$concurrency = 5;
|
||
$phpBin = 'D:\Softwares\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe';
|
||
|
||
echo "========================================================\n";
|
||
echo " GeekNow (gpt-image-2.6-flare @ 0.04元) $concurrency 并发高速入库\n";
|
||
echo "========================================================\n";
|
||
|
||
$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
|
||
");
|
||
$queue = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
$total = count($queue);
|
||
echo "待处理队列总数: $total 项\n\n";
|
||
|
||
if ($total === 0) {
|
||
echo "全站所有二级分类已全部入库!无需处理。\n";
|
||
exit(0);
|
||
}
|
||
|
||
$mh = curl_multi_init();
|
||
$runningHandles = []; // handle_id => ['ch' => $ch, 'cat' => $cat, 'retries' => 0]
|
||
$completedCount = 0;
|
||
$batchSaved = 0;
|
||
|
||
// 先填充初始并发池
|
||
while (count($runningHandles) < $concurrency && !empty($queue)) {
|
||
$cat = array_shift($queue);
|
||
$prompt = buildPromptForGeekNow($cat);
|
||
$ch = createGeekNowHandle($prompt, $apiKey, $apiUrl);
|
||
curl_multi_add_handle($mh, $ch);
|
||
$runningHandles[(int)$ch] = [
|
||
'ch' => $ch,
|
||
'cat' => $cat,
|
||
'retries' => 0
|
||
];
|
||
echo sprintf("[%d/%d] 发起请求: #%d %s\n", $completedCount + count($runningHandles), $total, $cat['id'], $cat['name']);
|
||
}
|
||
|
||
// 循环执行并发
|
||
do {
|
||
$status = curl_multi_exec($mh, $active);
|
||
if ($active) {
|
||
curl_multi_select($mh, 0.5);
|
||
}
|
||
|
||
while ($info = curl_multi_info_read($mh)) {
|
||
$ch = $info['handle'];
|
||
$chId = (int)$ch;
|
||
$meta = $runningHandles[$chId] ?? null;
|
||
|
||
if ($meta) {
|
||
$cat = $meta['cat'];
|
||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
$resp = curl_multi_getcontent($ch);
|
||
curl_multi_remove_handle($mh, $ch);
|
||
curl_close($ch);
|
||
unset($runningHandles[$chId]);
|
||
|
||
$imgUrl = false;
|
||
if ($httpCode === 200) {
|
||
$json = json_decode($resp, true);
|
||
$imgUrl = $json['data'][0]['url'] ?? false;
|
||
}
|
||
|
||
if ($imgUrl) {
|
||
$destFile = $downloadDir . '/' . $cat['id'] . '.jpg';
|
||
if (downloadHdImage($imgUrl, $destFile)) {
|
||
$completedCount++;
|
||
$batchSaved++;
|
||
$sz = round(filesize($destFile) / 1024);
|
||
echo sprintf("✔ [%d/%d] 成功下载: #%d %s (%d KB)\n", $completedCount, $total, $cat['id'], $cat['name'], $sz);
|
||
} else {
|
||
echo sprintf("✘ 下载失败: #%d %s,重新入队\n", $cat['id'], $cat['name']);
|
||
$queue[] = $cat;
|
||
}
|
||
} else {
|
||
if ($meta['retries'] < 3) {
|
||
echo sprintf("⚠ 生图失败 (Code: %d): #%d %s,进行第 %d 次重试\n", $httpCode, $cat['id'], $cat['name'], $meta['retries'] + 1);
|
||
$meta['retries']++;
|
||
$queue[] = $cat;
|
||
} else {
|
||
echo sprintf("❌ 最终失败 (已超最大重试): #%d %s\n", $cat['id'], $cat['name']);
|
||
}
|
||
}
|
||
|
||
// 每攒够 5 张,立即批量推流入库阿里云 OSS
|
||
if ($batchSaved >= 5 || (empty($queue) && empty($runningHandles) && $batchSaved > 0)) {
|
||
echo " ==> 正在批量推流到阿里云 OSS...\n";
|
||
flushToOss();
|
||
echo " ==> 本批次推流并入库完毕!\n";
|
||
$batchSaved = 0;
|
||
}
|
||
|
||
// 补充新请求维持并发池
|
||
if (!empty($queue) && count($runningHandles) < $concurrency) {
|
||
$nextCat = array_shift($queue);
|
||
$prompt = buildPromptForGeekNow($nextCat);
|
||
$newCh = createGeekNowHandle($prompt, $apiKey, $apiUrl);
|
||
curl_multi_add_handle($mh, $newCh);
|
||
$runningHandles[(int)$newCh] = [
|
||
'ch' => $newCh,
|
||
'cat' => $nextCat,
|
||
'retries' => 0
|
||
];
|
||
echo sprintf("[%d/%d] 发起请求: #%d %s\n", $completedCount + count($runningHandles) + count($queue), $total, $nextCat['id'], $nextCat['name']);
|
||
}
|
||
}
|
||
}
|
||
} while ($active || !empty($runningHandles) || !empty($queue));
|
||
|
||
// 如果还有残留图片,做最后一次推流
|
||
if ($batchSaved > 0 || count(glob($downloadDir . '/*.jpg')) > 0) {
|
||
echo " ==> 正在最终批量推流到阿里云 OSS...\n";
|
||
flushToOss();
|
||
}
|
||
|
||
curl_multi_close($mh);
|
||
echo "\n🎉🎉 全部多并发配图与 OSS 入库完毕!\n";
|