ImageGenerationController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers\AIGeneration;
  3. use Illuminate\Routing\Controller as BaseController;
  4. use App\Libs\Utils;
  5. use App\Services\AIGeneration\AIImageGenerationService;
  6. use App\Transformer\AIGeneration\AIGenerationTransformer;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Support\Facades\Validator;
  10. use App\Libs\ApiResponse;
  11. class ImageGenerationController extends BaseController
  12. {
  13. use ApiResponse;
  14. private $aiImageGenerationService;
  15. public function __construct(
  16. AIImageGenerationService $aiImageGenerationService
  17. ) {
  18. $this->aiImageGenerationService = $aiImageGenerationService;
  19. }
  20. /**
  21. * 创建图片生成任务
  22. *
  23. * @param Request $request
  24. * @return JsonResponse
  25. */
  26. public function createTask(Request $request): JsonResponse
  27. {
  28. $data = $request->all();
  29. $validator = Validator::make($data, [
  30. 'prompt' => 'required|string|max:2000',
  31. 'width' => 'required|numeric|between:1024,4096',
  32. 'height' => 'required|numeric|between:1024,4096',
  33. 'image_num' => 'required|numeric|between:1,4',
  34. 'scale' => 'required|numeric|between:0,100',
  35. ], [
  36. 'prompt.required' => '提示词不能为空',
  37. 'prompt.max' => '提示词不能超过2000个字符',
  38. 'width.required' => '宽不能为空',
  39. 'width.numeric' => '宽必须是数字',
  40. 'width.between' => '宽必须在1024到4096之间',
  41. 'height.required' => '高不能为空',
  42. 'height.numeric' => '高必须是数字',
  43. 'height.between' => '高必须在1024到4096之间',
  44. 'image_num.required' => '生成图片数量不能为空',
  45. 'image_num.numeric' => '生成图片数量必须是数字',
  46. 'image_num.between' => '生成图片数量必须在1到4之间',
  47. 'scale.required' => '缩放比例不能为空',
  48. 'scale.numeric' => '缩放比例必须是数字',
  49. 'scale.between' => '缩放比例必须在0到100之间',
  50. ]);
  51. if ($validator->fails()) {
  52. Utils::throwError('1002:'.$validator->errors()->first());
  53. }
  54. try {
  55. $task = $this->aiImageGenerationService->createImageGenerationTask($data);
  56. return $this->success(['task_id' => $task->task_id, 'status' => $task->status]);
  57. } catch (\Exception $e) {
  58. Utils::throwError('1001:'.$e->getMessage());
  59. }
  60. }
  61. /**
  62. * 查询任务状态
  63. *
  64. * @param string $taskId
  65. * @return JsonResponse
  66. */
  67. public function taskStatus(string $taskId): JsonResponse
  68. {
  69. $task = \App\Models\MpGeneratePicTask::where('task_id', $taskId)->first();
  70. if (!$task) {
  71. return response()->json([
  72. 'success' => false,
  73. 'message' => '任务不存在'
  74. ], 404);
  75. }
  76. return response()->json([
  77. 'success' => true,
  78. 'data' => [
  79. 'task_id' => $task->task_id,
  80. 'status' => $task->status,
  81. 'result_url' => $task->result_url,
  82. 'error_message' => $task->error_message,
  83. 'created_at' => $task->created_at,
  84. 'started_at' => $task->started_at,
  85. 'completed_at' => $task->completed_at
  86. ]
  87. ]);
  88. }
  89. /**
  90. * 获取任务列表
  91. *
  92. * @param Request $request
  93. * @return mixed
  94. */
  95. public function taskList(Request $request)
  96. {
  97. $data = $request->all();
  98. $result = $this->aiImageGenerationService->getTaskList($data);
  99. return $this->success($result, [new AIGenerationTransformer(), 'newBuildTaskList']);
  100. }
  101. }