Facility.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. class Facility extends Api
  7. {
  8. protected $noNeedLogin = ['*'];
  9. protected $noNeedRight = ['*'];
  10. public function getlsit()
  11. {
  12. // 获取前端传入的图片路径参数
  13. $params = $this->request->param('path', '');
  14. // 查询数据库
  15. $res = Db::name('text_to_image')->alias('b')
  16. ->field('b.chinese_description,b.english_description,b.new_image_url,b.custom_image_url,b.size,b.old_image_url')
  17. ->where('old_image_url', $params)
  18. ->where('custom_image_url', '<>', '')
  19. ->where('status', 1)
  20. ->order('b.id desc')
  21. ->select();
  22. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  23. }
  24. //获取指定目录所有图片
  25. public function getPreviewimg(){
  26. $relativePath = $this->request->param('path', '');
  27. $basePath = ROOT_PATH . 'public/';
  28. $fullPath = $basePath . $relativePath;
  29. if (!is_dir($fullPath)) {
  30. return json(['code' => 1, 'msg' => '目录不存在']);
  31. }
  32. // 1. 获取该目录下所有图片
  33. $images = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  34. // 2. 获取数据库中已处理的图片数据
  35. $processedList = Db::name('text_to_image')
  36. ->field('old_image_url,new_image_url,custom_image_url,chinese_description,english_description')
  37. ->where('custom_image_url', '<>', '')
  38. ->whereNotNull('custom_image_url')
  39. ->select();
  40. // 3. 构建以 old_image_url 为 key 的映射数组,方便快速查找
  41. $processedMap = [];
  42. foreach ($processedList as $item) {
  43. $processedMap[$item['old_image_url']] = $item;
  44. }
  45. // 4. 组装结果数据
  46. $data = [];
  47. $id = 1;
  48. foreach ($images as $imgPath) {
  49. $relative = str_replace($basePath, '', $imgPath);
  50. $info = getimagesize($imgPath);
  51. $sizeKB = round(filesize($imgPath) / 1024, 2);
  52. $ctime = date('Y-m-d H:i:s', filectime($imgPath));
  53. $processed = $processedMap[$relative] ?? '';
  54. // 查询数据库中相同 old_image_url 的条数
  55. $sameCount = Db::name('text_to_image')
  56. ->where('old_image_url', $relative)
  57. ->where('custom_image_url', '<>', '')
  58. ->count();
  59. $data[] = [
  60. 'id' => $id++,
  61. 'path' => $relative,
  62. 'width' => $info[0] ?? 0,
  63. 'height' => $info[1] ?? 0,
  64. 'size_kb' => $sizeKB,
  65. 'created_time' => $ctime,
  66. 'is_processed' => $processed ? 1 : 0,
  67. 'new_image_url' => $processed['new_image_url'] ?? '',
  68. 'custom_image_url' => $processed['custom_image_url'] ?? '',
  69. 'chinese_description' => ($processed['chinese_description'] ?? '') . ($processed['english_description'] ?? ''),
  70. 'same_count' => $sameCount // ✅ 加入统计数量
  71. ];
  72. }
  73. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  74. }
  75. /**
  76. * 获取原图目录及每个目录下的图片数量
  77. */
  78. public function getPreviewSubDirs()
  79. {
  80. // $baseDir = ROOT_PATH . 'public/uploads/operate/ai/Preview/';
  81. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview/';
  82. $baseRelativePath = 'uploads/operate/ai/Preview/';
  83. if (!is_dir($baseDir)) {
  84. return json(['code' => 1, 'msg' => '目录不存在']);
  85. }
  86. $items = scandir($baseDir);
  87. $dirs = [];
  88. $index = 1;
  89. foreach ($items as $item) {
  90. if ($item === '.' || $item === '..') continue;
  91. $fullPath = $baseDir . $item;
  92. if (!is_dir($fullPath)) continue;
  93. $relativeDir = $baseRelativePath . $item;
  94. // 查询该目录在数据库中是否有有效数据
  95. $hasData = Db::name('text_to_image')
  96. ->where('custom_image_url', '<>', '')
  97. ->whereLike('old_image_url', $relativeDir . '/%')
  98. ->whereNotNull('custom_image_url')
  99. ->count();
  100. // if ($hasData === 0) {
  101. // continue; // 如果没有有效数据,跳过此目录
  102. // }
  103. // 统计图片数量
  104. $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  105. $imageCount = is_array($imageFiles) ? count($imageFiles) : 0;
  106. // 创建时间
  107. $ctime = filectime($fullPath);
  108. $formattedDate = date('Y-m-d', $ctime);
  109. $dirs[] = [
  110. 'id' => $index++,
  111. 'name' => $item,
  112. 'count' => $hasData,
  113. 'ctime' => $formattedDate,
  114. 'image_count' => $imageCount,
  115. 'new_image_url' => "/uploads/operate/ai/dall-e/",
  116. 'old_image_url' => $relativeDir
  117. ];
  118. }
  119. // 排序:按时间倒序
  120. usort($dirs, function ($a, $b) {
  121. return strtotime($b['ctime']) - strtotime($a['ctime']);
  122. });
  123. return json(['code' => 0, 'msg' => '获取成功', 'data' => $dirs]);
  124. }
  125. /**
  126. * 图片上传
  127. * @return void
  128. */
  129. public function ImgUpload()
  130. {
  131. // 处理 CORS OPTIONS 预检请求
  132. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  133. header('Access-Control-Allow-Origin: *');
  134. header('Access-Control-Allow-Methods: POST, OPTIONS');
  135. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  136. header('Access-Control-Max-Age: 86400');
  137. exit(204);
  138. }
  139. // 实际请求必须返回 CORS 头
  140. header('Access-Control-Allow-Origin: *');
  141. $file = request()->file('image');
  142. if ($file) {
  143. $info = $file->validate([
  144. 'size' => 10 * 1024 * 1024, // 10MB
  145. 'ext' => 'jpg,png,jpeg'
  146. ])->move(ROOT_PATH . 'public' . DS . 'uploads/operate/ai/Preview/');
  147. if ($info) {
  148. $saveName = $info->getSaveName();
  149. $savePath = str_replace('\\', '/', $saveName);
  150. $fullPath = ROOT_PATH . 'public/uploads/operate/ai/Preview/' . $savePath;
  151. $relativePath = 'uploads/operate/ai/Preview/' . $savePath;
  152. // 获取图片信息
  153. $imgInfo = getimagesize($fullPath);
  154. $width = $imgInfo[0] ?? 0;
  155. $height = $imgInfo[1] ?? 0;
  156. $size_kb = round(filesize($fullPath) / 1024, 2);
  157. $created_time = date('Y-m-d H:i:s');
  158. // 写入数据库 original_image 表
  159. $insertData = [
  160. 'path' => $relativePath,
  161. 'width' => $width,
  162. 'height' => $height,
  163. 'size_kb' => $size_kb,
  164. 'created_time' => $created_time
  165. ];
  166. $result = Db::name('original_image')->insert($insertData);
  167. return json([
  168. 'code' => 0,
  169. 'msg' => '上传成功',
  170. 'data' => [
  171. 'url' => $relativePath,
  172. 'db_inserted' => $result ? true : false
  173. ]
  174. ]);
  175. } else {
  176. return json([
  177. 'code' => 1,
  178. 'msg' => '上传失败',
  179. 'data' => $file->getError()
  180. ]);
  181. }
  182. }
  183. return json([
  184. 'code' => 1,
  185. 'msg' => '没有接收到上传文件',
  186. 'data' => null
  187. ]);
  188. }
  189. }