-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeneratepic.php
102 lines (100 loc) · 4.13 KB
/
generatepic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
require_once('config.inc.php');
if ($_POST['action'] == "generatePic") {
error_reporting(E_ALL^E_WARNING^E_NOTICE);
//获取图片各个参数
$prompt = $_POST['message'];
$negative_prompt = $_POST['negative_message'];
$cfgscale = $_POST['cfg_scale'];
$sd_steps = $_POST['sd_steps'];
$stabilityai_engine = $_POST['stabilityai_engine'];
$sd_height = $_POST['sd_height'];
$sd_width = $_POST['sd_width'];
$sd_sampler = $_POST['sd_sampler'];
$sd_seed = $_POST['sd_seed'];
$sd_samples = $_POST['sd_samples'];
//stability api秘钥
$api_key = '';//use your own api key
$base_url = 'https://api.stability.ai';
$user_account_url = $base_url.'/v1/user/account';
$user_balance_url = $base_url.'/v1/user/balance';
$engine_list_url = $base_url.'/v1/engines/list';
//$prompt_image_url = $base_url.'/v1/generation/stable-diffusion-768-v2-1/text-to-image';
$prompt_image_url = STABILITYAI_BASE_URL.'/v1/generation/'.$stabilityai_engine.'/text-to-image';
//文字生成图片
function promptToImage($api_prompt_image_url, $stability_api_key, $message, $negative_message, $cfg_scale, $steps, $height, $width, $sampler, $seed, $samples)
{
//使用curl发送请求
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $api_prompt_image_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//设置本地代理服务器
//curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1');
//curl_setopt($ch,CURLOPT_PROXYPORT,'2712');
//停止验证证书和host
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
//"Accept: image/png",
"Authorization: Bearer " . $stability_api_key,
)
);
//设置请求的参数
if ($negative_message=="") {
$data = array("text_prompts"=>array(["text"=>$message]),
"cfg_scale"=>intval($cfg_scale),
"height"=>intval($height),
"width"=>intval($width),
"samples"=>intval($samples),
"sampler" => $sampler,
"style_preset" => $stylepreset,
"seed" => intval($seed),
"steps"=>intval($steps));
} else {
$data = array("text_prompts"=>array(["text"=>$message,"weight"=>1.0],["text"=>$negative_message,"weight"=>-1.0]),
"cfg_scale"=>intval($cfg_scale),
"height"=>intval($height),
"width"=>intval($width),
"samples"=>intval($samples),
"sampler" => $sampler,
"style_preset" => $stylepreset,
"seed" => intval($seed),
"steps"=>intval($steps));
}
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($data));
$response=curl_exec($ch);
curl_close($ch);
//处理反馈
$response_data=json_decode($response,true);
if ($response_data['name']=='unauthorized') {
echo "未授权,验证错误,请重试!";
} elseif ($response_data['name']=='server_error') {
echo "未预料到的服务器错误,请重试!";
} elseif ($response_data['name']=='permission_denied') {
echo "您不被允许访问此资源!";
} elseif ($response_data['name']=='not found') {
echo "指定的引擎未找到!";
} else {
//获取生成图片
$sd_pics = array();
foreach ($response_data['artifacts'] as $key=>$response) {
$imageName = date("YmdHis",time())."_".$response['seed'].'.png';
$path = 'pics';
$imageSrc= $path."/". $imageName;
$image = $response['base64'];
$image = base64_decode($image);
$r = file_put_contents($imageSrc, $image);
$image = imagecreatefrompng($path. "/".$imageName);
$sd_pics[] = array('image_path'=>$imageSrc, 'seed'=>$response['seed']);
}
echo json_encode($sd_pics);
}
}
promptToImage($prompt_image_url, $api_key, $prompt, $negative_prompt, $cfgscale, $sd_steps, $sd_height, $sd_width, $sd_sampler, $sd_seed, $sd_samples);
}
?>