title: how to use php to include an image in a word file?
body: <p>Somebody has asked me to make an app in php that will generate a .doc file with an image and a few tables in it. My first approach was:</p>
  
  <pre><code>&lt;?php
  function data_uri($file, $mime) 
  {  
    $contents = file_get_contents($file);
    $base64   = base64_encode($contents); 
    return ('data:' . $mime . ';base64,' . $base64);
  }
  $file = 'new.doc';
  $fh = fopen($file,'w');
  $uri = data_uri('pic.png','image/png');
  fwrite($fh,'&lt;table border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;something&lt;/b&gt;&lt;/td&gt;&lt;td&gt;something else&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;br/&gt;&lt;img src="'.$uri.'" alt="some text" /&gt;
  &lt;br/&gt;
  &lt;table border="1"&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;ceva&lt;/b&gt;&lt;/td&gt;&lt;td&gt;altceva&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;');
  fclose($fh);
  ?&gt;
  </code></pre>
  
  <p>This uses the data uri technique of embedding an image.</p>
  
  <p>This will generate an html file that will be rendered ok in web browsers but the image is missing in Microsoft Office Word, at least in the standard setup. Then, while editing the file with Word, i've replace the image with an image from file and Microsoft Word changed the contents of the file into Open XML and added a folder, new_files where he put the imported image (which was a .png), a .gif version of the image and a xml file:</p>
  
  <pre><code>&lt;xml xmlns:o="urn:schemas-microsoft-com:office:office"&gt;
    &lt;o:MainFile HRef="../new.doc" /&gt; 
    &lt;o:File HRef="image001.jpg" /&gt; 
    &lt;o:File HRef="filelist.xml" /&gt; 
  &lt;/xml&gt;
  </code></pre>
  
  <p>Now this isn't good enough either since i want this to be all kept in a single .doc file.
  Is there a way to embed an image in an OpenXML-formatted .doc file?</p>