首页 > 解决方案 > 在 Laravel 中添加图像 PHPWord

问题描述

所以我想在 Laravel 的 PHPWord 中为我的文档添加一个标题图像。所以这是我的代码

public function generateDocx()
{
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();

    $headerLogo = 'http://127.0.0.1:8000/img/logoAnevBulanan.png';
    $section->addImage($headerLogo);

    // Bunch of line to download the docx
}

而且我得到了超过 60 秒的最大执行时间,当我尝试文档中的其他方法时,我仍然遇到同样的错误。我尝试使用asset() 来自 laravel 的助手,但仍然没有工作

标签: laravelphpword

解决方案


尝试使用本地路径而不是 URL 获取图像

$source = file_get_contents('/path/to/my/images/earth.jpg');
$textrun->addImage($source);

参考文档:https ://phpword.readthedocs.io/en/latest/elements.html#images


推荐阅读