ImageService.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "prompt" => $params['prompt'] ?? '',
  24. "width" => $params['width'] ?? 512,
  25. "height" => $params['height'] ?? 512
  26. ];
  27. // 创建$num个相同的项目并合并到$arr
  28. $arr = array_merge($arr, array_fill(0, $num, $baseItem));
  29. }
  30. // 推送队列(启用时请去掉 die)
  31. //整体进行推送
  32. Queue::push('app\job\ImageArrJob', $arr,"arrimage");
  33. }
  34. public function sourceDir($filePath,$type){
  35. $arr = [];
  36. // 使用正则表达式匹配完整路径
  37. if (preg_match('/^(.+?)\/Preview\/(\d{8})\/(.+)$/', $filePath, $matches)) {
  38. $arr = [
  39. 'basePath' => $matches[1], // uploads/operate/ai
  40. 'date' => $matches[2], // 20250522
  41. 'filename' => $matches[3] // 333 (89).jpg
  42. ];
  43. }else{
  44. // 备用方案:如果正则匹配失败
  45. $pathParts = explode('/', $filePath);
  46. $filename = array_pop($pathParts);
  47. $date = '';
  48. $baseParts = [];
  49. foreach ($pathParts as $part) {
  50. if (preg_match('/^\d{8}$/', $part)) {
  51. $date = $part;
  52. break;
  53. }
  54. $baseParts[] = $part;
  55. }
  56. $arr = [
  57. 'basePath' => implode('/', $baseParts),
  58. 'date' => $date,
  59. 'filename' => $filename
  60. ];
  61. }
  62. if($type==1){
  63. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  64. return $arr["basePath"]."/Preview/".$arr["date"];
  65. }
  66. if($type==2){
  67. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  68. return '/'.$arr["basePath"]."/dall-e/".$arr["date"];
  69. }
  70. if($type==3){
  71. // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
  72. return $arr["filename"];
  73. }
  74. }
  75. }