-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Replace or add an image on a template #260
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
Comments
Hi @dmelo. You can't do that with PHPWord right now. We'll take this as an enhancement request. |
+1 : StackOverflow |
+1 I also need such a feature in order to insert some QRcode or so on in a Word document (and I always use a template as a rich base document) |
FYI @dmelo @Progi1984 code in StackOverflow has been updated in order to be compatible with last phpWord (0.11) |
Thanks @jdespatis. I think one of the simplest generic tool that we can develop is a document parser that can report the structure of a document, e.g. name of images and styles. |
+1 @ivanlanin Would be awesome Really not handy the above hack when we have several images in the template... They must all be present and in a specific order |
Thank you. I will test it soon. |
I'm working on |
Hi, I'm interested in that function too ! For the moment i'am using the hack proposed on StackOverflow, but it forces you to add the right number of (different) images in your template ..... |
@ivanlanin For the addChart, may be you can see from the side of phpoffice/phppowerpoint |
@Progi1984 Thanks for the direction. Very useful. There's an increase in my daily workload, so the progress is slow :) |
+1 |
+1 Waiting for this enhancement too! Would be great to be able to add (or replace) images of different sizes in a template. |
+1 |
+1 Perhaps setValue can just expect a $section object and if that contains an image only it can use that? |
I am trying to use the hack in StackOverflow, but I think I am misunderstanding how to use it. I am trying to use an image that was in my documents. Is that not right? Here's the function call: First sig2.png is what the name of the image I inserted into the Word document. Second param is where the new sig image is located: NEVERMIND! Saw a note somewhere to open it up in WinRAR to see what the actual image name is. |
I'm also looking for this. Doesn't have to replace an image, could just be adding an image. A kind of hacky way is to load the template, merge it, save it as new document and then load it again as a regular document and add an image to a section. Is there some way to tell PhpWord to insert it into some specific section/table or anything, without screwing up other pictures? |
+1 in this. It would save a lot of work |
+1, this functionality will be very handy |
I tried to use the hacks provided on stackoverflow, but first problem appears when calling "addFile" method, ZipArchive dont have it defined, I tried to call "pclzipAddFile" method instead, but here goes the seccond problem, a fatal error that the "add" method is not defined in "pclzipAddFile" method |
I'm trying to add an image to my template but when I open the document I receive an error message. Someone has a simple solution? |
As I know, adding images to templates isn't possible yet, you can set only string values. You can add images to an existing document by opening it first, the error may occur when you try to open the document but the file doesn't exist or you have no rights to do this action, check this because no error is thrown in this case, just the document isn't loaded |
The stackoverflow solution is all well and good if you're replacing a single image, but what if you need to repeat a row of data each with an image in? I've been trying to do this simply buy using ZipArchive when I stumbled on this and thought I found the holy grail with the addImage method. However, as just like everyone else here I'm stuck when it comes to a template. I know the code to output an image into a word docx manualy, I know how to add an image, I just can't get the word/_rels?document.xml.rels to update in code. So my next train of thought has been is it possible to load a docx file and open it in the standard PHP word and manually do the find and replacing for the template tags? So far I've got |
Okay, after spending the day doing this. I have now hacked together a solution which might help others, but please note I needed to output images on row data. Basically this is what I'm doing adding a image tag to the template testPhoto which on a row would be testPhoto#1 and testPhoto#2. I pass in the photo name for the ZipArchive and the image source. $templateProcessor->setImageValue('testPhoto#2', 'image4.png', 'my-image.jpg'); I then force in the word coding which isn't pretty but work, on the tag replacement, add the image to the folder and then recreate the word/_rels/document.xml.rels file on each call. If I've done the pull request correctly it should be found at: Hope this leads someone else who has more time than me to come with a better solution. |
A quick hack solution I came up with today. This isn't pretty in the slightest and is my first pull request so apologise if I've done it wrong. I normally work on BitBucket. Example Usage ------------------ $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('C:\Users\Robert\Desktop\sample.docx'); $templateProcessor->cloneRow('rowValue', 10); $templateProcessor->setValue('test#1', htmlspecialchars('Text goes here')); $templateProcessor->setImageValue('testPhoto#1', 'image3.png', 'my-other-image.jpg'); $templateProcessor->setValue('test#2', htmlspecialchars('Text2 goes here')); $templateProcessor->setImageValue('testPhoto#2', 'image4.png', my-image.jpg');
Wow! This looks like it could work. My solution was to just re-write the entire document without a template, which wouldn't work if my document wasn't dead simple. I haven't tried it yet, but it looks like you may have cracked it! Great work! |
this is a full example |
Thanks, |
i have discovery that the second image load a Content typel xlm file empty $types = $this->zipClass->getFromName('[Content_Types].xml'); any suggestion? |
After having three days of trying to figure out why when i download a document from drive using google Drive APIs and trying to manipulate the replace image doesn't work i figured out you have to change the picture XML since using the provide solution won't work if your app download the docs from drive and then push back the document to drive without user interaction with office application First google docs creates this xml <w:t xml:space="preserve"> instead of <w:t>
then alternative to add <w:pict><v:shape type="#_x0000_t75" style="width:WIDpx;height:HEIpx">
|
@mansonkibe glad you got it to work, I was meaning to comment but I forgot sorry. Your solution looks very similar to my earlier comment, see OAFCROB@ca52b09 I found that I had to also fork the branch for my particular needs |
this would also be a good feature that my app needed.. any updates on this? thanks. |
1 similar comment
this would also be a good feature that my app needed.. any updates on this? thanks. |
I really need this too!! |
+1 |
I compiled a few different ideas for the template.php... It has a better setValue function and replaceStrToImg. I had trouble with the original not recognizing some tags and also replacing a tag with an image didn't work correctly so I used a modified version of setValue to better find and replace the tags using preg_match and also with the replace StrToImg.... Hope this helps as much as it has me.... `class PHPWord_Template {
private $_header1XML;
//add function
}` |
I'd also vote for |
Hi guys, I was having an issue loading .docx from google, and I after research for some google does not handler images within shape objects, I did a change replacing the object shape image for a corresponding object image of word, I am happy to share with you my solution: In your extended class PhpOffice\PhpWord\TemplateProcessor add replace the above function for this:
Now in your template.docx you must to have an macro like this
In your .php action:
I hope that this would help you, thanks for share your experiences. |
any update related to this topic add image in template? |
No, this is the last one at the moment, keep in mind that that does not support replace macros from header and footer. In me case on the body is ok |
In case anyone comes here looking for a solution to insert the images into the header or footer, I've created a small repo here with the template processor file that can do it. |
@CodeJason you are my Hero¡¡¡¡ jajaj Looking for this solution for a long time. You really save my LIVE. Thank You Very Much...¡¡¡¡ GOOD JOB |
(Y) |
@CodeJason Muchas Gracias, thank you very much =) |
Thank you @CodeJason !!! Appreciate it! 👍 |
@CodeJason wow man! Really thanks! works like a charm. |
@CodeJason : Thanks a million. Works. You saved me days of works |
@CodeJason Thanks you. |
still has problem for image style. I want this image stay in background or foreground, not inline. |
@CodeJason Im trying to make this work with v0.14.0 but instead of image I'm getting just white space in size of the image or grey box with red x and message 'This image cannot currently be displayed'. Any ideas? |
Gracias @CodeJason gaste horas buscando la solucion hasta que llegue a tu comentario |
Could it be added to the main project? |
I could't find how to add or replace an image on a template.
I make an instance of PhpWord and use loadTemplate. I'm able to use setValue but I can't find out how to replace an image or add an image.
Is there a way to add or replace an image inside a template?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: