This project is read-only.

addImage not working

Jun 8, 2011 at 5:54 PM

I've got my code the create a doc and i'm able to write in text but when i try add an image i dont receive an error but when i open the .doc the image hasn't been added, code below, any ideas?

 

          $docx = new word();
          $section = $docx->createSection();
          $section->addText('Hello world!');
          $section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
          $section->addImage('http://localhost/label_creator/wp-content/media/Become-a-friend_small.jpg');
          $objWriter = PHPWord_IOFactory::createWriter($docx, 'Word2007');
          $objWriter->save('helloWorld2.docx');
all lines work apart from addImage, and that is defo the correct image path

Jun 8, 2011 at 6:18 PM

The path to the image should be full path from the root of the web server as specified in httpd.conf

$section->addImage('/label_creator/wp-content/media/Become-a-friend_small.jpg');
Jun 8, 2011 at 6:57 PM

thanks Jillelaine, i'll try that. Maybe the docs need updating if this is the case

Parameters

Type

Description

$src

String

Path to the image on the web server. The path can be a relative or a absolute path.

Jun 8, 2011 at 7:03 PM

The docs are correct, and I should have been more clear in my post. A relative path to the image will also work. So, if your image is in the same directory as your PHPWord code that creates the doc, you can use:

$section->addImage('Become-a-friend_small.jpg');

But, as far as I know, you should not use the http:// protocol to define the path to the image.

Jun 8, 2011 at 7:27 PM
Edited Jun 8, 2011 at 7:27 PM

thanks for clearing that up. Still no luck though i've tried relative, absolute, same folder, from the gd library but no images output onto the doc, it might have something to do with using the class as part of a wordpress plugin library. I had the same probs with phpdocx although i'm getting them in a pdf through fpdf library, very strange. Is there any error handling i can use?

Sep 13, 2012 at 9:32 AM
$docx = new word();

isn't new  PHPWord()?
Oct 10, 2012 at 5:13 AM

if i have this folder structure :

project_name/

 ..........................app/

 .......................................vendors/phpdocx -> phpword source file

 ........................................views/events/documents/word_template.ctp -> this contains the code and layout    of my generated document

.......................................webroot/img/image.png -> the image i would like to add

 

how can i insert an image on the document.

 

Thanks!