AIImageGenerationService.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. <?php
  2. namespace App\Services\AIGeneration;
  3. use App\Facade\Site;
  4. use App\Consts\BaseConst;
  5. use App\Models\MpGeneratePicTask;
  6. use App\Models\MpAsset;
  7. use App\Services\PointsService;
  8. use App\Services\VolcEngineService;
  9. use GuzzleHttp\Client;
  10. use GuzzleHttp\Exception\GuzzleException;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. class AIImageGenerationService
  15. {
  16. private $volcEngineService;
  17. private $httpClient;
  18. /** @var PointsService */
  19. private $pointsService;
  20. // 模型常量
  21. const MODEL_JIMENG_4 = 'jimeng_4.0';
  22. const MODEL_NANO_BANANA_2 = 'NanoBanana2';
  23. const MODEL_NANO_BANANA_PRO = 'NanoBananaPro';
  24. const MODEL_GPT_IMAGE_2 = 'GptImage2';
  25. public function __construct(VolcEngineService $volcEngineService, PointsService $pointsService)
  26. {
  27. $this->volcEngineService = $volcEngineService;
  28. $this->pointsService = $pointsService;
  29. $this->httpClient = new Client([
  30. 'timeout' => 300,
  31. ]);
  32. }
  33. /**
  34. * 构建图片任务计费信息
  35. *
  36. * @param array $params
  37. * @param string $model
  38. * @return array
  39. */
  40. private function buildImageChargeInfo(array $params, string $model): array
  41. {
  42. $uid = 0;
  43. try {
  44. $uid = (int)Site::getUid();
  45. } catch (\Throwable $e) {
  46. // 非请求上下文(如命令行)下可能无法获取用户ID,置为0
  47. }
  48. $width = (int)getProp($params, 'width', 1600);
  49. $height = (int)getProp($params, 'height', 2848);
  50. return [
  51. 'user_id' => $uid,
  52. 'model' => $model,
  53. 'width' => $width,
  54. 'height' => $height,
  55. 'resolution' => $this->pointsService->normalizeImageResolutionKey($width, $height),
  56. 'image_num' => (int)getProp($params, 'image_num', 1),
  57. 'source' => !empty($params['alias_segment_id']) ? 'segment' : (!empty($params['alias_act_id']) ? 'act' : ''),
  58. 'alias_segment_id' => $params['alias_segment_id'] ?? '',
  59. 'created_at' => date('Y-m-d H:i:s'),
  60. ];
  61. }
  62. /**
  63. * 图片生成余额预检
  64. *
  65. * 有用户上下文时校验本次生成所需积分(单价 × 生成张数),不足直接抛错;
  66. * 后台命令/队列等无用户上下文的场景跳过预检。
  67. *
  68. * @param array $chargeInfo
  69. * @return void
  70. */
  71. private function preCheckImageBalance(array $chargeInfo): void
  72. {
  73. $uid = (int)($chargeInfo['user_id'] ?? 0);
  74. if (!$uid) {
  75. return;
  76. }
  77. $imageNum = (int)($chargeInfo['image_num'] ?? 1);
  78. if ($imageNum <= 0) {
  79. $imageNum = 1;
  80. }
  81. $needPoints = $this->pointsService->getImageChargePoints($chargeInfo) * $imageNum;
  82. if ($needPoints <= 0) {
  83. return;
  84. }
  85. $this->pointsService->checkUserPointsEnough($needPoints, $uid);
  86. }
  87. /**
  88. * 图片生成成功统一处理(事务保证原子性)
  89. *
  90. * 在同一事务内完成:任务状态更新 + 积分计费 + 分镜表同步;
  91. * 任一步失败整体回滚,任务保持原状态,由后续定时任务/超时重置重试。
  92. *
  93. * @param MpGeneratePicTask $task
  94. * @param array $resultUrls
  95. * @param array $resultJson
  96. * @return void
  97. */
  98. private function handleImageTaskSuccess(MpGeneratePicTask $task, array $resultUrls, array $resultJson): void
  99. {
  100. try {
  101. DB::beginTransaction();
  102. // 更新任务状态为成功
  103. $task->updateStatus(MpGeneratePicTask::STATUS_SUCCESS, [
  104. 'result_url' => $resultUrls,
  105. 'result_json' => $resultJson,
  106. ]);
  107. // 记录计费明细并扣减用户积分(真实失败则抛错回滚,避免“成功但未计费”)
  108. $chargeResult = $this->pointsService->recordImageTaskCharge($task);
  109. if (empty($chargeResult['charged'])
  110. && !in_array((string)($chargeResult['reason'] ?? ''), ['already_charged', 'no_charge_info'], true)) {
  111. throw new \Exception('图片计费失败: ' . (string)($chargeResult['reason'] ?? 'unknown'));
  112. }
  113. // 同步调整分镜图片状态和结果
  114. $segmentId = getProp($task, 'alias_segment_id');
  115. if ($segmentId) {
  116. $updateSegmentResult = DB::table('mp_episode_segments')->where('segment_id', $segmentId)->update([
  117. 'img_url' => $resultUrls[0],
  118. 'pic_task_status' => '已完成',
  119. ]);
  120. // 本应更新却未更新到(分镜被删除等),抛错触发整体回滚重试
  121. if ($updateSegmentResult === false || $updateSegmentResult === 0) {
  122. throw new \Exception('更新分镜图片状态失败: 未找到对应的分镜记录');
  123. }
  124. // 如果是首帧图ID则更新anime表
  125. if (Redis::sismember('anime_first_frame_urls', $segmentId)) {
  126. $animeId = DB::table('mp_episode_segments')->where('segment_id', $segmentId)->value('anime_id');
  127. DB::table('mp_animes')->where('id', $animeId)->update(['first_frame_url' => $resultUrls[0], 'updated_at' => date('Y-m-d H:i:s')]);
  128. Redis::srem('anime_first_frame_urls', $segmentId);
  129. }
  130. }
  131. DB::commit();
  132. } catch (\Exception $e) {
  133. DB::rollBack();
  134. dLog('generate')->error('图片任务成功处理失败,已回滚: ' . $e->getMessage(), ['task_id' => $task->id]);
  135. logDB('generate', 'error', '图片任务成功处理失败,已回滚', [
  136. 'task_id' => $task->id,
  137. 'error' => $e->getMessage(),
  138. ]);
  139. }
  140. }
  141. /**
  142. * 异步创建图片生成任务
  143. *
  144. * @param array $params
  145. * @return MpGeneratePicTask
  146. */
  147. public function createImageGenerationTask(array $params): MpGeneratePicTask
  148. {
  149. // // 获取模型类型,默认使用即梦AI 4.0
  150. // $model = $params['model'] ?? self::MODEL_JIMENG_4;
  151. // 获取模型类型,默认使用即梦seedream5.0-lite
  152. $model = $params['model'] ?? 'doubao-seedream-5-0-lite-260128';
  153. // 构建计费信息并余额预检(不足直接报错)
  154. $chargeInfo = $this->buildImageChargeInfo($params, $model);
  155. $this->preCheckImageBalance($chargeInfo);
  156. // 检查是否有正在处理的任务(仅即梦AI需要排队)
  157. if ($model === self::MODEL_JIMENG_4 && $this->hasProcessingTask()) {
  158. // 如果有正在处理的任务,创建排队任务
  159. return $this->createQueuedTask($params);
  160. }
  161. // 生成唯一的任务ID
  162. $taskId = 'img_gen_' . time() . '_' . uniqid();
  163. // 创建任务记录
  164. $task = MpGeneratePicTask::create([
  165. 'task_id' => $taskId,
  166. 'alias_segment_id' => $params['alias_segment_id'] ?? 0,
  167. 'prompt' => $params['prompt'] ?? '',
  168. 'width' => $params['width'] ?? 1600,
  169. 'height' => $params['height'] ?? 2848,
  170. 'image_num' => $params['image_num'] ?? 1,
  171. 'scale' => $params['scale'] ?? 50,
  172. 'ref_img_url' => $params['ref_img_urls'] ?? null,
  173. 'mask_img_url' => $params['mask_img_url'] ?? null,
  174. 'extra_params' => $params['extra_params'] ?? null,
  175. 'status' => MpGeneratePicTask::STATUS_PENDING,
  176. 'model' => $model,
  177. 'charge_info' => $chargeInfo,
  178. ]);
  179. // 即梦AI和NanoBanana系列立即提交任务,火山API在定时任务中提交
  180. if ($model === self::MODEL_JIMENG_4) {
  181. $this->submitTaskToJimengApi($task);
  182. } elseif (in_array($model, BaseConst::NANO_BANANA_MODELS)) {
  183. $this->submitTaskToNanoBananaApi($task);
  184. }
  185. return $task;
  186. }
  187. /**
  188. * 检查是否有正在处理的任务
  189. *
  190. * @return bool
  191. */
  192. private function hasProcessingTask(): bool
  193. {
  194. return MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PROCESSING)
  195. ->exists();
  196. }
  197. /**
  198. * 创建排队任务
  199. *
  200. * @param array $params
  201. * @return MpGeneratePicTask
  202. */
  203. private function createQueuedTask(array $params): MpGeneratePicTask
  204. {
  205. // 生成唯一的任务ID
  206. $taskId = 'img_gen_' . time() . '_' . uniqid();
  207. // 获取模型类型
  208. $model = $params['model'] ?? self::MODEL_JIMENG_4;
  209. // 构建计费信息并余额预检(不足直接报错)
  210. $chargeInfo = $this->buildImageChargeInfo($params, $model);
  211. $this->preCheckImageBalance($chargeInfo);
  212. // 创建排队状态的任务记录
  213. return MpGeneratePicTask::create([
  214. 'task_id' => $taskId,
  215. 'alias_segment_id' => $params['alias_segment_id'] ?? 0,
  216. 'prompt' => $params['prompt'] ?? '',
  217. 'width' => $params['width'] ?? 1600,
  218. 'height' => $params['height'] ?? 2848,
  219. 'image_num' => $params['image_num'] ?? 1,
  220. 'scale' => $params['scale'] ?? 50,
  221. 'ref_img_url' => $params['ref_img_urls'] ?? null,
  222. 'mask_img_url' => $params['mask_img_url'] ?? null,
  223. 'extra_params' => $params['extra_params'] ?? null,
  224. 'status' => MpGeneratePicTask::STATUS_PENDING,
  225. 'model' => $model,
  226. 'charge_info' => $chargeInfo,
  227. ]);
  228. }
  229. /**
  230. * 提交任务到即梦AI API
  231. *
  232. * @param MpGeneratePicTask $task
  233. * @return void
  234. */
  235. private function submitTaskToJimengApi(MpGeneratePicTask $task): void
  236. {
  237. try {
  238. // 验证环境变量配置
  239. $accessKey = env('VOLC_AI_AK');
  240. $secretKey = env('VOLC_AI_SK');
  241. if (empty($accessKey) || empty($secretKey)) {
  242. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  243. 'error_message' => '火山引擎访问密钥未配置,请检查环境变量AK和SK'
  244. ]);
  245. return;
  246. }
  247. //构建即梦AI 4.0 API请求参数
  248. $apiParams = [
  249. 'req_key' => 'jimeng_t2i_v40',
  250. 'prompt' => $task->prompt,
  251. 'scale' => $task->scale / 100, //为0-1范围
  252. ];
  253. // 添加可选参数
  254. if ($task->image_num > 1) {
  255. $apiParams['force_single'] = false;
  256. } else {
  257. $apiParams['force_single'] = true;
  258. }
  259. // 参考图片
  260. if ($task->ref_img_url) {
  261. $apiParams['image_urls'] = $task->ref_img_url;
  262. }
  263. // if ($task->mask_img_url) {
  264. // $apiParams['image_urls'] = $task->mask_img_url;
  265. // }
  266. // 尺寸参数
  267. $area = $task->width * $task->height; //计算面积
  268. if ($area >= 1024*1024 && $area <= 4096*4096) {
  269. $apiParams['size'] = (int)$area;
  270. }
  271. dLog('generate')->info('即梦AI文生图参数: ', $apiParams);
  272. logDB('generate', 'info', '即梦AI文生图任务提交', ['task_id' => $task->task_id, 'params' => $apiParams]);
  273. //调用即梦AI 4.0 API提交任务
  274. $response = $this->volcEngineService->request(
  275. 'POST',
  276. 'visual.volcengineapi.com', //即梦AI API域名
  277. '/', // 使用根路径
  278. [], // Query参数
  279. json_encode($apiParams),
  280. $accessKey,
  281. $secretKey,
  282. 'cv', // 服务标识
  283. 'cn-north-1', //区域
  284. 'CVSync2AsyncSubmitTask',
  285. '2022-08-31',
  286. 'application/json'
  287. );
  288. $responseData = json_decode($response['body'], true);
  289. dLog('generate')->info('即梦AI 4.0 API提交任务响应: ', $responseData);
  290. logDB('generate', 'info', '即梦AI 4.0 API提交任务响应', ['task_id' => $task->task_id, 'response' => $responseData]);
  291. if ($responseData['code'] !== 10000) {
  292. // API返回错误
  293. logDB('generate', 'error', '即梦AI任务提交失败', ['task_id' => $task->task_id, 'error' => $responseData['message'] ?? 'API Error']);
  294. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  295. 'error_message' => $responseData['message'] ?? 'API Error',
  296. 'extra_params' => $responseData['data'] ?? []
  297. ]);
  298. } else {
  299. // 任务提交成功,更新状态为处理中
  300. $updateData = [];
  301. //存储API返回的任务ID
  302. if (isset($responseData['data']['task_id'])) {
  303. $updateData['task_id'] = $responseData['data']['task_id'];
  304. $updateData['extra_params'] = $responseData ?? [];
  305. }
  306. $task->updateStatus(MpGeneratePicTask::STATUS_PROCESSING, $updateData);
  307. }
  308. } catch (\Exception $e) {
  309. // 记录错误
  310. logDB('generate', 'error', '即梦AI任务提交异常', ['task_id' => $task->task_id, 'error' => $e->getMessage()]);
  311. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  312. 'error_message' => 'API请求失败: ' . $e->getMessage()
  313. ]);
  314. }
  315. }
  316. /**
  317. * 提交任务到GPT-Image2 API
  318. *
  319. * @param MpGeneratePicTask $task
  320. * @return void
  321. */
  322. public function submitTaskToGptImage2Api(MpGeneratePicTask $task): void
  323. {
  324. try {
  325. // 验证环境变量配置
  326. $apiKey = env('GPT_IMAGE2_API_KEY');
  327. if (empty($apiKey)) {
  328. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  329. 'error_message' => 'GPT-Image2 API KEY未配置,请检查环境变量GPT_IMAGE2_API_KEY'
  330. ]);
  331. return;
  332. }
  333. // 更新状态为处理中
  334. $task->updateStatus(MpGeneratePicTask::STATUS_PROCESSING);
  335. // $api_url = 'https://token.ithinkai.cn/v1/images/generations'; // 备用地址1
  336. // $api_url = 'https://api.nonelinear.com/v1/images/generations'; // 备用地址2
  337. $api_url = 'https://ai-api.kkidc.com/v1/images/generations';
  338. $isEditMode = false;
  339. // 参考图片 - 如果有参考图则使用 /edits 接口
  340. if ($task->ref_img_url) {
  341. // $api_url = 'https://token.ithinkai.cn/v1/images/edits'; // 备用地址1
  342. // $api_url = 'https://api.nonelinear.com/v1/images/generations'; // 备用地址2
  343. $api_url = 'https://ai-api.kkidc.com/v1/images/edits';
  344. $isEditMode = true;
  345. }
  346. dLog('generate')->info('开始调用GPT-Image2 API', ['task_id' => $task->task_id, 'mode' => $isEditMode ? 'edit' : 'generation']);
  347. logDB('generate', 'info', '开始调用GPT-Image2 API', ['task_id' => $task->task_id, 'mode' => $isEditMode ? 'edit' : 'generation']);
  348. // 创建专用客户端,优化配置
  349. $gptClient = new Client([
  350. 'timeout' => 1800, // 超时
  351. 'connect_timeout' => 120, // 连接超时10秒
  352. 'http_errors' => false, // 不自动抛出HTTP错误
  353. 'verify' => false, // 禁用SSL验证(如果有证书问题)
  354. 'allow_redirects' => true,
  355. ]);
  356. // 根据是否有参考图选择不同的请求方式
  357. if ($isEditMode) {
  358. // 使用原生 cURL 处理 multipart/form-data 格式
  359. $imageUrls = is_array($task->ref_img_url) ? $task->ref_img_url : [$task->ref_img_url];
  360. // 限制最多4张参考图
  361. $imageUrls = array_slice($imageUrls, 0, 4);
  362. dLog('generate')->info('下载参考图片', ['image_urls' => $imageUrls, 'count' => count($imageUrls)]);
  363. // 下载所有参考图片并保存为临时文件
  364. $tempFiles = [];
  365. foreach ($imageUrls as $index => $imageUrl) {
  366. $imageContent = @file_get_contents($imageUrl);
  367. if ($imageContent === false) {
  368. dLog('generate')->warning('下载参考图片失败,跳过', ['image_url' => $imageUrl]);
  369. continue;
  370. }
  371. dLog('generate')->info('参考图片下载完成', ['index' => $index, 'size' => strlen($imageContent)]);
  372. // 保存为临时文件(cURL 需要真实文件路径)
  373. $extension = pathinfo(parse_url($imageUrl, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'png';
  374. $tempFile = tempnam(sys_get_temp_dir(), 'gpt_img_') . '.' . $extension;
  375. file_put_contents($tempFile, $imageContent);
  376. $tempFiles[] = [
  377. 'path' => $tempFile,
  378. 'extension' => $extension
  379. ];
  380. dLog('generate')->info('临时文件创建成功', ['file' => $tempFile, 'size' => filesize($tempFile)]);
  381. }
  382. if (empty($tempFiles)) {
  383. throw new \Exception('所有参考图片下载失败');
  384. }
  385. // 构建 POST 字段
  386. $postFields = [
  387. 'prompt' => $task->prompt,
  388. 'model' => 'gpt-image-2',
  389. 'n' => (string)$task->image_num, // 新接口要求字符串类型
  390. 'response_format' => 'b64_json', // 新接口必填参数
  391. ];
  392. // 尺寸参数(必填)
  393. if ($task->width && $task->height) {
  394. $postFields['size'] = $task->width . 'x' . $task->height;
  395. } else {
  396. $postFields['size'] = '1024x1024'; // 默认尺寸
  397. }
  398. // 添加参考图片文件(支持多图,新接口要求使用 image 字段名)
  399. foreach ($tempFiles as $index => $tempFile) {
  400. $fieldName = 'image'; // 新接口统一使用 image 字段名
  401. $postFields[$fieldName] = new \CURLFile(
  402. $tempFile['path'],
  403. 'image/' . $tempFile['extension'],
  404. 'image' . ($index + 1) . '.' . $tempFile['extension']
  405. );
  406. }
  407. dLog('generate')->info('GPT-Image2 API参数(编辑模式-cURL)', [
  408. 'api_url' => $api_url,
  409. 'prompt' => $task->prompt,
  410. 'size' => $postFields['size'],
  411. 'n' => $postFields['n'],
  412. 'response_format' => $postFields['response_format'],
  413. 'image_count' => count($tempFiles)
  414. ]);
  415. logDB('generate', 'info', 'GPT-Image2任务提交(编辑模式)', [
  416. 'task_id' => $task->task_id,
  417. 'api_url' => $api_url,
  418. 'image_urls' => $imageUrls,
  419. 'prompt' => $task->prompt
  420. ]);
  421. $startTime = microtime(true);
  422. // 使用原生 cURL
  423. $ch = curl_init();
  424. curl_setopt_array($ch, [
  425. CURLOPT_URL => $api_url,
  426. CURLOPT_RETURNTRANSFER => true,
  427. CURLOPT_POST => true,
  428. CURLOPT_POSTFIELDS => $postFields,
  429. CURLOPT_HTTPHEADER => [
  430. 'Authorization: Bearer ' . $apiKey,
  431. 'Accept: application/json',
  432. 'Expect:', // 移除 Expect: 100-continue,避免等待
  433. ],
  434. CURLOPT_TIMEOUT => 600,
  435. CURLOPT_CONNECTTIMEOUT => 30,
  436. CURLOPT_SSL_VERIFYPEER => false,
  437. CURLOPT_SSL_VERIFYHOST => false,
  438. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // 强制使用 HTTP/1.1
  439. ]);
  440. dLog('generate')->info('开始发送cURL请求');
  441. $responseBody = curl_exec($ch);
  442. $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  443. $curlError = curl_error($ch);
  444. $curlErrno = curl_errno($ch);
  445. curl_close($ch);
  446. // 删除所有临时文件
  447. foreach ($tempFiles as $tempFile) {
  448. @unlink($tempFile['path']);
  449. }
  450. $elapsed = round(microtime(true) - $startTime, 2);
  451. dLog('generate')->info('cURL请求完成', ['elapsed' => $elapsed . 's', 'http_code' => $statusCode, 'curl_errno' => $curlErrno]);
  452. if ($curlErrno !== 0) {
  453. throw new \Exception('cURL错误: ' . $curlError . ' (错误码: ' . $curlErrno . ')');
  454. }
  455. if (empty($responseBody)) {
  456. throw new \Exception('API返回空响应,HTTP状态码: ' . $statusCode);
  457. }
  458. } else {
  459. // 使用 JSON 格式 (原有的文生图模式)
  460. $apiParams = [
  461. 'model' => 'gpt-image-2',
  462. 'prompt' => $task->prompt,
  463. 'n' => (int)$task->image_num,
  464. 'quality' => 'auto', // 新接口必填参数:图像质量等级
  465. 'response_format' => 'b64_json', // 新接口必填参数:返回格式
  466. ];
  467. // 尺寸参数
  468. if ($task->width && $task->height) {
  469. $apiParams['size'] = $task->width . 'x' . $task->height;
  470. } else {
  471. $apiParams['size'] = 'auto'; // 新接口必填参数
  472. }
  473. dLog('generate')->info('GPT-Image2 API参数', $apiParams);
  474. logDB('generate', 'info', 'GPT-Image2任务提交', ['task_id' => $task->task_id, 'api_url' => $api_url, 'params' => $apiParams]);
  475. $startTime = microtime(true);
  476. // 调用GPT-Image2 API
  477. $response = $gptClient->post($api_url, [
  478. 'headers' => [
  479. 'Authorization' => 'Bearer ' . $apiKey,
  480. 'Content-Type' => 'application/json',
  481. ],
  482. 'json' => $apiParams,
  483. ]);
  484. $statusCode = $response->getStatusCode();
  485. $responseBody = $response->getBody()->getContents();
  486. $elapsed = round(microtime(true) - $startTime, 2);
  487. dLog('generate')->info('Guzzle请求完成', ['elapsed' => $elapsed . 's', 'http_code' => $statusCode]);
  488. if (empty($responseBody)) {
  489. throw new \Exception('API返回空响应,HTTP状态码: ' . $statusCode);
  490. }
  491. }
  492. // 解析响应
  493. dLog('generate')->info('响应数据长度', ['length' => strlen($responseBody)]);
  494. $responseData = json_decode($responseBody, true);
  495. if (json_last_error() !== JSON_ERROR_NONE) {
  496. dLog('generate')->error('JSON解析失败', ['error' => json_last_error_msg(), 'body' => substr($responseBody, 0, 500)]);
  497. throw new \Exception('JSON解析失败: ' . json_last_error_msg());
  498. }
  499. // 移除 b64_json 字段用于日志记录,避免生成超大日志
  500. $logResponseData = $this->removeB64JsonFromResponse($responseData);
  501. dLog('generate')->info('GPT-Image2 API响应', $logResponseData);
  502. logDB('generate', 'info', 'GPT-Image2 API响应', ['task_id' => $task->task_id, 'response' => $logResponseData]);
  503. if (isset($responseData['error'])) {
  504. // API返回错误
  505. logDB('generate', 'error', 'GPT-Image2任务失败', ['task_id' => $task->task_id, 'error' => $responseData['error']['message'] ?? 'API Error']);
  506. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  507. 'error_message' => $responseData['error']['message'] ?? 'API Error',
  508. 'result_json' => $logResponseData
  509. ]);
  510. } else {
  511. // 任务提交成功,直接处理结果
  512. dLog('generate')->info('开始处理GPT-Image2响应结果');
  513. $this->processGptImage2ApiResponse($task, $responseData);
  514. }
  515. } catch (\Exception $e) {
  516. // 记录错误
  517. dLog('generate')->error('GPT-Image2 API请求失败: ' . $e->getMessage(), [
  518. 'task_id' => $task->task_id,
  519. 'trace' => $e->getTraceAsString()
  520. ]);
  521. logDB('generate', 'error', 'GPT-Image2任务异常', [
  522. 'task_id' => $task->task_id,
  523. 'error' => $e->getMessage(),
  524. 'file' => $e->getFile(),
  525. 'line' => $e->getLine()
  526. ]);
  527. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  528. 'error_message' => 'API请求失败: ' . $e->getMessage()
  529. ]);
  530. }
  531. }
  532. /**
  533. * 处理GPT-Image2 API响应结果
  534. *
  535. * @param MpGeneratePicTask $task
  536. * @param array $responseData
  537. * @return void
  538. */
  539. private function processGptImage2ApiResponse(MpGeneratePicTask $task, array $responseData): void
  540. {
  541. try {
  542. if (!isset($responseData['data']) || empty($responseData['data'])) {
  543. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  544. 'error_message' => '未返回图片数据',
  545. 'result_json' => $responseData
  546. ]);
  547. return;
  548. }
  549. $result_urls = [];
  550. foreach ($responseData['data'] as $index => $imageData) {
  551. // 优先处理 b64_json 格式(新接口返回格式)
  552. if (isset($imageData['b64_json']) && !empty($imageData['b64_json'])) {
  553. $base64Data = $imageData['b64_json'];
  554. $imageStream = base64_decode($base64Data);
  555. if ($imageStream === false) {
  556. dLog('generate')->error('Base64解码失败', ['task_id' => $task->task_id, 'index' => $index]);
  557. continue;
  558. }
  559. $pic_name = 'ai_generation_gpt_' . time() . '_' . uniqid() . '.png';
  560. // 压缩PNG图片流
  561. $compressedData = $this->compressImageStream($imageStream);
  562. $url = uploadStreamByTos('image', $compressedData, $pic_name);
  563. $result_urls[] = $url;
  564. // 记录优化后的提示词(如果有)
  565. if (isset($imageData['revised_prompt'])) {
  566. dLog('generate')->info('图片生成优化提示词', [
  567. 'task_id' => $task->task_id,
  568. 'index' => $index,
  569. 'revised_prompt' => $imageData['revised_prompt']
  570. ]);
  571. }
  572. } elseif (isset($imageData['url']) && !empty($imageData['url'])) {
  573. // 兼容旧的 url 格式(如果接口仍返回)
  574. $url = $imageData['url'];
  575. $pic_name = 'ai_generation_gpt_' . time() . '_' . uniqid();
  576. $pic_ext = getImgExtFromUrl($url);
  577. $pic_name = $pic_name . $pic_ext;
  578. if ($pic_ext === '.png') {
  579. $comporessed_data = compressRemoteImageUrlToSize($url);
  580. $url = uploadStreamByTos('image', $comporessed_data, $pic_name);
  581. } else {
  582. $url = uploadStreamByTos('image', file_get_contents($url), $pic_name);
  583. }
  584. $result_urls[] = $url;
  585. }
  586. }
  587. if (empty($result_urls)) {
  588. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  589. 'error_message' => '所有图片生成失败',
  590. 'result_json' => $responseData
  591. ]);
  592. return;
  593. }
  594. // 移除 b64_json 字段以避免存储大量 Base64 数据
  595. $cleanedResponseData = $responseData;
  596. if (isset($cleanedResponseData['data'])) {
  597. foreach ($cleanedResponseData['data'] as &$imageData) {
  598. unset($imageData['b64_json']); // 移除 Base64 数据
  599. }
  600. unset($imageData); // 解除引用
  601. }
  602. // 图片生成成功统一处理(事务:状态更新 + 计费 + 分镜同步)
  603. $this->handleImageTaskSuccess($task, $result_urls, $cleanedResponseData);
  604. } catch (\Exception $e) {
  605. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  606. 'error_message' => '处理响应失败: ' . $e->getMessage()
  607. ]);
  608. }
  609. }
  610. /**
  611. * 提交任务到火山图片生成API
  612. *
  613. * @param MpGeneratePicTask $task
  614. * @return void
  615. */
  616. public function submitTaskToVolcApi(MpGeneratePicTask $task): void
  617. {
  618. try {
  619. // 验证环境变量配置
  620. $apiKey = env('VOLC_AI_API_KEY');
  621. if (empty($apiKey)) {
  622. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  623. 'error_message' => '火山引擎API KEY未配置,请检查环境变量VOLC_AI_API_KEY'
  624. ]);
  625. return;
  626. }
  627. // 更新状态为处理中
  628. $task->updateStatus(MpGeneratePicTask::STATUS_PROCESSING);
  629. // 构建火山图片生成API请求参数
  630. $apiParams = [
  631. 'model' => isset(BaseConst::MODELS_MAP[$task->model]) ? BaseConst::MODELS_MAP[$task->model] : $task->model,
  632. 'prompt' => $task->prompt,
  633. 'watermark' => false,
  634. ];
  635. // 尺寸参数
  636. if ($task->width && $task->height) {
  637. $apiParams['size'] = $task->width . 'x' . $task->height;
  638. }
  639. // 参考图片
  640. if ($task->ref_img_url) {
  641. if (is_array($task->ref_img_url)) {
  642. // $apiParams['image'] = count($task->ref_img_url) === 1 ? $task->ref_img_url[0] : $task->ref_img_url;
  643. $apiParams['image'] = $task->ref_img_url;
  644. } else {
  645. $apiParams['image'] = $task->ref_img_url;
  646. }
  647. }
  648. // 生成多图或者单图
  649. if ((int)$task->image_num === 1) {
  650. $apiParams['sequential_image_generation'] = 'disabled';
  651. }else if ($task->image_num > 1) {
  652. $apiParams['sequential_image_generation'] = 'auto';
  653. $apiParams['sequential_image_generation_options']['max_images'] = $task->image_num;
  654. }
  655. dLog('generate')->info('火山图片生成API参数: ', $apiParams);
  656. logDB('generate', 'info', '火山图片生成任务提交', ['task_id' => $task->task_id, 'params' => $apiParams]);
  657. // 调用火山图片生成API
  658. $response = $this->httpClient->post('https://ark.cn-beijing.volces.com/api/v3/images/generations', [
  659. 'headers' => [
  660. 'Authorization' => 'Bearer ' . $apiKey,
  661. 'Content-Type' => 'application/json',
  662. ],
  663. 'json' => $apiParams,
  664. ]);
  665. $responseData = json_decode($response->getBody()->getContents(), true);
  666. dLog('generate')->info('火山图片生成API响应: ', $responseData);
  667. logDB('generate', 'info', '火山图片生成API响应', ['task_id' => $task->task_id, 'response' => $responseData]);
  668. if (isset($responseData['error'])) {
  669. // API返回错误
  670. logDB('generate', 'error', '火山图片生成任务失败', ['task_id' => $task->task_id, 'error' => $responseData['error']['message'] ?? 'API Error']);
  671. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  672. 'error_message' => $responseData['error']['message'] ?? 'API Error',
  673. 'result_json' => $responseData
  674. ]);
  675. } else {
  676. // 任务提交成功,直接处理结果
  677. $this->processVolcApiResponse($task, $responseData);
  678. }
  679. } catch (\Exception $e) {
  680. // 记录错误
  681. dLog('generate')->error('火山API请求失败: ' . $e->getMessage());
  682. logDB('generate', 'error', '火山图片生成任务异常', ['task_id' => $task->task_id, 'error' => $e->getMessage()]);
  683. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  684. 'error_message' => 'API请求失败: ' . $e->getMessage()
  685. ]);
  686. }
  687. }
  688. /**
  689. * 处理火山API响应结果
  690. *
  691. * @param MpGeneratePicTask $task
  692. * @param array $responseData
  693. * @return void
  694. */
  695. private function processVolcApiResponse(MpGeneratePicTask $task, array $responseData): void
  696. {
  697. try {
  698. if (!isset($responseData['data']) || empty($responseData['data'])) {
  699. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  700. 'error_message' => '未返回图片数据',
  701. 'result_json' => $responseData
  702. ]);
  703. return;
  704. }
  705. $result_urls = [];
  706. foreach ($responseData['data'] as $imageData) {
  707. if (isset($imageData['error'])) {
  708. // 单张图片生成失败
  709. dLog('generate')->warning('单张图片生成失败: ', $imageData['error']);
  710. continue;
  711. }
  712. if (isset($imageData['url'])) {
  713. $url = $imageData['url'];
  714. $pic_name = 'ai_generation_' . time() . '_' . uniqid();
  715. $pic_ext = getImgExtFromUrl($url);
  716. $pic_name = $pic_name . $pic_ext;
  717. if ($pic_ext === '.png') {
  718. $comporessed_data = compressRemoteImageUrlToSize($url);
  719. $url = uploadStreamByTos('image', $comporessed_data, $pic_name);
  720. } else {
  721. // 将图片另存到tos
  722. $url = uploadStreamByTos('image', file_get_contents($url), $pic_name);
  723. }
  724. $result_urls[] = $url;
  725. }
  726. }
  727. if (empty($result_urls)) {
  728. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  729. 'error_message' => '所有图片生成失败',
  730. 'result_json' => $responseData
  731. ]);
  732. return;
  733. }
  734. // 图片生成成功统一处理(事务:状态更新 + 计费 + 分镜同步)
  735. $this->handleImageTaskSuccess($task, $result_urls, $responseData);
  736. } catch (\Exception $e) {
  737. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  738. 'error_message' => '处理响应失败: ' . $e->getMessage()
  739. ]);
  740. }
  741. }
  742. public function getTaskList($data)
  743. {
  744. $task_id = getProp($data, 'task_id');
  745. $status = getProp($data, 'status');
  746. $query = MpGeneratePicTask::select('*');
  747. if ($task_id) {
  748. $query->where('id', $task_id);
  749. }
  750. if ($status) {
  751. $query->where('status', $status);
  752. }
  753. return $query->orderBy('created_at', 'desc')->paginate();
  754. }
  755. /**
  756. * 查询任务状态
  757. *
  758. * @param MpGeneratePicTask $task
  759. * @return array
  760. */
  761. public function queryTaskStatus(MpGeneratePicTask $task): array
  762. {
  763. // 根据模型类型调用不同的查询方法
  764. if ($task->model === self::MODEL_JIMENG_4) {
  765. return $this->queryJimengTaskStatus($task);
  766. } elseif (in_array($task->model, BaseConst::NANO_BANANA_MODELS)) {
  767. return $this->queryNanoBananaTaskStatus($task);
  768. } else {
  769. // 火山API是同步返回结果,不需要查询
  770. return [
  771. 'status' => $task->status === MpGeneratePicTask::STATUS_SUCCESS ? 'success' : 'failed',
  772. 'error_message' => $task->error_message,
  773. 'result_url' => $task->result_url,
  774. 'result_json' => $task->result_json ?? []
  775. ];
  776. }
  777. }
  778. /**
  779. * 查询即梦AI任务状态
  780. *
  781. * @param MpGeneratePicTask $task
  782. * @return array
  783. */
  784. private function queryJimengTaskStatus(MpGeneratePicTask $task): array
  785. {
  786. try {
  787. // 获取API任务ID
  788. $apiTaskId = $task->task_id ?? null;
  789. if (!$apiTaskId) {
  790. return [
  791. 'status' => 'failed',
  792. 'error_message' => 'API任务ID不存在',
  793. 'result_url' => null
  794. ];
  795. }
  796. //构建查询参数
  797. $apiParams = [
  798. 'req_key' => 'jimeng_t2i_v40',
  799. 'task_id' => $apiTaskId,
  800. 'req_json' => '{"return_url":true}' // 返回URL格式
  801. ];
  802. //调用即梦AI 4.0 API查询任务状态
  803. $response = $this->volcEngineService->request(
  804. 'POST',
  805. 'visual.volcengineapi.com',
  806. '/', // 使用根路径
  807. [], // Query参数
  808. json_encode($apiParams),
  809. env('VOLC_AI_AK'),
  810. env('VOLC_AI_SK'),
  811. 'cv',
  812. 'cn-north-1',
  813. 'CVSync2AsyncGetResult',
  814. '2022-08-31',
  815. 'application/json'
  816. );
  817. $responseData = json_decode($response['body'], true);
  818. dLog('generate')->info('即梦AI 4.0 API查询任务状态响应: ', $responseData);
  819. logDB('generate', 'info', '即梦AI任务状态查询', ['task_id' => $apiTaskId, 'response' => $responseData]);
  820. if ($responseData['code'] !== 10000) {
  821. // API返回错误
  822. logDB('generate', 'error', '即梦AI任务状态查询失败', ['task_id' => $apiTaskId, 'error' => $responseData['message'] ?? 'API Error']);
  823. return [
  824. 'status' => 'failed',
  825. 'error_message' => $responseData['message'] ?? 'API Error',
  826. 'result_url' => null,
  827. 'result_json' => $responseData ?? []
  828. ];
  829. }
  830. // 解析API响应
  831. $result = $responseData['data'] ?? [];
  832. $taskStatus = $result['status'] ?? 'failed';
  833. $returnData = [
  834. 'status' => $taskStatus,
  835. 'error_message' => null,
  836. 'result_url' => null,
  837. 'result_json' => $responseData ?? []
  838. ];
  839. // 如果任务成功,获取结果URL
  840. if ($taskStatus === 'done' && isset($result['image_urls']) && count($result['image_urls']) > 0) {
  841. $result_urls = [];
  842. foreach ($result['image_urls'] as $url) {
  843. $pic_name = 'ai_generation_' . time() . '_' . uniqid();
  844. $pic_ext = getImgExtFromUrl($url);
  845. $pic_name = $pic_name . $pic_ext;
  846. if ($pic_ext === '.png') {
  847. $comporessed_data = compressRemoteImageUrlToSize($url);
  848. $url = uploadStreamByTos('image', $comporessed_data, $pic_name);
  849. }else {
  850. // 将图片另存到tos
  851. $url = uploadStreamByTos('image', file_get_contents($url), $pic_name);
  852. }
  853. $result_urls[] = $url;
  854. }
  855. $returnData['result_url'] = $result_urls;
  856. $returnData['status'] = 'success';
  857. } elseif (in_array($taskStatus, ['not_found', 'expired'])) {
  858. $returnData['status'] = 'failed';
  859. $returnData['error_message'] = '任务未找到或已过期';
  860. } elseif ($taskStatus === 'failed') {
  861. $returnData['status'] = 'failed';
  862. $returnData['error_message'] = '任务执行失败';
  863. }
  864. // in_queue 或 generating状态保持原status
  865. return $returnData;
  866. } catch (\Exception $e) {
  867. return [
  868. 'status' => 'failed',
  869. 'error_message' => $e->getMessage(),
  870. 'result_url' => null
  871. ];
  872. }
  873. }
  874. /**
  875. * 提交任务到NanoBanana API
  876. *
  877. * @param MpGeneratePicTask $task
  878. * @return void
  879. */
  880. private function submitTaskToNanoBananaApi(MpGeneratePicTask $task): void
  881. {
  882. try {
  883. // 验证环境变量配置
  884. $apiKey = env('NANO_BANANA_API_KEY');
  885. if (empty($apiKey)) {
  886. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  887. 'error_message' => 'NanoBanana API KEY未配置,请检查环境变量NANO_BANANA_API_KEY'
  888. ]);
  889. return;
  890. }
  891. // 根据模型选择API地址
  892. switch ($task->model) {
  893. case self::MODEL_NANO_BANANA_2:
  894. $apiUrl = 'https://api.wuyinkeji.com/api/async/image_nanoBanana2';
  895. break;
  896. case self::MODEL_NANO_BANANA_PRO:
  897. $apiUrl = 'https://api.wuyinkeji.com/api/async/image_nanoBanana_pro';
  898. break;
  899. case self::MODEL_GPT_IMAGE_2:
  900. $apiUrl = 'https://api.wuyinkeji.com/api/async/image_gpt';
  901. break;
  902. default:
  903. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  904. 'error_message' => '不支持的模型类型: ' . $task->model
  905. ]);
  906. logDB('generate', 'error', '模型类型错误', ['task_id' => $task->task_id, 'model' => $task->model]);
  907. return;
  908. }
  909. // 构建API请求参数
  910. $apiParams = [
  911. 'key' => $apiKey,
  912. 'prompt' => $task->prompt,
  913. ];
  914. if ($task->model == self::MODEL_GPT_IMAGE_2) {
  915. // 该模型下,size需设置为9:16类似的数值,请根据如下比例反推
  916. $apiParams['size'] = $this->getRatioFromSize($task->width, $task->height);
  917. }else {
  918. // 尺寸参数(根据宽高判断)
  919. $area = $task->width * $task->height;
  920. if ($area >= 1024*1024 && $area < 2048*2048) {
  921. $apiParams['size'] = '1k';
  922. } elseif ($area >= 2048*2048) {
  923. $apiParams['size'] = '2k';
  924. } else {
  925. $apiParams['size'] = '1k'; // 默认1k
  926. }
  927. // 画面比例
  928. if ($task->width && $task->height) {
  929. $ratio = $task->width . ':' . $task->height;
  930. // 简化比例
  931. $gcd = $this->gcd($task->width, $task->height);
  932. $ratio = ($task->width / $gcd) . ':' . ($task->height / $gcd);
  933. $apiParams['aspectRatio'] = $ratio;
  934. }
  935. }
  936. // 参考图片
  937. if ($task->ref_img_url) {
  938. $apiParams['urls'] = is_array($task->ref_img_url) ? $task->ref_img_url : [$task->ref_img_url];
  939. }
  940. dLog('generate')->info('NanoBanana API参数: ', $apiParams);
  941. logDB('generate', 'info', 'NanoBanana任务提交', ['task_id' => $task->task_id, 'model' => $task->model, 'params' => $apiParams]);
  942. // 调用NanoBanana API
  943. $response = $this->httpClient->post($apiUrl, [
  944. 'query' => ['key' => $apiKey],
  945. 'json' => $apiParams,
  946. ]);
  947. $responseData = json_decode($response->getBody()->getContents(), true);
  948. dLog('generate')->info('NanoBanana API响应: ', $responseData);
  949. logDB('generate', 'info', 'NanoBanana API响应', ['task_id' => $task->task_id, 'response' => $responseData]);
  950. if ($responseData['code'] !== 200) {
  951. // API返回错误
  952. logDB('generate', 'error', 'NanoBanana任务提交失败', ['task_id' => $task->task_id, 'error' => $responseData['msg'] ?? 'API Error']);
  953. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  954. 'error_message' => $responseData['msg'] ?? 'API Error',
  955. 'extra_params' => $responseData
  956. ]);
  957. } else {
  958. // 任务提交成功,更新状态为处理中
  959. $updateData = [];
  960. // 存储API返回的任务ID
  961. if (isset($responseData['data']['id'])) {
  962. $updateData['task_id'] = $responseData['data']['id'];
  963. $updateData['extra_params'] = $responseData;
  964. }
  965. $task->updateStatus(MpGeneratePicTask::STATUS_PROCESSING, $updateData);
  966. }
  967. } catch (\Exception $e) {
  968. // 记录错误
  969. logDB('generate', 'error', 'NanoBanana任务提交异常', ['task_id' => $task->task_id, 'error' => $e->getMessage()]);
  970. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  971. 'error_message' => 'API请求失败: ' . $e->getMessage()
  972. ]);
  973. }
  974. }
  975. /**
  976. * 查询NanoBanana任务状态
  977. *
  978. * @param MpGeneratePicTask $task
  979. * @return array
  980. */
  981. private function queryNanoBananaTaskStatus(MpGeneratePicTask $task): array
  982. {
  983. try {
  984. // 获取API任务ID
  985. $apiTaskId = $task->task_id ?? null;
  986. $apiKey = env('NANO_BANANA_API_KEY');
  987. if (!$apiTaskId) {
  988. return [
  989. 'status' => 'failed',
  990. 'error_message' => 'API任务ID不存在',
  991. 'result_url' => null
  992. ];
  993. }
  994. if (empty($apiKey)) {
  995. return [
  996. 'status' => 'failed',
  997. 'error_message' => 'NanoBanana API KEY未配置',
  998. 'result_url' => null
  999. ];
  1000. }
  1001. // 调用统一查询接口
  1002. $response = $this->httpClient->get('https://api.wuyinkeji.com/api/async/detail', [
  1003. 'query' => [
  1004. 'key' => $apiKey,
  1005. 'id' => $apiTaskId
  1006. ]
  1007. ]);
  1008. $responseData = json_decode($response->getBody()->getContents(), true);
  1009. dLog('generate')->info('NanoBanana任务状态查询响应: ', $responseData);
  1010. logDB('generate', 'info', 'NanoBanana任务状态查询', ['task_id' => $apiTaskId, 'response' => $responseData]);
  1011. if ($responseData['code'] !== 200) {
  1012. // API返回错误
  1013. logDB('generate', 'error', 'NanoBanana任务状态查询失败', ['task_id' => $apiTaskId, 'error' => $responseData['msg'] ?? 'API Error']);
  1014. return [
  1015. 'status' => 'failed',
  1016. 'error_message' => $responseData['msg'] ?? 'API Error',
  1017. 'result_url' => null,
  1018. 'result_json' => $responseData
  1019. ];
  1020. }
  1021. // 解析API响应
  1022. $data = $responseData['data'] ?? [];
  1023. $taskStatus = $data['status'] ?? 0;
  1024. $returnData = [
  1025. 'status' => 'processing',
  1026. 'error_message' => null,
  1027. 'result_url' => null,
  1028. 'result_json' => $responseData
  1029. ];
  1030. // status: 2表示成功
  1031. if ($taskStatus === 2 && isset($data['result']) && !empty($data['result'])) {
  1032. $result_urls = [];
  1033. foreach ($data['result'] as $url) {
  1034. $pic_name = 'ai_generation_nanobanana_' . time() . '_' . uniqid();
  1035. $pic_ext = getImgExtFromUrl($url);
  1036. $pic_name = $pic_name . $pic_ext;
  1037. if ($pic_ext === '.png') {
  1038. $comporessed_data = compressRemoteImageUrlToSize($url);
  1039. $url = uploadStreamByTos('image', $comporessed_data, $pic_name);
  1040. } else {
  1041. // 将图片另存到tos
  1042. $url = uploadStreamByTos('image', file_get_contents($url), $pic_name);
  1043. }
  1044. $result_urls[] = $url;
  1045. }
  1046. $returnData['result_url'] = $result_urls;
  1047. $returnData['status'] = 'success';
  1048. } elseif ($taskStatus === 3) {
  1049. // status: 3表示失败
  1050. $returnData['status'] = 'failed';
  1051. $returnData['error_message'] = $data['message'] ?? '任务执行失败';
  1052. }
  1053. // status: 0或1表示处理中,保持processing状态
  1054. return $returnData;
  1055. } catch (\Exception $e) {
  1056. return [
  1057. 'status' => 'failed',
  1058. 'error_message' => $e->getMessage(),
  1059. 'result_url' => null
  1060. ];
  1061. }
  1062. }
  1063. /**
  1064. * 计算最大公约数(用于简化比例)
  1065. *
  1066. * @param int $a
  1067. * @param int $b
  1068. * @return int
  1069. */
  1070. private function gcd(int $a, int $b): int
  1071. {
  1072. while ($b != 0) {
  1073. $temp = $b;
  1074. $b = $a % $b;
  1075. $a = $temp;
  1076. }
  1077. return $a;
  1078. }
  1079. /**
  1080. * 创建素材
  1081. *
  1082. * @param array $params
  1083. * @return array
  1084. */
  1085. public function createAsset(array $params): array
  1086. {
  1087. try {
  1088. // 验证必填参数
  1089. if (empty($params['url'])) {
  1090. return [
  1091. 'code' => 1,
  1092. 'msg' => '素材URL不能为空',
  1093. 'data' => null
  1094. ];
  1095. }
  1096. // 先获取表中是否已有该url(仅限智真AI来源的素材)
  1097. $asset = DB::table('mp_assets')->where('url', $params['url'])->where('source_type', 'zhizhen')->first();
  1098. if ($asset) {
  1099. return [
  1100. 'code' => 0,
  1101. 'msg' => '创建成功',
  1102. 'data' => [
  1103. 'id' => $asset->id,
  1104. 'asset_id' => $asset->asset_id,
  1105. 'group_id' => $asset->group_id,
  1106. 'group_name' => $asset->group_name,
  1107. ]
  1108. ];
  1109. }
  1110. // 获取API配置
  1111. $apiKey = env('ZHIZHEN_AI_SK');
  1112. $apiUrl = 'https://model-relay.zzengine.net';
  1113. if (empty($apiKey)) {
  1114. return [
  1115. 'code' => 1002,
  1116. 'msg' => '智真AI API KEY未配置,请检查环境变量ZHIZHEN_AI_SK',
  1117. 'data' => null
  1118. ];
  1119. }
  1120. // 构建请求参数
  1121. $requestData = [
  1122. 'url' => $params['url'],
  1123. 'asset_type' => $params['asset_type'] ?? MpAsset::TYPE_IMAGE,
  1124. ];
  1125. // 可选参数
  1126. if (!empty($params['name'])) {
  1127. $requestData['name'] = $params['name'];
  1128. }
  1129. if (!empty($params['group_id'])) {
  1130. $requestData['group_id'] = $params['group_id'];
  1131. }
  1132. dLog('generate')->info('智真AI创建素材请求参数: ', $requestData);
  1133. // 调用智真AI API
  1134. $response = $this->httpClient->post($apiUrl . '/api/v1/asset/create', [
  1135. 'headers' => [
  1136. 'X-Api-Key' => $apiKey,
  1137. 'Content-Type' => 'application/json',
  1138. ],
  1139. 'json' => $requestData,
  1140. ]);
  1141. $responseData = json_decode($response->getBody()->getContents(), true);
  1142. dLog('generate')->info('智真AI创建素材响应: ', $responseData);
  1143. logDB('generate', 'info', '智真AI创建素材', ['params' => $requestData, 'response' => $responseData]);
  1144. if ($responseData['code'] !== 0) {
  1145. return [
  1146. 'code' => $responseData['code'],
  1147. 'msg' => $responseData['msg'] ?? '创建素材失败',
  1148. 'data' => null
  1149. ];
  1150. }
  1151. // 保存素材记录到数据库
  1152. $assetData = $responseData['data'] ?? [];
  1153. $asset = MpAsset::create([
  1154. 'asset_id' => $assetData['asset_id'] ?? '',
  1155. 'group_id' => $assetData['group_id'] ?? null,
  1156. 'group_name' => $assetData['group_name'] ?? null,
  1157. 'name' => $params['name'] ?? null,
  1158. 'url' => $params['url'],
  1159. 'asset_type' => $params['asset_type'] ?? MpAsset::TYPE_IMAGE,
  1160. 'source_type' => 'zhizhen',
  1161. 'status' => MpAsset::STATUS_PROCESSING,
  1162. 'extra_params' => $responseData,
  1163. ]);
  1164. return [
  1165. 'code' => 0,
  1166. 'msg' => '创建成功',
  1167. 'data' => [
  1168. 'id' => $asset->id,
  1169. 'asset_id' => $asset->asset_id,
  1170. 'group_id' => $asset->group_id,
  1171. 'group_name' => $asset->group_name,
  1172. ]
  1173. ];
  1174. } catch (\Exception $e) {
  1175. dLog('generate')->error('创建素材异常: ' . $e->getMessage());
  1176. logDB('generate', 'error', '创建素材异常', ['params' => $params, 'error' => $e->getMessage()]);
  1177. return [
  1178. 'code' => 20003,
  1179. 'msg' => '创建素材失败: ' . $e->getMessage(),
  1180. 'data' => null
  1181. ];
  1182. }
  1183. }
  1184. /**
  1185. * 查询素材
  1186. *
  1187. * @param string $assetId
  1188. * @return array
  1189. */
  1190. public function getAsset(string $assetId): array
  1191. {
  1192. try {
  1193. // 验证参数
  1194. if (empty($assetId)) {
  1195. return [
  1196. 'code' => 20003,
  1197. 'msg' => '素材ID不能为空',
  1198. 'data' => null
  1199. ];
  1200. }
  1201. // 先获取表中是否已有该素材(仅限智真AI来源的素材)
  1202. $assetInfo = DB::table('mp_assets')->where('asset_id', $assetId)->where('source_type', 'zhizhen')->first();
  1203. if ($assetInfo) {
  1204. $assetInfo = (array)$assetInfo;
  1205. $status = getProp($assetInfo, 'status');
  1206. if (in_array($status, ['Active', 'Failed'])) {
  1207. $assetInfo['id'] = $assetInfo['asset_id'];
  1208. return [
  1209. 'code' => 0,
  1210. 'msg' => '查询成功',
  1211. 'data' => [
  1212. 'asset' => $assetInfo
  1213. ]
  1214. ];
  1215. }
  1216. }
  1217. // 获取API配置
  1218. $apiKey = env('ZHIZHEN_AI_SK');
  1219. $apiUrl = 'https://model-relay.zzengine.net';
  1220. if (empty($apiKey)) {
  1221. return [
  1222. 'code' => 1002,
  1223. 'msg' => '智真AI API KEY未配置,请检查环境变量ZHIZHEN_AI_SK',
  1224. 'data' => null
  1225. ];
  1226. }
  1227. dLog('generate')->info('智真AI查询素材: ' . $assetId);
  1228. // 调用智真AI API
  1229. $response = $this->httpClient->post($apiUrl . '/api/v1/asset/get', [
  1230. 'headers' => [
  1231. 'X-Api-Key' => $apiKey,
  1232. 'Content-Type' => 'application/json',
  1233. ],
  1234. 'json' => [
  1235. 'asset_id' => $assetId,
  1236. ],
  1237. ]);
  1238. $responseData = json_decode($response->getBody()->getContents(), true);
  1239. dLog('generate')->info('智真AI查询素材响应: ', $responseData);
  1240. logDB('generate', 'info', '智真AI查询素材', ['asset_id' => $assetId, 'response' => $responseData]);
  1241. if ($responseData['code'] !== 0) {
  1242. return [
  1243. 'code' => $responseData['code'],
  1244. 'msg' => $responseData['msg'] ?? '查询素材失败',
  1245. 'data' => null
  1246. ];
  1247. }
  1248. $assetInfo = $responseData['data']['asset'] ?? [];
  1249. // 更新本地数据库记录
  1250. $localAsset = MpAsset::where('asset_id', $assetId)->first();
  1251. if ($localAsset) {
  1252. $localAsset->update([
  1253. 'status' => $assetInfo['status'] ?? MpAsset::STATUS_PROCESSING,
  1254. 'cover_url' => $assetInfo['url'] ?? null,
  1255. 'extra_params' => $responseData,
  1256. ]);
  1257. }
  1258. return [
  1259. 'code' => 0,
  1260. 'msg' => '查询成功',
  1261. 'data' => [
  1262. 'asset' => $assetInfo
  1263. ]
  1264. ];
  1265. } catch (\Exception $e) {
  1266. dLog('generate')->error('查询素材异常: ' . $e->getMessage());
  1267. logDB('generate', 'error', '查询素材异常', ['asset_id' => $assetId, 'error' => $e->getMessage()]);
  1268. return [
  1269. 'code' => 20003,
  1270. 'msg' => '查询素材失败: ' . $e->getMessage(),
  1271. 'data' => null
  1272. ];
  1273. }
  1274. }
  1275. /**
  1276. * 更新所有待处理任务的状态
  1277. *
  1278. * @return void
  1279. */
  1280. public function updatePendingTasks(): void
  1281. {
  1282. dLog('generate')->info('更新图片状态ing');
  1283. // 1. 处理火山API的pending任务(批量提交到队列)
  1284. $volcPendingTasks = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PENDING)
  1285. ->whereIn('model', BaseConst::VOLC_PIC_MODELS)
  1286. ->orderBy('created_at', 'asc')
  1287. ->limit(50) // 每次最多处理50个任务,避免超时
  1288. ->get();
  1289. foreach ($volcPendingTasks as $task) {
  1290. dLog('generate')->info('开始分发火山API任务到队列: ' . $task->task_id);
  1291. // 分发到队列
  1292. \App\Jobs\ProcessVolcImageGenerationJob::dispatch($task->id)->onConnection('redis');
  1293. }
  1294. // 2. 处理GPT-Image2的pending任务(批量提交到队列)
  1295. $gptImage2PendingTasks = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PENDING)
  1296. ->whereIn('model', BaseConst::GPT_IMAGE2_MODELS)
  1297. ->orderBy('created_at', 'asc')
  1298. ->limit(50) // 每次最多处理50个任务,避免超时
  1299. ->get();
  1300. foreach ($gptImage2PendingTasks as $task) {
  1301. dLog('generate')->info('开始分发GPT-Image2任务到队列: ' . $task->task_id);
  1302. // 分发到队列
  1303. \App\Jobs\ProcessGptImage2GenerationJob::dispatch($task->id)->onConnection('redis');
  1304. }
  1305. // 3. 获取所有处理中的任务(即梦AI和NanoBanana系列需要查询状态)
  1306. $processingTasks = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PROCESSING)
  1307. ->whereIn('model', array_merge([self::MODEL_JIMENG_4], BaseConst::NANO_BANANA_MODELS))
  1308. ->get();
  1309. foreach ($processingTasks as $task) {
  1310. $statusInfo = $this->queryTaskStatus($task);
  1311. if ($statusInfo['status'] === 'success') {
  1312. logDB('generate', 'info', '即梦AI图片生成任务成功', ['task_id' => $task->task_id, 'result_url' => $statusInfo['result_url']]);
  1313. // 图片生成成功统一处理(事务:状态更新 + 计费 + 分镜同步)
  1314. $this->handleImageTaskSuccess($task, $statusInfo['result_url'], $statusInfo['result_json'] ?? []);
  1315. } elseif ($statusInfo['status'] === 'failed') {
  1316. logDB('generate', 'error', '即梦AI图片生成任务失败', ['task_id' => $task->task_id, 'error' => $statusInfo['error_message']]);
  1317. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  1318. 'error_message' => $statusInfo['error_message'],
  1319. 'result_json' => $statusInfo['result_json'] ?? []
  1320. ]);
  1321. if (getProp($task, 'alias_segment_id')) {
  1322. DB::table('mp_episode_segments')->where('segment_id', getProp($task, 'alias_segment_id'))->update([
  1323. 'pic_task_status' => '失败',
  1324. ]);
  1325. }
  1326. }
  1327. // 如果仍然是处理中状态,不做任何操作
  1328. //处理:如果任务处理超过12小时,标记为失败
  1329. $processingTime = now()->diffInHours($task->created_at);
  1330. if ($processingTime > 12) {
  1331. logDB('generate', 'warning', '即梦AI图片生成任务超时', ['task_id' => $task->task_id, 'processing_hours' => $processingTime]);
  1332. $task->updateStatus(MpGeneratePicTask::STATUS_FAILED, [
  1333. 'error_message' => '任务处理超时(超过12小时)'
  1334. ]);
  1335. }
  1336. }
  1337. // 4. 检查是否有任务完成,如果有则处理下一个排队任务(仅即梦AI)
  1338. $this->processNextQueuedTask();
  1339. }
  1340. /**
  1341. * 处理下一个排队的任务
  1342. *
  1343. * @return void
  1344. */
  1345. public function processNextQueuedTask(): void
  1346. {
  1347. // 检查是否还有正在处理的任务(仅即梦AI)
  1348. if ($this->hasProcessingTask()) {
  1349. return; // 如果还有处理中的任务,不处理新任务
  1350. }
  1351. // 获取最早的待处理任务(仅即梦AI)
  1352. $nextTask = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PENDING)
  1353. ->where('model', self::MODEL_JIMENG_4)
  1354. ->orderBy('created_at', 'asc')
  1355. ->first();
  1356. if ($nextTask) {
  1357. dLog('generate')->info('开始处理排队任务: ' . $nextTask->task_id);
  1358. logDB('generate', 'info', '开始处理即梦AI排队任务', ['task_id' => $nextTask->task_id]);
  1359. $this->submitTaskToJimengApi($nextTask);
  1360. }
  1361. }
  1362. /**
  1363. * 获取任务队列状态
  1364. *
  1365. * @return array
  1366. */
  1367. public function getQueueStatus(): array
  1368. {
  1369. $processingCount = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PROCESSING)->count();
  1370. $pendingCount = MpGeneratePicTask::where('status', MpGeneratePicTask::STATUS_PENDING)->count();
  1371. return [
  1372. 'processing_count' => $processingCount,
  1373. 'pending_count' => $pendingCount,
  1374. 'can_submit_new_task' => $processingCount === 0
  1375. ];
  1376. }
  1377. /**
  1378. * 处理智真AI的参考图片转素材
  1379. *
  1380. * @param array $referenceImages 参考图片URL数组
  1381. * @param string &$fullPrompt 完整提示词(引用传递,用于删除失败图片的标记)
  1382. * @return array 返回素材ID数组
  1383. */
  1384. public function processReferenceImagesToAssets(array $referenceImages, string &$fullPrompt): array
  1385. {
  1386. $assetIds = [];
  1387. $failedIndexes = []; // 记录失败的图片序号
  1388. foreach ($referenceImages as $index => $imageUrl) {
  1389. $imageIndex = $index + 1; // 图片序号从1开始
  1390. try {
  1391. dLog('generate')->info("开始处理参考图片 {$imageIndex}: {$imageUrl}");
  1392. // 1. 创建素材
  1393. $createResult = $this->createAsset([
  1394. 'url' => $imageUrl,
  1395. 'asset_type' => MpAsset::TYPE_IMAGE,
  1396. 'name' => "参考图片_{$imageIndex}_" . time(),
  1397. ]);
  1398. if ($createResult['code'] !== 0) {
  1399. dLog('generate')->warning("图片 {$imageIndex} 创建素材失败: " . $createResult['msg']);
  1400. logDB('generate', 'warning', '创建素材失败', [
  1401. 'image_url' => $imageUrl,
  1402. 'index' => $imageIndex,
  1403. 'error' => $createResult['msg']
  1404. ]);
  1405. $failedIndexes[] = $imageIndex;
  1406. continue;
  1407. }
  1408. $assetId = $createResult['data']['asset_id'];
  1409. dLog('generate')->info("图片 {$imageIndex} 素材创建成功,asset_id: {$assetId},开始轮询查询状态");
  1410. // 2. 轮询查询素材状态(每3秒查询一次,最多查询100次即5分钟)
  1411. $maxAttempts = 100;
  1412. $attempt = 0;
  1413. $assetStatus = null;
  1414. while ($attempt < $maxAttempts) {
  1415. sleep(3); // 等待3秒
  1416. $attempt++;
  1417. $getResult = $this->getAsset($assetId);
  1418. if ($getResult['code'] !== 0) {
  1419. dLog('generate')->warning("图片 {$imageIndex} 查询素材状态失败: " . $getResult['msg']);
  1420. continue;
  1421. }
  1422. $assetStatus = $getResult['data']['asset']['status'] ?? null;
  1423. dLog('generate')->info("图片 {$imageIndex} 素材状态查询第 {$attempt} 次,状态: {$assetStatus}");
  1424. if ($assetStatus === 'Active') {
  1425. // 素材可用
  1426. $assetIds[] = $assetId;
  1427. dLog('generate')->info("图片 {$imageIndex} 素材处理成功,asset_id: {$assetId}");
  1428. logDB('generate', 'info', '素材转换成功', [
  1429. 'image_url' => $imageUrl,
  1430. 'index' => $imageIndex,
  1431. 'asset_id' => $assetId
  1432. ]);
  1433. break;
  1434. } elseif ($assetStatus === 'Failed') {
  1435. // 素材处理失败
  1436. dLog('generate')->warning("图片 {$imageIndex} 素材处理失败");
  1437. logDB('generate', 'warning', '素材处理失败', [
  1438. 'image_url' => $imageUrl,
  1439. 'index' => $imageIndex,
  1440. 'asset_id' => $assetId
  1441. ]);
  1442. $failedIndexes[] = $imageIndex;
  1443. break;
  1444. }
  1445. // 其他状态(Processing等)继续等待
  1446. }
  1447. // 如果超时还未成功
  1448. if ($attempt >= $maxAttempts && $assetStatus !== 'Active') {
  1449. dLog('generate')->warning("图片 {$imageIndex} 素材处理超时");
  1450. logDB('generate', 'warning', '素材处理超时', [
  1451. 'image_url' => $imageUrl,
  1452. 'index' => $imageIndex,
  1453. 'asset_id' => $assetId,
  1454. 'last_status' => $assetStatus
  1455. ]);
  1456. $failedIndexes[] = $imageIndex;
  1457. }
  1458. } catch (\Exception $e) {
  1459. dLog('generate')->error("图片 {$imageIndex} 处理异常: " . $e->getMessage());
  1460. logDB('generate', 'error', '素材转换异常', [
  1461. 'image_url' => $imageUrl,
  1462. 'index' => $imageIndex,
  1463. 'error' => $e->getMessage()
  1464. ]);
  1465. $failedIndexes[] = $imageIndex;
  1466. }
  1467. }
  1468. // 处理失败的图片:从 fullPrompt 中删除对应的标记并调整序号
  1469. if (!empty($failedIndexes)) {
  1470. dLog('generate')->info('开始处理失败图片的提示词调整,失败序号: ' . implode(',', $failedIndexes));
  1471. // 按序号从大到小排序,避免删除时影响后续序号
  1472. rsort($failedIndexes);
  1473. foreach ($failedIndexes as $failedIndex) {
  1474. // 删除失败图片的标记,例如 <图片1>、<图片2> 等
  1475. $pattern = "/<图片{$failedIndex}>/u";
  1476. $fullPrompt = preg_replace($pattern, '', $fullPrompt);
  1477. dLog('generate')->info("已从提示词中删除 <图片{$failedIndex}>");
  1478. }
  1479. // 重新调整剩余图片的序号
  1480. // 例如:如果图片2失败,图片3、图片4需要改为图片2、图片3
  1481. $needAdjust = false;
  1482. foreach ($failedIndexes as $failedIndex) {
  1483. $needAdjust = true;
  1484. // 将所有大于失败序号的图片标记序号减1
  1485. for ($i = $failedIndex + 1; $i <= count($referenceImages); $i++) {
  1486. $oldPattern = "/<图片{$i}>/u";
  1487. $newTag = "<图片" . ($i - 1) . ">";
  1488. $fullPrompt = preg_replace($oldPattern, $newTag, $fullPrompt);
  1489. }
  1490. }
  1491. if ($needAdjust) {
  1492. dLog('generate')->info('提示词图片序号调整完成');
  1493. logDB('generate', 'info', '提示词调整', [
  1494. 'failed_indexes' => $failedIndexes,
  1495. 'adjusted_prompt' => $fullPrompt
  1496. ]);
  1497. }
  1498. }
  1499. dLog('generate')->info('参考图片转素材完成,成功数量: ' . count($assetIds) . ',失败数量: ' . count($failedIndexes));
  1500. // 格式化为可直接在API接口中使用的数组
  1501. foreach($assetIds as &$assetId) {
  1502. $assetId = "asset://{$assetId}";
  1503. }
  1504. return $assetIds;
  1505. }
  1506. /**
  1507. * 根据宽高反推图片比例
  1508. * @param int $width 宽度
  1509. * @param int $height 高度
  1510. * @return string 比例字符串,如 "9:16",匹配不到则返回 "auto"
  1511. */
  1512. private function getRatioFromSize($width, $height)
  1513. {
  1514. if (!$width || !$height) {
  1515. return 'auto';
  1516. }
  1517. // 遍历所有预定义的比例,查找匹配的
  1518. foreach (BaseConst::IMAGE_RATIOS as $ratio => $size) {
  1519. if ($size['width'] == $width && $size['height'] == $height) {
  1520. return $ratio;
  1521. }
  1522. }
  1523. // 没有精确匹配,尝试通过计算比例匹配
  1524. $gcd = $this->gcd($width, $height);
  1525. $simplifiedRatio = ($width / $gcd) . ':' . ($height / $gcd);
  1526. // 检查简化后的比例是否在预定义列表中
  1527. if (isset(BaseConst::IMAGE_RATIOS[$simplifiedRatio])) {
  1528. return $simplifiedRatio;
  1529. }
  1530. return 'auto';
  1531. }
  1532. /**
  1533. * 压缩图片流(针对PNG格式)
  1534. *
  1535. * @param string $imageStream 图片二进制数据
  1536. * @param int $maxBytes 最大字节数(默认3MB)
  1537. * @return string|null 压缩后的图片二进制数据,失败时返回原数据
  1538. */
  1539. private function compressImageStream(string $imageStream, int $maxBytes = 3 * 1024 * 1024): ?string
  1540. {
  1541. try {
  1542. // 检查图片类型
  1543. $imgInfo = @getimagesizefromstring($imageStream);
  1544. if (!$imgInfo) {
  1545. dLog('generate')->warning('无法识别图片类型,返回原始数据');
  1546. return $imageStream;
  1547. }
  1548. $mime = $imgInfo['mime'] ?? '';
  1549. // 只处理PNG,JPEG不做有损压缩
  1550. if ($mime === 'image/jpeg') {
  1551. return $imageStream;
  1552. }
  1553. // 如果已经小于最大限制,直接返回
  1554. if (strlen($imageStream) <= $maxBytes) {
  1555. return $imageStream;
  1556. }
  1557. // 载入图像对象
  1558. $srcImg = @imagecreatefromstring($imageStream);
  1559. if (!$srcImg) {
  1560. dLog('generate')->warning('无法创建图像资源,返回原始数据');
  1561. return $imageStream;
  1562. }
  1563. $origW = imagesx($srcImg);
  1564. $origH = imagesy($srcImg);
  1565. // 逐步降低质量直到满足大小要求
  1566. $quality = 9; // PNG压缩等级 0-9
  1567. $scaleFactor = 1.0;
  1568. $maxAttempts = 10;
  1569. $attempt = 0;
  1570. $compressedData = null;
  1571. while ($attempt < $maxAttempts) {
  1572. $attempt++;
  1573. // 如果需要缩放
  1574. if ($scaleFactor < 1.0) {
  1575. $newW = (int)($origW * $scaleFactor);
  1576. $newH = (int)($origH * $scaleFactor);
  1577. $resizedImg = imagecreatetruecolor($newW, $newH);
  1578. // 保持透明度
  1579. imagealphablending($resizedImg, false);
  1580. imagesavealpha($resizedImg, true);
  1581. imagecopyresampled($resizedImg, $srcImg, 0, 0, 0, 0, $newW, $newH, $origW, $origH);
  1582. $targetImg = $resizedImg;
  1583. } else {
  1584. $targetImg = $srcImg;
  1585. }
  1586. // 输出到内存
  1587. ob_start();
  1588. imagepng($targetImg, null, $quality);
  1589. $compressedData = ob_get_clean();
  1590. if ($scaleFactor < 1.0) {
  1591. imagedestroy($targetImg);
  1592. }
  1593. // 检查大小
  1594. $size = strlen($compressedData);
  1595. dLog('generate')->info('图片压缩尝试', [
  1596. 'attempt' => $attempt,
  1597. 'quality' => $quality,
  1598. 'scale' => $scaleFactor,
  1599. 'size' => $size,
  1600. 'target' => $maxBytes
  1601. ]);
  1602. if ($size <= $maxBytes) {
  1603. break;
  1604. }
  1605. // 调整参数
  1606. if ($quality > 0) {
  1607. $quality--;
  1608. } else {
  1609. $scaleFactor *= 0.9; // 每次缩小10%
  1610. }
  1611. }
  1612. imagedestroy($srcImg);
  1613. return $compressedData ?: $imageStream;
  1614. } catch (\Exception $e) {
  1615. dLog('generate')->error('图片压缩失败', ['error' => $e->getMessage()]);
  1616. return $imageStream;
  1617. }
  1618. }
  1619. /**
  1620. * 从响应数据中移除 b64_json 字段
  1621. * 避免在日志和数据库中存储大量 Base64 数据
  1622. *
  1623. * @param array $responseData
  1624. * @return array
  1625. */
  1626. private function removeB64JsonFromResponse(array $responseData): array
  1627. {
  1628. $cleanedData = $responseData;
  1629. if (isset($cleanedData['data']) && is_array($cleanedData['data'])) {
  1630. foreach ($cleanedData['data'] as &$imageData) {
  1631. if (isset($imageData['b64_json'])) {
  1632. // 移除 b64_json 字段,可选择性保留数据长度信息
  1633. $b64Length = strlen($imageData['b64_json']);
  1634. unset($imageData['b64_json']);
  1635. $imageData['b64_json_removed'] = true;
  1636. $imageData['b64_json_length'] = $b64Length;
  1637. }
  1638. }
  1639. unset($imageData); // 解除引用
  1640. }
  1641. return $cleanedData;
  1642. }
  1643. }