-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox-download.php
63 lines (55 loc) · 1.63 KB
/
checkbox-download.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
<?php
if (isset($_POST['submit'])) {
$prompt = $_POST['prompt'];
$num_images = $_POST['num-images'];
$image_size = $_POST['image-size'];
$url = 'https://api.openai.com/v1/images/generations';
$api_key = 'sk-YOUR-KEY-HERE';
$data = array(
'model' => 'image-alpha-001',
'prompt' => $prompt,
'num_images' => (int)$num_images,
'size' => $image_size
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
$images = $response['data'];
if (is_array($images)) {
echo '<form method="post">';
foreach ($images as $index => $image) {
echo '<div>
<img src="' . $image['url'] . '">
<label for="image-' . $index . '">Download Image</label>
<input type="checkbox" name="images[]" id="image-' . $index . '" value="' . $image['url'] . '">
</div>';
}
echo '<button type="submit" name="download">Download Selected Images</button>
</form>';
} else {
echo 'No images found.';
}
if (isset($_POST['download'])) {
$images = $_POST['images'];
if (is_array($images)) {
foreach ($images as $image) {
$filename = basename($image);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($image);
}
}
}
}
?>
<p><a href="demo.php">return</a></p>