This project is read-only.

Images in Templates

Jun 27, 2011 at 8:02 PM

Is it possible to create a template with a spot to add in an image?

Mar 21, 2012 at 3:15 PM
Edited Mar 21, 2012 at 3:17 PM

This is pretty much untested. but this is working for me (although mine is slightly different):

  1. add the following function to PHPWord/Template.php :
     public function save_image($id,$filepath,&$document=null) { 
        	if(file_exists($filepath))
        	{
        		$this->_objZip->deleteName('word/media/'.$id);    		
        		$this->_objZip->addFile ($filepath,'word/media/'.$id);
        		//$document->setValue($id.'::width', "300px");
        		//$document->setValue($id.'::height', "300px");
        	}   
        }
  2. create a document with an actual image to be used as a place holder (this solution don't allow setting the with & height of the image or multiple extensions).
  3. unzip the the documnt and check for the file name in word/media folder. use this file name as the $id for the save_image function.

you can now use:

$document->save_image('image1',$image_path,$document);

$id
Apr 14, 2014 at 5:32 PM
Hello!

I tried your example but I got the following error:
Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file.' in C:\Program Files (x86)\EasyPHP-5.3.6.1\www\ihomologacion\Classes\PHPWord\PHPWord\Template.php:180 Stack trace: #0 C:\Program Files (x86)\EasyPHP-5.3.6.1\www\ihomologacion\word.php(18): PHPWord_Template->save('./wordRelleno.d...') #1 {main} thrown in C:\Program Files (x86)\EasyPHP-5.3.6.1\www\ihomologacion\Classes\PHPWord\PHPWord\Template.php on line 180

Thank you a lot for your help!
Jul 7, 2015 at 6:06 PM
add in Template.php class :

public function replaceStrToImg( $strKey, $arrImgPath ){
        $strKey = '${'.$strKey.'}';
        if( !is_array($arrImgPath) )
            $arrImgPath = array($arrImgPath);
        
        $rels = $this->_objZip->getFromName('word/_rels/document.xml.rels'); 
        $types = $this->_objZip->getFromName('[Content_Types].xml'); 
        
        $count =  substr_count($rels, 'Relationship') - 1;
        $relationTmpl = '<Relationship Id="RID" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/IMG"/>';
        $imgTmpl = '<w:pict><v:shape type="#_x0000_t75" style="width:289px;height:108px"><v:imagedata r:id="RID" o:title=""/></v:shape></w:pict>';
        $typeTmpl = ' <Override PartName="/word/media/IMG" ContentType="image/EXT"/>';
        $toAdd = $toAddImg = $toAddType = '';
        $aSearch = array('RID', 'IMG');
        $aSearchType = array('IMG', 'EXT');
        
        foreach($arrImgPath as $index => $img ){
            $imgExt = array_pop( explode('.', $img) );
            if( in_array($imgExt, array('jpg', 'JPG') ) )
                $imgExt = 'jpeg';
            $imgName = 'img' . ( time() + $index ) . '.' . $imgExt;
            $rid = 'rId' . ($count + $index);
            
            $this->_objZip->addFile($img, 'word/media/' . $imgName);
            
            $toAddImg .= str_replace('RID', $rid, $imgTmpl) ;
            
            $aReplace = array($imgName, $imgExt);
            $toAddType .= str_replace($aSearchType, $aReplace, $typeTmpl) ;
            
            $aReplace = array($rid, $imgName);
            $toAdd .= str_replace($aSearch, $aReplace, $relationTmpl);
        }
        
        $this->_documentXML = str_replace( '<w:t>' . $strKey . '</w:t>', $toAddImg, $this->_documentXML);
        $types = str_replace('</Types>', $toAddType, $types) . '</Types>';
        $rels = str_replace('</Relationships>', $toAdd, $rels) . '</Relationships>';
       
        $this->_objZip->addFromString('word/_rels/document.xml.rels', $rels);
        $this->_objZip->addFromString('[Content_Types].xml', $types);
        $this->_objZip->addFromString('word/document.xml', $this->_documentXML);
    }


use example:

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate($template);
$arrImagenes =  array(
    '../../images/mc.png',
    '../../images/logo.png',
    '../../mineria/image/logo.jpg',
    '../../images/draw-icon.png'
    
);
$document->replaceStrToImg('AreaImg', $arrImagenes);
$documentName = 'Concepto_Tecnico_' . date('Ymd_His') . '.docx';
$document->save( $documentName);