Laravel DOMPDF:如何从存储文件夹加载图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37079529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:44:25  来源:igfitidea点击:

Laravel DOMPDF: how to load image from storage folder?

phpimagelaraveldompdf

提问by greener

An image stored in the /storage/app/folder of Laravel 5.2isn't being rendered into DOMPDF. I receive an error saying: image not found or type unknown.

存储在Laravel 5.2/storage/app/文件夹中的图像没有被渲染到DOMPDF 中。我收到一条错误消息:。image not found or type unknown

The image's path is storage/app/v6_dashboard.pngand I tried these:

图像的路径是storage/app/v6_dashboard.png,我尝试了这些:

<img src="/media/v6_dashboard.png" />
<img src="media/v6_dashboard.png" />
<img src="http://localhost:8000/media/v6_dashboard.png" />

which use this route:

使用这条路线:

Route::get('/media/{img}', function ($img) {
  $image = Image::make(Storage::disk('local')->get($img));
  return Response::make($image->encode('jpg'), 200, ['Content-Type' => 'image/jpeg']);
});

Note that I have this config set: "DOMPDF_ENABLE_REMOTE" => true

请注意,我有这个配置集: "DOMPDF_ENABLE_REMOTE" => true

It works in a typical HTML view but not in DOMPDF. Is there a fundamental incompatibility or am I doing something wrong?

它适用于典型的 HTML 视图,但不适用于 DOMPDF。是否存在根本不兼容或我做错了什么?

采纳答案by greener

In the end, I bypassed the route and used a str_replaceto switch /media/to the absolute path like so:

最后,我绕过了路由并使用 astr_replace切换/media/到绝对路径,如下所示:

str_replace('/media/', '../storage/app/', $link);

Slightly counter-intuitive to go from publicto storagein the HTML but it seems to be the only way for DOMPDF to render the image. Also found out that DOMPDF won't render remote images over HTTPS.

在 HTML 中从public到有点违反直觉,storage但它似乎是 DOMPDF 呈现图像的唯一方法。还发现 DOMPDF 不会通过 HTTPS 呈现远程图像。

回答by Eka putra

you need to reference the public path of your image. So if 'image.jpg' is in your public/images folder and stored in a variable called $image, you can do something like:

您需要引用图像的公共路径。因此,如果“image.jpg”在您的 public/images 文件夹中并存储在名为 $image 的变量中,您可以执行以下操作:

<img src="{{ public_path() . $image }}">

dompdf needs the absolute path on the server, not what the client would see!

dompdf 需要服务器上的绝对路径,而不是客户端会看到的!