ImageGenerationController.php 3.7 KB

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