WorkOrderVerification.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\job\ImageJob;
  5. use app\job\InsertDataJob;
  6. use think\Db;
  7. use think\Log;
  8. use think\Queue;
  9. class WorkOrderVerification extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. protected $config = [
  14. 'gpt' => [
  15. 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
  16. 'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
  17. ],
  18. 'dalle' => [
  19. 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
  20. 'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
  21. ]
  22. ];
  23. public function startGenerateImage()
  24. {
  25. //插入队列
  26. $job = new ImageJob(); // 创建任务实例
  27. // 推送任务到队列
  28. //批量图片插入
  29. $imgs = [
  30. "202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
  31. "202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
  32. "202511157-zwu7493_Vintage_Sunflower_Bunch_on_Vintage_Background_sunflower_8f02bc13-354c-474c-acca-65d45585f885.png",
  33. "202511157-zwu7493_Vintage_Sunflower_Bunch_on_Vintage_Background_sunflower_8f02bc13-354c-474c-acca-65d45585f885.png",
  34. "202511199-jz1229_watercolor_jungle_leaves_white_background_light_blue_and_053c2fbb-beca-45e7-a812-ed76f311cc12.png",
  35. "202511199-jz1229_watercolor_jungle_leaves_white_background_light_blue_and_053c2fbb-beca-45e7-a812-ed76f311cc12.png",
  36. "202514598-zwu7493_Watercolor_Succulents_Beautiful_Colors_and_Light_Dreamy_85cbc0d4-c729-429f-a9d8-b2042993be3f.png",
  37. "202514598-zwu7493_Watercolor_Succulents_Beautiful_Colors_and_Light_Dreamy_85cbc0d4-c729-429f-a9d8-b2042993be3f.png"
  38. ];
  39. $arr = [];
  40. foreach ($imgs as $k=>$v){
  41. $arr[$k] = [
  42. "dir_name"=>"/uploads/operate/ai/Preview/20250508",
  43. "file_name"=>$v,
  44. "prompt"=>"请按以下要求分析图案,详细描述的图案信息提示词,描述的信息只保留图案内容原版风格仅限图案本体,生成的描述信息",
  45. "outputDirRaw"=>"/uploads/operate/ai/dall-e/",
  46. "width"=>"679",
  47. "height"=>"862"
  48. ];
  49. }
  50. // $arr = [
  51. // [
  52. // "dir_name"=>"/uploads/operate/ai/Preview/20250508",
  53. // "file_name"=>"202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
  54. // "prompt"=>"请按以下要求分析图案,详细描述的图案信息提示词,描述的信息只保留图案内容原版风格仅限图案本体,生成的描述信息",
  55. // "outputDirRaw"=>"/uploads/operate/ai/dall-e/",
  56. // "width"=>"679",
  57. // "height"=>"862"
  58. // ],
  59. // ];
  60. foreach ($arr as $key => $value){
  61. Queue::push('app\job\ImageJob',$value); // 推送任务到队列
  62. }
  63. }
  64. public function callGptApi($imageUrl, $prompt)
  65. {
  66. $data = [
  67. "model" => "gpt-4-vision-preview",
  68. "messages" => [[
  69. "role" => "user",
  70. "content" => [
  71. ["type" => "text", "text" => $prompt],
  72. ["type" => "image_url", "image_url" => [
  73. "url" => $imageUrl,
  74. "detail" => "auto"
  75. ]]
  76. ]
  77. ]],
  78. "max_tokens" => 1000
  79. ];
  80. return $this->callApi($this->config['gpt']['api_url'], $this->config['gpt']['api_key'], $data);
  81. }
  82. public function callDalleApi($prompt)
  83. {
  84. $data = [
  85. 'prompt' => $prompt,
  86. 'model' => 'dall-e-2',
  87. 'n' => 1,
  88. 'size' => '1024x1024'
  89. ];
  90. return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
  91. }
  92. public function callApi($url, $apiKey, $data)
  93. {
  94. $maxRetries = 2;
  95. $attempt = 0;
  96. $lastError = '';
  97. while ($attempt <= $maxRetries) {
  98. $ch = curl_init();
  99. curl_setopt_array($ch, [
  100. CURLOPT_URL => $url,
  101. CURLOPT_RETURNTRANSFER => true,
  102. CURLOPT_POST => true,
  103. CURLOPT_POSTFIELDS => json_encode($data),
  104. CURLOPT_HTTPHEADER => [
  105. 'Content-Type: application/json',
  106. 'Authorization: Bearer ' . $apiKey
  107. ],
  108. CURLOPT_TIMEOUT => 180,
  109. CURLOPT_SSL_VERIFYPEER => false,
  110. CURLOPT_SSL_VERIFYHOST => 0
  111. ]);
  112. $response = curl_exec($ch);
  113. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  114. $curlError = curl_error($ch);
  115. curl_close($ch);
  116. if ($response !== false && $httpCode === 200) {
  117. $result = json_decode($response, true);
  118. if (json_last_error() === JSON_ERROR_NONE) return $result;
  119. }
  120. $lastError = $curlError ?: "HTTP错误:{$httpCode}";
  121. $attempt++;
  122. sleep(1);
  123. }
  124. return ['error' => true, 'msg' => $lastError];
  125. }
  126. public function logToDatabase($data)
  127. {
  128. $record = [
  129. 'old_image_url' => $data['old_image_url'] ?? '',
  130. 'new_image_url' => $data['new_image_url'] ?? '',
  131. 'custom_image_url' => $data['custom_image_url'] ?? '',
  132. 'size' => isset($data['image_width'], $data['image_height']) ?
  133. $data['image_width'] . 'x' . $data['image_height'] : '',
  134. 'chinese_description' => $data['chinese_description'] ?? '',
  135. 'english_description' => $data['english_description'] ?? '',
  136. 'model' => 'dall-e-2',
  137. 'quality' => 'standard',
  138. 'style' => 'vivid',
  139. 'status' => $data['status'] ?? 0,
  140. 'error_msg' => $data['error_msg'] ?? '',
  141. 'created_time' => date('Y-m-d H:i:s'),
  142. 'updated_time' => date('Y-m-d H:i:s')
  143. ];
  144. if (isset($data['id'])) {
  145. Db::name('text_to_image')->where('id', $data['id'])->update($record);
  146. } else {
  147. Db::name('text_to_image')->insert($record);
  148. }
  149. }
  150. }