Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to TemplateProcessor for rendering HTML content.Include Image #2547

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
format
Maybe-U committed Jan 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit aabbc3cbc0597f479fc087aade64ae1b74755f50
2 changes: 1 addition & 1 deletion src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
@@ -334,7 +334,7 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
if (!empty($imageSrcList)) {
foreach ($imageSrcList as $imageSrc) {
try {
$content= file_get_contents($imageSrc);
$content = file_get_contents($imageSrc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of the code is quite clever, but (a) I'm not really sure it's needed, and (b) I don't think you've coded it correctly. I would personally eliminate all the code after addSection and before addHtml and just let it fail later if the file isn't available; your changes to your test member are sufficient to address my original concern by making the files local rather than external. But, I can see that this checking might be perceived as a benefit, so let's discuss (b). file_get_contents does not normally throw an exception (I am not expert in the Php internals, so I suppose there might be some edge case where it throws); it normally just returns false if it fails and issues some warning messages describing the failure. So, I think what you want to do is:

foreach ... {
    $content = @file_get_contents(...); // suppress warning messages
    if ($content === false) {
        $localImg = ...
        $htmlContent = ...
    }
}

Copy link
Author

@Maybe-U Maybe-U Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks you for your Suggestion,I wasn't thoughtful enough ,I reviewed the html::add html method,Use @ to suppress errors when processing images in this method
image
so i eliminate all the code after addSection and before addHtml Perhaps the abnormal picture should be handled by the user itself.

In addition, this is my first time to submit pr. Could you please help me merge this pr
@oleibman

} catch (\Exception $e) {
$localImg = __DIR__ . '/resources/doc.png';
$htmlContent = str_replace($imageSrc, $localImg, $htmlContent);