refactor image download

This commit is contained in:
quenousimporte 2024-06-13 09:42:57 +02:00
parent 2bd760c960
commit 60ed00946c
1 changed files with 14 additions and 16 deletions

View File

@ -44,6 +44,15 @@
}
}
function lm_download_image($base_url, $url_part, $id)
{
$image_url = preg_replace('/GetPublicationContentItems-.*\.json/', $url_part . $id . '.jpg', $base_url);
$temp_img_path = './temp/' . $id . '.jpg';
$temp_content = file_get_contents($image_url);
file_put_contents($temp_img_path, $temp_content);
return $temp_img_path;
}
date_default_timezone_set('Europe/Paris');
$date = (new DateTime('today'))->format('Ymd');
@ -103,17 +112,13 @@
}))[0];
$pageid = $pageobj->PublicationPageID;
// SMALL / MEDIUM / XLARGE
$pageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Preview-MEDIUM-' . $pageid . '.jpg', $url);
$tempimgpath = './temp/' . $pageid . '.jpg';
$tempcontent = file_get_contents($pageurl);
file_put_contents($tempimgpath, $tempcontent);
$epub->AddPage('<img style="width: 100%" src="' . $tempimgpath . '">', false, 'Page ' . $page, true);
$path = lm_download_image($url, 'Preview-MEDIUM-', $pageid);
$epub->AddPage('<img style="width: 100%" src="' . $path . '">', false, 'Page ' . $page, true);
// Add page 1 as cover
if ($page == 1)
{
$epub->AddImage($tempimgpath, 'image/jpeg', true);
$epub->AddImage($path, 'image/jpeg', true);
}
}
@ -142,15 +147,8 @@
$images = array_values(array_filter($article->ContentItem, function($item) { return $item->ContentType == 'graphic/jpeg' || $item->ContentType == 'image/jpeg'; }));
foreach ($images as $image)
{
$imageid = $image->ContentItemId;
$imageurl = preg_replace('/GetPublicationContentItems-.*\.json/', 'Image-MEDIUM-' . $imageid . '.jpg', $url);
// Download image from source, put it in temp and include it into article main html content.
// Then pass true to last AddPage param to download image to epub.
$tempimgpath = './temp/' . $imageid . '.jpg';
$tempcontent = file_get_contents($imageurl);
file_put_contents($tempimgpath, $tempcontent);
$pagecontent .= '<p><img style="width: 100%" src="' . $tempimgpath . '"></p>';
$path = lm_download_image($url, 'Image-MEDIUM-', $image->ContentItemId);
$pagecontent .= '<p><img style="width: 100%" src="' . $path . '"></p>';
}
}