1111namespace Imactool \HyperfStableDiffusion ;
1212
1313 use Exception ;
14+ use Hyperf \Codec \Json ;
1415 use Hyperf \Collection \Arr ;
1516 use Hyperf \Context \ApplicationContext ;
1617 use Hyperf \Guzzle \ClientFactory ;
@@ -27,25 +28,36 @@ class StableDiffusion
2728
2829 private static $ platform = 'stablediffusionapi ' ;
2930
30- private function __construct ()
31+ private function __construct (protected $ apiVersion )
3132 {
33+ $ class = '\\Imactool \\HyperfStableDiffusion \\Uri \\' . $ apiVersion ;
34+ if (! class_exists ($ class )) {
35+ throw new Exception ("Class {$ class } not found " );
36+ }
37+ $ this ->apiBase = ApplicationContext::getContainer ()->get ($ class );
3238 }
3339
34- public static function make (): self
40+ // 获取( https://stablediffusionapi.com)支持的已被本SDK适配的API集合
41+ public function supportApi (string $ api = '' ): array |string
3542 {
36- return new self ();
37- }
43+ $ aiApis = [
44+ 'StableDiffusionApiV3 ' , // Stable Diffusion V3 APIs comes with below features https://documenter.getpostman.com/view/18679074/2s83zdwReZ#c7e3c6a0-b57d-4d17-ad5a-c4eb8571021f
45+ 'DreamboothApiV4 ' , // [Beta] DreamBooth API https://documenter.getpostman.com/view/18679074/2s83zdwReZ#27db9713-6068-41c2-8431-ada0d08d3cd5
46+ ];
3847
39- public function useStableDiffusionApiV3 (): StableDiffusion
40- {
41- $ this ->apiBase = ApplicationContext::getContainer ()->get (StableDiffusionApiV3::class);
42- return $ this ;
48+ if ($ api ) {
49+ if (in_array ($ api , $ aiApis )) {
50+ return $ aiApis [$ api ];
51+ }
52+ throw new Exception ('无效 ' . $ api . '。仅支持这些API ' . Json::encode ($ aiApis ));
53+ }
54+
55+ return $ aiApis ;
4356 }
4457
45- public function useDreamboothApiV4 ( ): StableDiffusion
58+ public static function make ( string $ witchAPi = ' StableDiffusionApiV3 ' ): self
4659 {
47- $ this ->apiBase = ApplicationContext::getContainer ()->get (DreamboothApiV4::class);
48- return $ this ;
60+ return new StableDiffusion ($ witchAPi );
4961 }
5062
5163 /**
@@ -83,7 +95,6 @@ public function text2img()
8395 );
8496
8597 $ result = json_decode ($ response ->getBody ()->getContents (), true );
86- var_dump (['$result 请求结果 ' => $ result ]);
8798 $ this ->saveResult ($ result , $ this ->apiBase ->text2imgUrl ());
8899 return $ result ;
89100 }
@@ -102,7 +113,7 @@ public function img2img()
102113 );
103114
104115 $ result = json_decode ($ response ->getBody ()->getContents (), true );
105- var_dump ([ ' $result 请求结果 ' => $ result ]);
116+
106117 $ this ->saveResult ($ result , $ this ->apiBase ->img2imgUrl ());
107118 return $ result ;
108119 }
0 commit comments