| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\job\ImageJob;
- use app\job\InsertDataJob;
- use think\Db;
- use think\Log;
- use think\Queue;
- class WorkOrderVerification extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- protected $config = [
- 'gpt' => [
- 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
- 'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
- ],
- 'dalle' => [
- 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
- 'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
- ]
- ];
- public function startGenerateImage()
- {
- //插入队列
- $job = new ImageJob(); // 创建任务实例
- // 推送任务到队列
- //批量图片插入
- $imgs = [
- "202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
- "202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
- "202511157-zwu7493_Vintage_Sunflower_Bunch_on_Vintage_Background_sunflower_8f02bc13-354c-474c-acca-65d45585f885.png",
- "202511157-zwu7493_Vintage_Sunflower_Bunch_on_Vintage_Background_sunflower_8f02bc13-354c-474c-acca-65d45585f885.png",
- "202511199-jz1229_watercolor_jungle_leaves_white_background_light_blue_and_053c2fbb-beca-45e7-a812-ed76f311cc12.png",
- "202511199-jz1229_watercolor_jungle_leaves_white_background_light_blue_and_053c2fbb-beca-45e7-a812-ed76f311cc12.png",
- "202514598-zwu7493_Watercolor_Succulents_Beautiful_Colors_and_Light_Dreamy_85cbc0d4-c729-429f-a9d8-b2042993be3f.png",
- "202514598-zwu7493_Watercolor_Succulents_Beautiful_Colors_and_Light_Dreamy_85cbc0d4-c729-429f-a9d8-b2042993be3f.png"
- ];
- $arr = [];
- foreach ($imgs as $k=>$v){
- $arr[$k] = [
- "dir_name"=>"/uploads/operate/ai/Preview/20250508",
- "file_name"=>$v,
- "prompt"=>"请按以下要求分析图案,详细描述的图案信息提示词,描述的信息只保留图案内容原版风格仅限图案本体,生成的描述信息",
- "outputDirRaw"=>"/uploads/operate/ai/dall-e/",
- "width"=>"679",
- "height"=>"862"
- ];
- }
- // $arr = [
- // [
- // "dir_name"=>"/uploads/operate/ai/Preview/20250508",
- // "file_name"=>"202506763-jz1229_A_painting_of_sunflowers_with_each_flower_having_six_pet_c65bd1f7-8a30-4774-ad12-05ff34fa1b21.png",
- // "prompt"=>"请按以下要求分析图案,详细描述的图案信息提示词,描述的信息只保留图案内容原版风格仅限图案本体,生成的描述信息",
- // "outputDirRaw"=>"/uploads/operate/ai/dall-e/",
- // "width"=>"679",
- // "height"=>"862"
- // ],
- // ];
- foreach ($arr as $key => $value){
- Queue::push('app\job\ImageJob',$value); // 推送任务到队列
- }
- }
- public function callGptApi($imageUrl, $prompt)
- {
- $data = [
- "model" => "gpt-4-vision-preview",
- "messages" => [[
- "role" => "user",
- "content" => [
- ["type" => "text", "text" => $prompt],
- ["type" => "image_url", "image_url" => [
- "url" => $imageUrl,
- "detail" => "auto"
- ]]
- ]
- ]],
- "max_tokens" => 1000
- ];
- return $this->callApi($this->config['gpt']['api_url'], $this->config['gpt']['api_key'], $data);
- }
- public function callDalleApi($prompt)
- {
- $data = [
- 'prompt' => $prompt,
- 'model' => 'dall-e-2',
- 'n' => 1,
- 'size' => '1024x1024'
- ];
- return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
- }
- public function callApi($url, $apiKey, $data)
- {
- $maxRetries = 2;
- $attempt = 0;
- $lastError = '';
- while ($attempt <= $maxRetries) {
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- 'Authorization: Bearer ' . $apiKey
- ],
- CURLOPT_TIMEOUT => 180,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => 0
- ]);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $curlError = curl_error($ch);
- curl_close($ch);
- if ($response !== false && $httpCode === 200) {
- $result = json_decode($response, true);
- if (json_last_error() === JSON_ERROR_NONE) return $result;
- }
- $lastError = $curlError ?: "HTTP错误:{$httpCode}";
- $attempt++;
- sleep(1);
- }
- return ['error' => true, 'msg' => $lastError];
- }
- public function logToDatabase($data)
- {
- $record = [
- 'old_image_url' => $data['old_image_url'] ?? '',
- 'new_image_url' => $data['new_image_url'] ?? '',
- 'custom_image_url' => $data['custom_image_url'] ?? '',
- 'size' => isset($data['image_width'], $data['image_height']) ?
- $data['image_width'] . 'x' . $data['image_height'] : '',
- 'chinese_description' => $data['chinese_description'] ?? '',
- 'english_description' => $data['english_description'] ?? '',
- 'model' => 'dall-e-2',
- 'quality' => 'standard',
- 'style' => 'vivid',
- 'status' => $data['status'] ?? 0,
- 'error_msg' => $data['error_msg'] ?? '',
- 'created_time' => date('Y-m-d H:i:s'),
- 'updated_time' => date('Y-m-d H:i:s')
- ];
- if (isset($data['id'])) {
- Db::name('text_to_image')->where('id', $data['id'])->update($record);
- } else {
- Db::name('text_to_image')->insert($record);
- }
- }
- }
|