| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\job\ImageJob;
- use app\service\ImageService;
- use think\App;
- use think\Db;
- use think\Log;
- use think\Queue;
- use think\queue\job\Redis;
- class WorkOrder extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 出图接口
- * 此方法处理图像转换为文本的请求,将图像信息存入队列以供后续处理。
- */
- public function imageToText()
- {
- $params = $this->request->param();
- $service = new ImageService();
- $service->handleImage($params);
- $this->success('任务成功提交至队列');
- }
- /**
- * 图生图
- * /sdapi/v1/img2img
- */
- public function imgtowimg()
- {
- $prompt = $this->request->param('prompt', '');
- $denoising = (float)$this->request->param('denoising_strength', 0.2);
- $scale = (float)$this->request->param('scale', 2.0);
- $modelName = $this->request->param('model', 'realisticVisionV51_v51VAE-inpainting.safetensors [f0d4872d24]');
- // 原图路径
- $imgRelPath = 'uploads/operate/ai/Preview/arr/0828004096727.png';
- $imgPath = ROOT_PATH . 'public/' . $imgRelPath;
- if (!file_exists($imgPath)) {
- return json(['code' => 1, 'msg' => '原图不存在:' . $imgRelPath]);
- }
- // 获取原图尺寸 × 倍数
- list($originW, $originH) = getimagesize($imgPath);
- $targetW = intval($originW * $scale);
- $targetH = intval($originH * $scale);
- // base64 编码
- $imgData = file_get_contents($imgPath);
- $base64Img = base64_encode($imgData);
- $initImage = 'data:image/png;base64,' . $base64Img;
- // 请求体
- $postData = json_encode([
- 'prompt' => $prompt,
- 'steps' => 30,
- 'cfg_scale' => 7,
- 'denoising_strength' => $denoising,
- 'width' => $targetW,
- 'height' => $targetH,
- 'resize_mode' => 1,
- 'inpaint_full_res' => true,
- 'inpainting_fill' => 1,
- 'init_images' => [$initImage],
- 'override_settings' => [
- 'sd_model_checkpoint' => $modelName
- ]
- ]);
- $apiUrl = "http://20.0.17.233:45001/sdapi/v1/img2img";
- $headers = ['Content-Type: application/json'];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $apiUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_TIMEOUT, 90);
- $response = curl_exec($ch);
- $error = curl_error($ch);
- curl_close($ch);
- if ($error) {
- return json(['code' => 1, 'msg' => '请求失败:' . $error]);
- }
- $data = json_decode($response, true);
- if (!isset($data['images'][0])) {
- return json(['code' => 1, 'msg' => '接口未返回图像数据']);
- }
- // 保存图像:原图名 + -1
- $resultImg = base64_decode($data['images'][0]);
- $saveDir = ROOT_PATH . 'public/uploads/img2img/';
- if (!is_dir($saveDir)) {
- mkdir($saveDir, 0755, true);
- }
- $originalBaseName = pathinfo($imgRelPath, PATHINFO_FILENAME);
- $fileName = $originalBaseName . '-1.png';
- $savePath = $saveDir . $fileName;
- file_put_contents($savePath, $resultImg);
- return json([
- 'code' => 0,
- 'msg' => '图像生成成功',
- 'data' => [
- 'origin_url' => '/uploads/img2img/' . $fileName
- ]
- ]);
- }
- /**
- * 后期图像处理
- * /sdapi/v1/extra-single-image
- */
- public function extra_image()
- {
- $imgRelPath = 'uploads/img2img/0828004096727-1.png'; // 图生图结果
- $imgPath = ROOT_PATH . 'public/' . $imgRelPath;
- if (!file_exists($imgPath)) {
- return json(['code' => 1, 'msg' => '原图不存在:' . $imgRelPath]);
- }
- $imgData = file_get_contents($imgPath);
- $base64Img = base64_encode($imgData);
- $postData = json_encode([
- 'resize_mode' => 0,
- 'show_extras_results' => true,
- 'gfpgan_visibility' => 0,
- 'codeformer_visibility' => 0,
- 'codeformer_weight' => 0,
- 'upscaling_resize' => 2.45,
- 'upscaling_crop' => true,
- 'upscaler_1' => 'R-ESRGAN 4x+ Anime6B',
- 'upscaler_2' => 'None',
- 'extras_upscaler_2_visibility' => 0,
- 'upscale_first' => false,
- 'image' => $base64Img
- ]);
- $apiUrl = "http://20.0.17.233:45001/sdapi/v1/extra-single-image";
- $headers = ['Content-Type: application/json'];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $apiUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_TIMEOUT, 90);
- $response = curl_exec($ch);
- $error = curl_error($ch);
- curl_close($ch);
- if ($error) {
- return json(['code' => 1, 'msg' => '请求失败:' . $error]);
- }
- $data = json_decode($response, true);
- if (!isset($data['image'])) {
- return json(['code' => 1, 'msg' => '接口未返回图像数据']);
- }
- // 保存:原图基础名 + -2
- $resultImg = base64_decode($data['image']);
- $saveDir = ROOT_PATH . 'public/uploads/extra_image/';
- if (!is_dir($saveDir)) {
- mkdir($saveDir, 0755, true);
- }
- $originalBaseName = pathinfo($imgRelPath, PATHINFO_FILENAME);
- $fileName = $originalBaseName . '-2.png';
- $savePath = $saveDir . $fileName;
- file_put_contents($savePath, $resultImg);
- return json([
- 'code' => 0,
- 'msg' => '图像后处理成功',
- 'data' => [
- 'url' => '/uploads/extra_image/' . $fileName
- ]
- ]);
- }
- /**
- * 查询队列列表
- * 统计文件对应的队列情况
- */
- public function get_queue_logs()
- {
- $params = $this->request->param('old_image_file', '');
- $queue_logs = Db::name('queue_logs')
- ->where('old_image_file', $params)
- ->order('id desc')
- ->select();
- $result = []; //初始化变量,避免未定义错误
- foreach ($queue_logs as &$log) {
- $taskId = $log['id'];
- $statusCount = Db::name('image_task_log')
- ->field('status, COUNT(*) as count')
- ->where('task_id', $taskId)
- ->where('mod_rq', null)
- ->group('status')
- ->select();
- $log['已完成数量'] = 0;
- $log['处理中数量'] = 0;
- $log['排队中的数量'] = 0;
- $log['失败数量'] = 0;
- foreach ($statusCount as $item) {
- switch ($item['status']) {
- case 0:
- $log['排队中的数量'] = $item['count'];
- break;
- case 1:
- $log['处理中数量'] = $item['count'];
- break;
- case 2:
- $log['已完成数量'] = $item['count'];
- break;
- case -1:
- $log['失败数量'] = $item['count'];
- break;
- }
- }
- // if ($log['排队中的数量'] >$log['已完成数量']) {
- // $result[] = $log;
- // }
- if ($log['排队中的数量']) {
- $result[] = $log;
- }
- // if ($log['处理中数量'] >= 0) {
- // $result[] = $log;
- // }
- }
- return json([
- 'code' => 0,
- 'msg' => '查询成功',
- 'data' => $result,
- 'count' => count($result)
- ]);
- }
- /**
- * 查询总队列状态(统计当前处理的数据量)
- */
- public function queueStats()
- {
- $statusList = Db::name('image_task_log')
- ->field('status, COUNT(*) as total')
- ->where('mod_rq', null)
- ->where('create_time', '>=', date('Y-m-d 00:00:00'))
- ->group('status')
- ->select();
- $statusCount = [];
- foreach ($statusList as $item) {
- $statusCount[$item['status']] = $item['total'];
- }
- // 总数为所有状态和
- $total = array_sum($statusCount);
- //获取队列当前状态
- $statusText = Db::name('queue_logs')->order('id desc')->value('status');
- return json([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- '总任务数' => $total,
- '待处理' => $statusCount[0] ?? 0,
- '处理中' => $statusCount[1] ?? 0,
- '成功' => $statusCount[2] ?? 0,
- '失败' => $statusCount[-1] ?? 0,
- '当前状态' => $statusText
- ]
- ]);
- }
- /**
- * 显示当前运行中的队列监听进程
- */
- public function viewQueueStatus()
- {
- $redis = new \Redis();
- $redis->connect('127.0.0.1', 6379);
- $redis->auth('123456');
- $redis->select(15);
- $key = 'queues:imgtotxt';
- // 判断 key 是否存在,避免报错
- if (!$redis->exists($key)) {
- return json([
- 'code' => 0,
- 'msg' => '查询成功,队列为空',
- 'count' => 0,
- 'tasks_preview' => []
- ]);
- }
- $count = $redis->lLen($key);
- $list = $redis->lRange($key, 0, 9);
- // 解码 JSON 内容,确保每一项都有效
- $parsed = array_filter(array_map(function ($item) {
- return json_decode($item, true);
- }, $list), function ($item) {
- return !is_null($item);
- });
- return json([
- 'code' => 0,
- 'msg' => '查询成功',
- 'count' => $count,
- 'tasks_preview' => $parsed
- ]);
- }
- /**
- * 清空队列并删除队列日志记录
- */
- public function stopQueueProcesses()
- {
- $redis = new \Redis();
- $redis->connect('127.0.0.1', 6379);
- $redis->auth('123456');
- $redis->select(15);
- $key_txttoimg = 'queues:txttoimg';
- $key_txttotxt = 'queues:txttotxt';
- $key_imgtotxt = 'queues:imgtotxt';
- $key_imgtoimg = 'queues:imgtoimg';
- $count = $redis->lLen($key_txttoimg) + $redis->lLen($key_txttotxt) + $redis->lLen($key_imgtotxt) + $redis->lLen($key_imgtoimg);
- if ($count === 0) {
- return json([
- 'code' => 1,
- 'msg' => '暂无队列需要停止'
- ]);
- }
- // 清空 Redis 队列
- $redis->del($key_txttoimg);
- $redis->del($key_txttotxt);
- $redis->del($key_imgtotxt);
- $redis->del($key_imgtoimg);
- $counts = Db::name('image_task_log')
- ->where('log', '队列中')
- ->whereOr('status', 1)
- ->where('create_time', '>=', date('Y-m-d 00:00:00'))
- ->update([
- 'status' => "-1",
- 'log' => '清空取消队列',
- 'mod_rq' => date('Y-m-d H:i:s')
- ]);
- return json([
- 'code' => 0,
- 'msg' => '成功停止队列任务'
- ]);
- }
- /**
- * 开启队列任务
- * 暂时用不到、服务器已开启自动开启队列模式
- */
- // public function kaiStats()
- // {
- // // 判断是否已有监听进程在运行
- // $check = shell_exec("ps aux | grep 'queue:listen' | grep -v grep");
- // if ($check) {
- // return json([
- // 'code' => 1,
- // 'msg' => '监听进程已存在,请勿重复启动'
- // ]);
- // }
- // // 启动监听
- // $command = 'nohup php think queue:listen --queue --timeout=300 --sleep=3 --memory=256 > /var/log/job_queue.log 2>&1 &';
- // exec($command, $output, $status);
- // if ($status === 0) {
- // return json([
- // 'code' => 0,
- // 'msg' => '队列监听已启动'
- // ]);
- // } else {
- // return json([
- // 'code' => 1,
- // 'msg' => '队列启动失败',
- // 'output' => $output
- // ]);
- // }
- // }
- /**
- * 通过店铺ID-查询对应店铺表数据
- *
- */
- public function PatternApi()
- {
- $params = $this->request->param('pattern_id', '');
- $tableName = 'pattern-' . $params;
- // 连接 MongoDB
- $mongo = Db::connect('mongodb');
- // 查询指定 skc 的数据
- $data = $mongo->table($tableName)
- ->field('
- name,
- skc,
- file
- ')
- ->where("skc", '0853004152036')
- ->select();
- $data = json_decode(json_encode($data), true); // 数组
- return json([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ]);
- }
- }
|