How to add Image to template?

Feb 12, 2014 at 6:41 AM
Hi all,
I need add image to doc template. Who can help me?
Thanks.
Mar 20, 2014 at 5:12 AM
Currently you can't do that. We're working on DOCX Reader. Once this is completed, you can manipulate the template easier.
Jul 7, 2015 at 5:04 PM
Edited Jul 7, 2015 at 5: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);