Skip to content

Commit 8e2f0e5

Browse files
authored
Merge pull request #17 from keiserjb/1.x-1.x
link to download image
2 parents e521d74 + e51578e commit 8e2f0e5

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

modules/openai_dalle/openai_dalle.module

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function openai_dalle_form($form, &$form_state) {
142142
];
143143

144144
$form['response'] = [
145-
'#markup' => 'The response will create a link to the image below.',
145+
'#markup' => 'The response will display the generated image below and provide a link to download it.',
146146
];
147147

148148
$form['file'] = [
@@ -241,9 +241,27 @@ function openai_dalle_form_validate($form, &$form_state) {
241241
* The modified form element.
242242
*/
243243
function openai_dalle_get_response($form, &$form_state) {
244-
if (isset($form_state['storage']) && !empty($form_state['storage']['filepath'])) {
245-
$file_url = $form_state['storage']['filepath'];
246-
$form['file']['#markup'] = '<img src="' . $file_url . '" alt="' . t('Generated image') . '">';
244+
$storage = $form_state['storage'];
245+
246+
if (!empty($storage['fid'])) {
247+
// Load the file entity using the file ID.
248+
$file = file_load($storage['fid']);
249+
if ($file) {
250+
$file_url = file_create_url($file->uri);
251+
252+
// Generate the markup for displaying the image and providing the link.
253+
$form['file']['#markup'] = t('
254+
<div class="openai-dalle-result">
255+
<p><strong>Generated Image:</strong></p>
256+
<img src="@file_url" alt="Generated Image" style="max-width: 100%; border: 1px solid #ccc; padding: 10px; display: block;" />
257+
<p><a href="@file_url" target="_blank" download>@filename</a></p>
258+
</div>', [
259+
'@file_url' => $file_url,
260+
'@filename' => $file->filename,
261+
]);
262+
} else {
263+
$form['file']['#markup'] = t('The file could not be loaded.');
264+
}
247265
} else {
248266
$form['file']['#markup'] = t('No image generated yet.');
249267
}

0 commit comments

Comments
 (0)