-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
New function in TemplateProcessor to add images to documents #1084
Conversation
- utility functions to parse lengths given in absolute CSS units (in. px, mm, cm, pc)
Image placeholders have the form ${img:name}. The width and height of the image to insert may be specified as additional arguments to the placeholder: ${img:name:width:height} For the width and height, CSS units cm, mm, px, pt and pc may be used, e.g. ${img:beispiel:15cm:360pt}
(updated the PR to split into two PRs due to code overlap with #1086) |
that's helpful. |
@SadPencil tbh I'm not sure what would be possible internally in the DOCX, but it should be straightforward to allow the image to be passed as a string (no need for base64-encoding!) instead of a file path and then write the string to an internal file in the DOCX. It would require a little refactoring of addImageToArchive() and insertImage() (probably to API methods insertImageFromString() and insertImageFromFile() with a common helper method), but the changes to this PR should be pretty straightforward. Why not give it a go? |
When will this pull request be merged? |
@ejn: I like your work a lot. And I've got a working copy of something similar to insertImageFromString() but broader.
That already allows to retain the dimensions/position/properties of the image and only change the image. Tested to work with Office 365 and LibreOffice. Even if the new jpg has a bigger size. A working example is in my pull request: #1147 @SadPencil You can do exactly what you describe in the current PHPWord version 0.13; first define this class (You can do it inline, just above your code):
now instead of
and you have injected a new image. |
@ejn Still working on a united script, can you make your script that it also allows a insertImageFromString() and a insertImageFromFile() where the latter just uses the first to do all the job? Still waiting for your Feedback about if it is better to have uniform names, |
Hi @ejn , I'm trying to implement your changes, but the output file is borked. I've checked out your branch and using the code 100% as you pushed it. I've set up an empty file with just the variable to test, this is the result: Template: empty.docx When I open the output file, I get this message: After clicking "ok" here, I get the option to "recover the contents", which works, but it's not a viable solution. Am I missing something here? Or what could cause this issue? |
@rkorebrits: Looking at the result file and the file after Word has repaired it, the [Content_Types].xml is borked - I'm guessing by a . in the filename of the image you're adding which is being interpreted as the file extension (Extension="76766837.jpeg"). The workaround is probably to make sure that there is no . in the filename apart from before the real extension, a real fix would be to replace strpos with strrpos in line 820/821 (or do getting the extension in some other better way). If/when I get around to reworking this to take care of the points raised by @FBnil (using the existing methods to generate the XML rather than just slapping in pre-prepared chunks) then I'll be sure to take care of that too. |
Thanks @ejn! That did the trick :-) |
@ejn I have updated the code to check if there is an image passed, and if the image actually exists. If the image doesn't exist it threw a PHP error before on
|
I merged #1170 which is doing the same. Feel free to open a new PR if features are missing |
Image placeholders have the form ${img:<name>}.
The width and height of the image to insert may be specified as
additional arguments to the placeholder: ${img:<name>:width:height}
For the width and height, CSS units cm, mm, px, pt and pc may be used,
e.g. ${img:beispiel:15cm:360pt}