Skip to content

Commit 9758872

Browse files
committed
Updated image fetching in exporting
Added domain check to see if possibly local even when whole url found. Changed image fetch from file_get_contents to curl for external resources. Hopeful solution to #392
1 parent b711bc6 commit 9758872

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

app/Services/ExportService.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected function htmlToPdf($html)
136136
* Bundle of the contents of a html file to be self-contained.
137137
* @param $htmlContent
138138
* @return mixed|string
139+
* @throws \Exception
139140
*/
140141
protected function containHtml($htmlContent)
141142
{
@@ -153,9 +154,27 @@ protected function containHtml($htmlContent)
153154
} else {
154155
$pathString = $srcString;
155156
}
157+
158+
// Attempt to find local files even if url not absolute
159+
$base = baseUrl('/');
160+
if (strpos($srcString, $base) === 0) {
161+
$isLocal = true;
162+
$relString = str_replace($base, '', $srcString);
163+
$pathString = public_path(trim($relString, '/'));
164+
}
165+
156166
if ($isLocal && !file_exists($pathString)) continue;
157167
try {
158-
$imageContent = file_get_contents($pathString);
168+
if ($isLocal) {
169+
$imageContent = file_get_contents($pathString);
170+
} else {
171+
$ch = curl_init();
172+
curl_setopt_array($ch, [CURLOPT_URL => $pathString, CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 5]);
173+
$imageContent = curl_exec($ch);
174+
$err = curl_error($ch);
175+
curl_close($ch);
176+
if ($err) throw new \Exception("Image fetch failed, Received error: " . $err);
177+
}
159178
$imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
160179
$newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
161180
} catch (\ErrorException $e) {

0 commit comments

Comments
 (0)