ImageService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // 1. 正确的队列任务类 application/job/ImageJob.php
  3. namespace app\service;
  4. use think\Db;
  5. use think\Queue;
  6. class ImageService
  7. {
  8. public function handleImage($params) {
  9. // 如果是单条,转为数组
  10. if(!isset($params["batch"])){
  11. return false;
  12. }
  13. $arr = [];
  14. $batch = $params["batch"];
  15. $num = $params["num"];
  16. //进行数据处理
  17. $j = 0;
  18. foreach ($batch as $k => $v) {
  19. $baseItem = [
  20. "sourceDir" => $this->sourceDir($v, 1) ?? '',
  21. "outputDir" => $this->sourceDir($v, 2) ?? '',
  22. "file_name" => $this->sourceDir($v, 3) ?? '',
  23. "type" => $params['type'] ?? '',
  24. "prompt" => $params['prompt'] ?? '',
  25. "width" => $params['width'] ?? 512,
  26. "height" => $params['height'] ?? 512
  27. ];
  28. // 创建$num个相同的项目并合并到$arr
  29. $arr = array_merge($arr, array_fill(0, $num, $baseItem));
  30. }
  31. // 推送队列(启用时请去掉 die)
  32. //整体进行推送
  33. Queue::push('app\job\ImageArrJob', $arr,"arrimage");
  34. }
  35. public function sourceDir($filePath,$type){
  36. $arr = [];
  37. // 使用正则表达式匹配完整路径
  38. if (preg_match('/^(.+?)\/Preview\/(\d{8})\/(.+)$/', $filePath, $matches)) {
  39. $arr = [
  40. 'basePath' => $matches[1], // uploads/operate/ai
  41. 'date' => $matches[2], // 20250522
  42. 'filename' => $matches[3] // 333 (89).jpg
  43. ];
  44. }else{
  45. // 备用方案:如果正则匹配失败
  46. $pathParts = explode('/', $filePath);
  47. $filename = array_pop($pathParts);
  48. $date = '';
  49. $baseParts = [];
  50. foreach ($pathParts as $part) {
  51. if (preg_match('/^\d{8}$/', $part)) {
  52. $date = $part;
  53. break;
  54. }
  55. $baseParts[] = $part;
  56. }
  57. $arr = [
  58. 'basePath' => implode('/', $baseParts),
  59. 'date' => $date,
  60. 'filename' => $filename
  61. ];
  62. }
  63. if($type==1){
  64. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  65. return $arr["basePath"]."/Preview/".$arr["date"];
  66. }
  67. if($type==2){
  68. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  69. return '/'.$arr["basePath"]."/dall-e/".$arr["date"];
  70. }
  71. if($type==3){
  72. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  73. return $arr["filename"];
  74. }
  75. }
  76. }