| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Request;
- class Facility extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function getlsit()
- {
- // 获取前端传入的图片路径参数
- $params = $this->request->param('path', '');
- // 查询数据库
- $res = Db::name('text_to_image')->alias('b')
- ->field('b.chinese_description,b.english_description,b.new_image_url,b.custom_image_url,b.size,b.old_image_url')
- ->where('old_image_url', $params)
- ->where('custom_image_url', '<>', '')
- ->where('status', 1)
- ->order('b.id desc')
- ->select();
- return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
- }
- //获取指定目录所有图片
- public function getPreviewimg(){
- $relativePath = $this->request->param('path', '');
- $basePath = ROOT_PATH . 'public/';
- $fullPath = $basePath . $relativePath;
- if (!is_dir($fullPath)) {
- return json(['code' => 1, 'msg' => '目录不存在']);
- }
- // 1. 获取该目录下所有图片
- $images = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
- // 2. 获取数据库中已处理的图片数据
- $processedList = Db::name('text_to_image')
- ->field('old_image_url,new_image_url,custom_image_url,chinese_description,english_description')
- ->where('custom_image_url', '<>', '')
- ->whereNotNull('custom_image_url')
- ->select();
- // 3. 构建以 old_image_url 为 key 的映射数组,方便快速查找
- $processedMap = [];
- foreach ($processedList as $item) {
- $processedMap[$item['old_image_url']] = $item;
- }
- // 4. 组装结果数据
- $data = [];
- $id = 1;
- foreach ($images as $imgPath) {
- $relative = str_replace($basePath, '', $imgPath);
- $info = getimagesize($imgPath);
- $sizeKB = round(filesize($imgPath) / 1024, 2);
- $ctime = date('Y-m-d H:i:s', filectime($imgPath));
- $processed = $processedMap[$relative] ?? '';
- // 查询数据库中相同 old_image_url 的条数
- $sameCount = Db::name('text_to_image')
- ->where('old_image_url', $relative)
- ->where('custom_image_url', '<>', '')
- ->count();
- $data[] = [
- 'id' => $id++,
- 'path' => $relative,
- 'width' => $info[0] ?? 0,
- 'height' => $info[1] ?? 0,
- 'size_kb' => $sizeKB,
- 'created_time' => $ctime,
- 'is_processed' => $processed ? 1 : 0,
- 'new_image_url' => $processed['new_image_url'] ?? '',
- 'custom_image_url' => $processed['custom_image_url'] ?? '',
- 'chinese_description' => ($processed['chinese_description'] ?? '') . ($processed['english_description'] ?? ''),
- 'same_count' => $sameCount // ✅ 加入统计数量
- ];
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
- }
- /**
- * 获取原图目录及每个目录下的图片数量
- */
- public function getPreviewSubDirs()
- {
- // $baseDir = ROOT_PATH . 'public/uploads/operate/ai/Preview/';
- $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview/';
- $baseRelativePath = 'uploads/operate/ai/Preview/';
- if (!is_dir($baseDir)) {
- return json(['code' => 1, 'msg' => '目录不存在']);
- }
- $items = scandir($baseDir);
- $dirs = [];
- $index = 1;
- foreach ($items as $item) {
- if ($item === '.' || $item === '..') continue;
- $fullPath = $baseDir . $item;
- if (!is_dir($fullPath)) continue;
- $relativeDir = $baseRelativePath . $item;
- // 查询该目录在数据库中是否有有效数据
- $hasData = Db::name('text_to_image')
- ->where('custom_image_url', '<>', '')
- ->whereLike('old_image_url', $relativeDir . '/%')
- ->whereNotNull('custom_image_url')
- ->count();
- // if ($hasData === 0) {
- // continue; // 如果没有有效数据,跳过此目录
- // }
- // 统计图片数量
- $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
- $imageCount = is_array($imageFiles) ? count($imageFiles) : 0;
- // 创建时间
- $ctime = filectime($fullPath);
- $formattedDate = date('Y-m-d', $ctime);
- $dirs[] = [
- 'id' => $index++,
- 'name' => $item,
- 'count' => $hasData,
- 'ctime' => $formattedDate,
- 'image_count' => $imageCount,
- 'new_image_url' => "/uploads/operate/ai/dall-e/",
- 'old_image_url' => $relativeDir
- ];
- }
- // 排序:按时间倒序
- usort($dirs, function ($a, $b) {
- return strtotime($b['ctime']) - strtotime($a['ctime']);
- });
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $dirs]);
- }
- /**
- * 图片上传
- * @return void
- */
- public function ImgUpload()
- {
- // 处理 CORS OPTIONS 预检请求
- if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: POST, OPTIONS');
- header('Access-Control-Allow-Headers: Content-Type, Authorization');
- header('Access-Control-Max-Age: 86400');
- exit(204);
- }
- // 实际请求必须返回 CORS 头
- header('Access-Control-Allow-Origin: *');
- $file = request()->file('image');
- if ($file) {
- $info = $file->validate([
- 'size' => 10 * 1024 * 1024, // 10MB
- 'ext' => 'jpg,png,jpeg'
- ])->move(ROOT_PATH . 'public' . DS . 'uploads/operate/ai/Preview/');
- if ($info) {
- $saveName = $info->getSaveName();
- $savePath = str_replace('\\', '/', $saveName);
- $fullPath = ROOT_PATH . 'public/uploads/operate/ai/Preview/' . $savePath;
- $relativePath = 'uploads/operate/ai/Preview/' . $savePath;
- // 获取图片信息
- $imgInfo = getimagesize($fullPath);
- $width = $imgInfo[0] ?? 0;
- $height = $imgInfo[1] ?? 0;
- $size_kb = round(filesize($fullPath) / 1024, 2);
- $created_time = date('Y-m-d H:i:s');
- // 写入数据库 original_image 表
- $insertData = [
- 'path' => $relativePath,
- 'width' => $width,
- 'height' => $height,
- 'size_kb' => $size_kb,
- 'created_time' => $created_time
- ];
- $result = Db::name('original_image')->insert($insertData);
- return json([
- 'code' => 0,
- 'msg' => '上传成功',
- 'data' => [
- 'url' => $relativePath,
- 'db_inserted' => $result ? true : false
- ]
- ]);
- } else {
- return json([
- 'code' => 1,
- 'msg' => '上传失败',
- 'data' => $file->getError()
- ]);
- }
- }
- return json([
- 'code' => 1,
- 'msg' => '没有接收到上传文件',
- 'data' => null
- ]);
- }
- }
|