| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- namespace app\job;
- use think\Db;
- use think\Queue;
- use think\queue\Job;
- class TextToImageJob
- {
- 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 fire(Job $job, $data)
- {
- echo date('Y-m-d H:i:s') . " 任务开始执行\n";
- try {
- if (isset($data['type']) && $data['type'] === '文生图') {
- echo date('Y-m-d H:i:s') . " [文生图 - 数据库模式] 阶段开始\n";
- $list = Db::name("text_to_image")
- ->where('old_image_url', $data['sourceDir'] . '/' . $data['file_name'])
- ->where('img_name', '<>', '')
- ->where('status', 0)
- ->select();
- if (!empty($list)) {
- foreach ($list as $index => $row) {
- echo "处理第 " . ($index + 1) . " 条数据,处理ID:" . $row['id'] . "\n";
- // 调用 textToImage 方法
- $result = $this->textToImage(
- $data["file_name"], // 文件名
- $data["outputDir"], // 输出目录
- $data["width"], // 宽度
- $data["height"], // 高度
- $row["english_description"], // 英文描述
- $row["img_name"] // 图片名
- );
- echo "处理结果:" . $result . "\n";
- }
- echo date('Y-m-d H:i:s') . " 文生图任务全部执行完成\n";
- } else {
- echo "未找到 status=0 的数据,跳过执行\n";
- }
- $job->delete(); // 删除队列任务
- } else {
- echo date('Y-m-d H:i:s') . " [文生图 - 单条传参模式] 开始执行\n";
- $result = $this->textToImage(
- $data["fileName"],
- $data["outputDir"],
- $data["width"],
- $data["height"],
- $data["englishDesc"],
- $data["img_name"]
- );
- echo "返回结果:" . $result . "\n";
- echo date('Y-m-d H:i:s') . " 单条文生图执行结束\n";
- $job->delete();
- }
- } catch (\Exception $e) {
- echo "异常信息: " . $e->getMessage() . "\n";
- echo "文件: " . $e->getFile() . "\n";
- echo "行号: " . $e->getLine() . "\n";
- echo date('Y-m-d H:i:s') . " 任务执行失败\n";
- $job->delete();
- }
- }
- /**
- * 任务失败时的处理
- */
- public function failed($data)
- {
- // 记录失败日志或发送通知
- echo "ImageJob failed: " . json_encode($data);
- }
- /**
- * 文生图接口
- */
- public function textToImage($fileName, $outputDirRaw, $width, $height, $prompt, $img_name)
- {
- $rootPath = str_replace('\\', '/', ROOT_PATH);
- $outputDir = rtrim($rootPath . 'public/' . $outputDirRaw, '/') . '/';
- $dateDir = date('Y-m-d') . '/';
- $fullBaseDir = $outputDir . $dateDir;
- // 创建输出目录结构
- foreach ([$fullBaseDir, $fullBaseDir . '1024x1024/', $fullBaseDir . "{$width}x{$height}/"] as $dir) {
- if (!is_dir($dir)) {
- mkdir($dir, 0755, true);
- }
- }
- // 清理 prompt 的换行
- $prompt = preg_replace('/[\r\n\t]+/', ' ', $prompt);
- // 查询数据库记录
- $record = Db::name('text_to_image')
- ->where('old_image_url', 'like', "%{$fileName}")
- ->order('id desc')
- ->find();
- if (!$record) {
- return '没有找到匹配的图像记录';
- }
- // 写入 prompt 日志
- $logDir = $rootPath . 'runtime/logs/';
- if (!is_dir($logDir)) mkdir($logDir, 0755, true);
- // echo 2345;
- // 调用文生图模型接口生成图像
- $startTime = microtime(true);
- // echo "<pre>";
- // print_r($prompt);
- // echo "<pre>";die;
- $dalle1024 = $this->callDalleApi($prompt);
- // $dalle1024 = json_decode('{"created":1747932746,"data":[{"revised_prompt":"**First paragraph:** A geometric abstract design with a central motif consisting of curved and angular shapes in a symmetrical arrangement. The color scheme predominantly features shades of blue, with hints of white and black creating a contrast. The design incorporates smooth, flowing lines mixed with sharp angles. The overall style has a modern, minimalist aesthetic, with a focus on balance and clean shapes.","url":"https:\/\/filesystem.site\/cdn\/20250523\/3NVcCUaZDkLimWjtgOwJYniGezDX8d.png"}],"usage":{"total_tokens":4250,"input_tokens":75,"output_tokens":4175,"input_tokens_details":{"text_tokens":75}}}',true);
- $endTime = microtime(true);
- $executionTime = $endTime - $startTime;
- echo "API调用耗时: " . round($executionTime, 3) . " 秒\n";
- // 检查 URL 返回是否成功
- if (!isset($dalle1024['data'][0]['url']) || empty($dalle1024['data'][0]['url'])) {
- $errorText = $dalle1024['error']['message'] ?? '未知错误';
- echo '生成失败:' . $errorText;
- }
- // echo 342342;
- // 下载图像
- $imgUrl1024 = $dalle1024['data'][0]['url'];
- $imgData1024 = @file_get_contents($imgUrl1024);
- if (!$imgData1024 || strlen($imgData1024) < 1000) {
- return "下载图像失败或内容异常";
- }
- // echo 3423424444;
- // file_put_contents(
- // $logDir . 'img_name.txt',
- // "\n====图片 " . date('Y-m-d H:i:s') . " ====\n" . $img_name . "\n\n",
- // FILE_APPEND
- // );
- // 保存原图(1024x1024)
- // $img_name = $this->limitStringLength($img_name);
- $img_name = preg_replace('/[^\x{4e00}-\x{9fa5}A-Za-z0-9_\- ]/u', '', $img_name);
- $img_name = mb_substr($img_name, 0, 30); // 限制为前30个字符(避免路径过长)
- $filename1024 = $img_name . '.png';
- $savePath1024 = $fullBaseDir . '1024x1024/' . $filename1024;
- file_put_contents($savePath1024, $imgData1024);
- // echo 342344543532;
- // 处理缩略图
- $im = @imagecreatefromstring($imgData1024);
- if (!$im) return "图像格式不受支持或已损坏";
- $srcWidth = imagesx($im);
- $srcHeight = imagesy($im);
- $srcRatio = $srcWidth / $srcHeight;
- $dstRatio = $width / $height;
- // 居中裁剪逻辑
- if ($srcRatio > $dstRatio) {
- $cropHeight = $srcHeight;
- $cropWidth = intval($srcHeight * $dstRatio);
- $srcX = intval(($srcWidth - $cropWidth) / 2);
- $srcY = 0;
- } else {
- $cropWidth = $srcWidth;
- $cropHeight = intval($srcWidth / $dstRatio);
- $srcX = 0;
- $srcY = intval(($srcHeight - $cropHeight) / 2);
- }
- // echo 789;
- $dstImg = imagecreatetruecolor($width, $height);
- imagecopyresampled($dstImg, $im, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight);
- // 保存裁剪后图像
- $filenameCustom = $img_name . ".png";
- $savePathCustom = $fullBaseDir . "{$width}x{$height}/" . $filenameCustom;
- imagepng($dstImg, $savePathCustom);
- imagedestroy($im);
- imagedestroy($dstImg);
- // file_put_contents(
- // $logDir . 'image_url.txt',
- // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePath1024) . "\n\n",
- // "\n====图片路径 " . date('Y-m-d H:i:s') . " ====\n" . str_replace($rootPath . 'public/', '', $savePathCustom) . "\n\n",
- // FILE_APPEND
- // );
- $status = trim($img_name) === '' ? 0 : 1;
- // 更新数据库记录
- $updateRes = Db::name('text_to_image')->where('id', $record['id'])->update([
- 'new_image_url' => str_replace($rootPath . 'public/', '', $savePath1024),
- 'custom_image_url' => str_replace($rootPath . 'public/', '', $savePathCustom),
- 'img_name' => $img_name,
- 'error_msg' => '',
- 'size' => "{$width}x{$height}",
- 'updated_time' => date('Y-m-d H:i:s'),
- 'status' => $status
- ]);
- return 0;
- }
- /**
- * 处理字符串长度,超出限制则截断
- *
- * @param string $str 输入字符串
- * @param int $maxLength 最大长度限制(默认200)
- * @return string 处理后的字符串
- */
- public function limitStringLength($str, $maxLength = 10)
- {
- // 如果字符串长度没有超出限制,直接返回
- if (mb_strlen($str, 'UTF-8') <= $maxLength) {
- return $str;
- }
- // 超出限制则截断
- return mb_substr($str, 0, $maxLength, 'UTF-8');
- }
- public function cleanImageUrl($input) {
- // 去除字符串首尾空格和中文引号替换为英文引号
- $input = trim($input);
- $input = str_replace(['“', '”', '‘', '’'], '"', $input);
- // 判断是否为纯中文文字
- if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $input)) {
- // 纯中文:替换掉不适合用于文件名的字符
- $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
- } elseif (preg_match('/[a-zA-Z]/', $input) && !preg_match('/[\x{4e00}-\x{9fa5}]/u', $input)) {
- // 如果是纯字母和空格,且没有中文字符:保留空格,去掉其他符号
- $cleaned = preg_replace('/[^a-zA-Z\s]/', '', $input);
- } else {
- // 如果包含中文或是其他混合字符,按照纯中文的规则清理符号
- $cleaned = preg_replace('/[\/\\\:\*\?"<>\|,。!¥【】、;‘’“”《》\s]+/u', '', $input);
- }
- return $cleaned;
- }
- /**
- * 文生图模型
- */
- public function callDalleApi($prompt)
- {
- $data = [
- 'prompt' => $prompt,
- // 'model' => 'dall-e-2',
- 'model' => 'gpt-image-1',
- 'n' => 1,
- 'size' => '1024x1024',
- 'quality' => 'standard',
- 'style' => 'vivid',
- 'response_format' => 'url'
- ];
- return $this->callApi($this->config['dalle']['api_url'], $this->config['dalle']['api_key'], $data);
- }
- /**
- * 通用API调用方法
- */
- 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 => 120,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_TCP_KEEPALIVE => 1,
- CURLOPT_FORBID_REUSE => false
- ]);
- $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);
- return $result;
- }
- $lastError = $curlError ?: "HTTP错误:{$httpCode}";
- $attempt++;
- sleep(1);
- }
- throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
- }
- }
|