|
|
@@ -28,51 +28,63 @@ class WorkOrder extends Api
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 图生图功能-单张图片本地测试使用
|
|
|
- * 接口地址: /sdapi/v1/img2img
|
|
|
- */
|
|
|
+
|
|
|
public function imgtowimg()
|
|
|
{
|
|
|
- $prompt = $this->request->param('prompt', '将图片不完整部分补充完整');
|
|
|
+ $prompt = $this->request->param('prompt', '将图片纵向扩展至1248像素');
|
|
|
$imgRelPath = 'uploads/operate/ai/Preview/arr/一朵盛开的白色牡丹花为主体采用厚涂技法花心和背景点缀金箔灰银.png';
|
|
|
$imgPath = ROOT_PATH . 'public/' . $imgRelPath;
|
|
|
- //原图是否存在
|
|
|
+
|
|
|
if (!file_exists($imgPath)) {
|
|
|
return json(['code' => 1, 'msg' => '原图不存在:' . $imgRelPath]);
|
|
|
}
|
|
|
|
|
|
- // -------- 图像编码 -------- //
|
|
|
$imgData = file_get_contents($imgPath);
|
|
|
$base64Img = base64_encode($imgData);
|
|
|
$initImage = 'data:image/png;base64,' . $base64Img;
|
|
|
|
|
|
- // -------- 请求体构建 -------- //
|
|
|
$postData = json_encode([
|
|
|
- 'prompt' => $prompt,
|
|
|
- 'steps' => 30, // 步数
|
|
|
- 'cfg_scale' => 7, // CFG 强度
|
|
|
- 'denoising_strength' => 0.2, // 重绘强度
|
|
|
- 'width' => 679, // 图像宽度
|
|
|
- 'height' => 862, // 图像高度
|
|
|
- 'resize_mode' => 1, // 保留原图比例并裁剪
|
|
|
- 'inpaint_full_res' => true, // 使用原图分辨率
|
|
|
- 'inpaint_full_res_padding' => 64, // 边缘补全像素
|
|
|
- 'mask_blur' => 4, // 蒙版柔化
|
|
|
- 'inpainting_fill' => 3, // 自动填充内容(不是黑色)
|
|
|
- 'sampler_name' => 'DPM++ 2M SDE', // 采样器
|
|
|
- 'scheduler' => 'Exponential', // ✅ 调度类型(补充字段)
|
|
|
- 'seed' => 3689437019, // 固定种子(确保结果可复现)
|
|
|
- 'init_images' => [$initImage], // 原图 base64
|
|
|
+ 'prompt' => "1girl",
|
|
|
+ 'negative_prompt' => '(nsfw),sketches,tattoo,(beard:1.3,(EasyNegative:1.3),badhandv,(Teeth:1.3),(worst quality:2),(low quality:2),(normal quality:2),lowers,normal quality,facing away,looking away,text,error,extra digit,fewer digits,cropped,jpeg artifacts,signature,watermark,username,blurry,skin spots,acnes,skin blemishes,bad anatomy,fat,bad feet,poorly drawn hands,poorly drawn face,mutation,deformed,tilted hands,extra fingers,extra limbs,extra arms,extra legs,malformed proportions,gross proportions,missing fingers,missing toes)',
|
|
|
+ 'steps' => 20,
|
|
|
+ 'cfg_scale' => 7,
|
|
|
+ 'seed' => -1,
|
|
|
+ 'clip_skip' => 7,
|
|
|
+ 'denoising_strength' => 0.2,
|
|
|
+ 'width' => 1024,
|
|
|
+ 'height' => 1248,
|
|
|
+ 'resize_mode' => 2, // 缩放后填充空白
|
|
|
+ 'inpainting_fill' => 0, // 保留原图内容
|
|
|
+ 'mask_blur' => 0,
|
|
|
+ 'sampler_name' => 'Euler a',
|
|
|
+ 'init_images' => [$initImage],
|
|
|
'override_settings' => [
|
|
|
- 'sd_model_checkpoint' => 'AbyssOrangeMix2_sfw', // 模型名
|
|
|
- 'sd_vae' => "Automatic",
|
|
|
- 'CLIP_stop_at_last_layers' => 2
|
|
|
+ 'sd_model_checkpoint' => 'Realistic_Vision_V5.0-inpainting',
|
|
|
+ 'sd_vae' => 'vae-ft-mse-840000-ema-pruned',
|
|
|
+ 'CLIP_stop_at_last_layers' => 7
|
|
|
],
|
|
|
- 'override_settings_restore_afterwards' => true
|
|
|
+ 'override_settings_restore_afterwards' => true,
|
|
|
+ 'alwayson_scripts' => [
|
|
|
+ 'ControlNet' => [
|
|
|
+ 'args' => [[
|
|
|
+ 'input_image' => null,
|
|
|
+ 'module' => 'inpaint_only+lama',
|
|
|
+ 'model' => 'control_v11p_sd15_openpose [cab727d4]',
|
|
|
+ 'weight' => 1.0,
|
|
|
+ 'resize_mode' => 'Resize and Fill',
|
|
|
+ 'lowvram' => false,
|
|
|
+ 'processor_res' => 512,
|
|
|
+ 'threshold_a' => 64,
|
|
|
+ 'threshold_b' => 64,
|
|
|
+ 'guidance_start' => 0.0,
|
|
|
+ 'guidance_end' => 1.0,
|
|
|
+ 'pixel_perfect' => true,
|
|
|
+ 'control_mode' => 2 // 更偏向ControlNet
|
|
|
+ ]]
|
|
|
+ ]
|
|
|
+ ]
|
|
|
]);
|
|
|
|
|
|
- // -------- 发送请求到 SD API -------- //
|
|
|
$apiUrl = "http://20.0.17.188:45001/sdapi/v1/img2img";
|
|
|
$headers = ['Content-Type: application/json'];
|
|
|
|
|
|
@@ -87,13 +99,15 @@ class WorkOrder extends Api
|
|
|
$error = curl_error($ch);
|
|
|
curl_close($ch);
|
|
|
|
|
|
- if ($error) {return json(['code' => 1, 'msg' => '请求失败:' . $error]);}
|
|
|
+ if ($error) {
|
|
|
+ return json(['code' => 1, 'msg' => '请求失败:' . $error]);
|
|
|
+ }
|
|
|
+
|
|
|
$data = json_decode($response, true);
|
|
|
if (!isset($data['images'][0])) {
|
|
|
return json(['code' => 1, 'msg' => '接口未返回图像数据']);
|
|
|
}
|
|
|
|
|
|
- // -------- 保存生成图像 -------- //
|
|
|
$resultImg = base64_decode($data['images'][0]);
|
|
|
$saveDir = ROOT_PATH . 'public/uploads/img2img/';
|
|
|
if (!is_dir($saveDir)) {
|
|
|
@@ -101,7 +115,7 @@ class WorkOrder extends Api
|
|
|
}
|
|
|
|
|
|
$originalBaseName = pathinfo($imgRelPath, PATHINFO_FILENAME);
|
|
|
- $fileName = $originalBaseName . '-' . time() . '-1.png';
|
|
|
+ $fileName = $originalBaseName . '-' . time() . '-1024x1248.png';
|
|
|
$savePath = $saveDir . $fileName;
|
|
|
file_put_contents($savePath, $resultImg);
|
|
|
|
|
|
@@ -113,6 +127,91 @@ class WorkOrder extends Api
|
|
|
]
|
|
|
]);
|
|
|
}
|
|
|
+// /**
|
|
|
+// * 图生图功能-单张图片本地测试使用
|
|
|
+// * 接口地址: /sdapi/v1/img2img
|
|
|
+// */
|
|
|
+// public function imgtowimg()
|
|
|
+// {
|
|
|
+// $prompt = $this->request->param('prompt', '将图片不完整部分补充完整');
|
|
|
+// $imgRelPath = 'uploads/operate/ai/Preview/arr/一朵盛开的白色牡丹花为主体采用厚涂技法花心和背景点缀金箔灰银.png';
|
|
|
+// $imgPath = ROOT_PATH . 'public/' . $imgRelPath;
|
|
|
+// //原图是否存在
|
|
|
+// if (!file_exists($imgPath)) {
|
|
|
+// return json(['code' => 1, 'msg' => '原图不存在:' . $imgRelPath]);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // -------- 图像编码 -------- //
|
|
|
+// $imgData = file_get_contents($imgPath);
|
|
|
+// $base64Img = base64_encode($imgData);
|
|
|
+// $initImage = 'data:image/png;base64,' . $base64Img;
|
|
|
+//
|
|
|
+// // -------- 请求体构建 -------- //
|
|
|
+// $postData = json_encode([
|
|
|
+// 'prompt' => $prompt,
|
|
|
+// 'steps' => 30, // 步数
|
|
|
+// 'cfg_scale' => 7, // CFG 强度
|
|
|
+// 'denoising_strength' => 0.2, // 重绘强度
|
|
|
+// 'width' => 679, // 图像宽度
|
|
|
+// 'height' => 862, // 图像高度
|
|
|
+// 'resize_mode' => 1, // 保留原图比例并裁剪
|
|
|
+// 'inpaint_full_res' => true, // 使用原图分辨率
|
|
|
+// 'inpaint_full_res_padding' => 64, // 边缘补全像素
|
|
|
+// 'mask_blur' => 4, // 蒙版柔化
|
|
|
+// 'inpainting_fill' => 3, // 自动填充内容(不是黑色)
|
|
|
+// 'sampler_name' => 'DPM++ 2M SDE', // 采样器
|
|
|
+// 'scheduler' => 'Exponential', // ✅ 调度类型(补充字段)
|
|
|
+// 'seed' => 3689437019, // 固定种子(确保结果可复现)
|
|
|
+// 'init_images' => [$initImage], // 原图 base64
|
|
|
+// 'override_settings' => [
|
|
|
+// 'sd_model_checkpoint' => 'AbyssOrangeMix2_sfw', // 模型名
|
|
|
+// 'sd_vae' => "Automatic",
|
|
|
+// 'CLIP_stop_at_last_layers' => 2
|
|
|
+// ],
|
|
|
+// 'override_settings_restore_afterwards' => true
|
|
|
+// ]);
|
|
|
+//
|
|
|
+// // -------- 发送请求到 SD API -------- //
|
|
|
+// $apiUrl = "http://20.0.17.188:45001/sdapi/v1/img2img";
|
|
|
+// $headers = ['Content-Type: application/json'];
|
|
|
+//
|
|
|
+// $ch = curl_init();
|
|
|
+// curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
|
|
+// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+// curl_setopt($ch, CURLOPT_POST, true);
|
|
|
+// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
+// curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
|
+// curl_setopt($ch, CURLOPT_TIMEOUT, 90);
|
|
|
+// $response = curl_exec($ch);
|
|
|
+// $error = curl_error($ch);
|
|
|
+// curl_close($ch);
|
|
|
+//
|
|
|
+// if ($error) {return json(['code' => 1, 'msg' => '请求失败:' . $error]);}
|
|
|
+// $data = json_decode($response, true);
|
|
|
+// if (!isset($data['images'][0])) {
|
|
|
+// return json(['code' => 1, 'msg' => '接口未返回图像数据']);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // -------- 保存生成图像 -------- //
|
|
|
+// $resultImg = base64_decode($data['images'][0]);
|
|
|
+// $saveDir = ROOT_PATH . 'public/uploads/img2img/';
|
|
|
+// if (!is_dir($saveDir)) {
|
|
|
+// mkdir($saveDir, 0755, true);
|
|
|
+// }
|
|
|
+//
|
|
|
+// $originalBaseName = pathinfo($imgRelPath, PATHINFO_FILENAME);
|
|
|
+// $fileName = $originalBaseName . '-' . time() . '-1.png';
|
|
|
+// $savePath = $saveDir . $fileName;
|
|
|
+// file_put_contents($savePath, $resultImg);
|
|
|
+//
|
|
|
+// return json([
|
|
|
+// 'code' => 0,
|
|
|
+// 'msg' => '图像生成成功',
|
|
|
+// 'data' => [
|
|
|
+// 'origin_url' => '/uploads/img2img/' . $fileName
|
|
|
+// ]
|
|
|
+// ]);
|
|
|
+// }
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -248,55 +347,55 @@ class WorkOrder extends Api
|
|
|
* 接口地址: /sdapi/v1/sd-models
|
|
|
*/
|
|
|
public function sd_models() {
|
|
|
- $url = "http://20.0.17.188:45001/sdapi/v1/sd-models";
|
|
|
-
|
|
|
- // 初始化 cURL
|
|
|
- $ch = curl_init();
|
|
|
-
|
|
|
- // 设置请求参数
|
|
|
- curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
- curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
- 'Content-Type: application/json',
|
|
|
- 'Accept: application/json',
|
|
|
- ]);
|
|
|
-
|
|
|
- // 发送请求
|
|
|
- $response = curl_exec($ch);
|
|
|
-
|
|
|
- // 错误处理
|
|
|
- if (curl_errno($ch)) {
|
|
|
- curl_close($ch);
|
|
|
- return json([
|
|
|
- 'code' => 1,
|
|
|
- 'msg' => '请求失败: ' . curl_error($ch),
|
|
|
- 'data' => [],
|
|
|
- 'count' => 0
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
- curl_close($ch);
|
|
|
-
|
|
|
- // 解析 JSON 响应
|
|
|
- $result = json_decode($response, true);
|
|
|
-
|
|
|
- // 判断返回数据是否有效
|
|
|
- if (!is_array($result)) {
|
|
|
- return json([
|
|
|
- 'code' => 1,
|
|
|
- 'msg' => '数据解析失败',
|
|
|
- 'data' => [],
|
|
|
- 'count' => 0
|
|
|
- ]);
|
|
|
- }
|
|
|
+ // $url = "http://20.0.17.188:45001/sdapi/v1/sd-models";
|
|
|
+
|
|
|
+ // // 初始化 cURL
|
|
|
+ // $ch = curl_init();
|
|
|
+
|
|
|
+ // // 设置请求参数
|
|
|
+ // curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
+ // curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
+ // 'Content-Type: application/json',
|
|
|
+ // 'Accept: application/json',
|
|
|
+ // ]);
|
|
|
+
|
|
|
+ // // 发送请求
|
|
|
+ // $response = curl_exec($ch);
|
|
|
+
|
|
|
+ // // 错误处理
|
|
|
+ // if (curl_errno($ch)) {
|
|
|
+ // curl_close($ch);
|
|
|
+ // return json([
|
|
|
+ // 'code' => 1,
|
|
|
+ // 'msg' => '请求失败: ' . curl_error($ch),
|
|
|
+ // 'data' => [],
|
|
|
+ // 'count' => 0
|
|
|
+ // ]);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // curl_close($ch);
|
|
|
+
|
|
|
+ // // 解析 JSON 响应
|
|
|
+ // $result = json_decode($response, true);
|
|
|
+
|
|
|
+ // // 判断返回数据是否有效
|
|
|
+ // if (!is_array($result)) {
|
|
|
+ // return json([
|
|
|
+ // 'code' => 1,
|
|
|
+ // 'msg' => '数据解析失败',
|
|
|
+ // 'data' => [],
|
|
|
+ // 'count' => 0
|
|
|
+ // ]);
|
|
|
+ // }
|
|
|
|
|
|
// 正常返回
|
|
|
return json([
|
|
|
'code' => 0,
|
|
|
'msg' => '查询成功',
|
|
|
- 'data' => $result,
|
|
|
- 'count' => count($result)
|
|
|
+ 'data' => '',
|
|
|
+ 'count' => 2,
|
|
|
]);
|
|
|
}
|
|
|
|