title: scan and edit img width height through php before output
body: <p>this might be a funky question, but I wonder if anyone could think of a way how to take a chunk of html, scan it for <code>&lt;img&gt;</code> tags and if the tag has no width+height value apply it with <code>list($width, $height, $type, $attr);</code> ?</p>
  
  <p>More in detail, I have a php page that includes another page with html only.
  I would want the html to be changed before output to browser.</p>
  
  <p>This is a simplified version of what I am looking at:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="content"&gt;
  &lt;?php 
  include_once("client-contributed-text-and-images.php");
  ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>after some input below I came up with following:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="content"&gt;
  &lt;?php
  $dom = new DOMDocument();
  $dom-&gt;loadHTMLFile("client-contributed-text-and-images.php");
  
  foreach ($dom-&gt;getElementsByTagName('img') as $item) {
  
      $item-&gt;setAttribute('width', '100');
      echo $dom-&gt;saveHTML();
      exit;
  }
  ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The problem is that it generates a complete html4 file in the middle, while only changing the first img tag and seemingly not outputting the code afterwards:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="content"&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
  &lt;html&gt;&lt;body&gt;&lt;img src="img1.jpg" width="100"&gt;&lt;h1&gt;header&lt;/h1&gt;
  &lt;p&gt;some text&lt;/p&gt;
  &lt;a href="http://google.com"&gt;some link&lt;/a&gt;
  &lt;img src="img2.jpg"&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>so I switched gears and tried fopen() instead and got it to work partly:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="content"&gt;
  &lt;?php
  $root = realpath($_SERVER['DOCUMENT_ROOT']);
  $file = $root."/client-contributed-text-and-images.php";
  $f = fopen($file, 'r');
  $contents = fread($f, filesize($file));
  fclose($f);
  
  $new_contents = str_replace("&lt;img ", "&lt;img width='100' height='100' ", $contents); 
  echo $new_contents;
  ?&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>which gave:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="content"&gt;
  &lt;img width='100' height='100' src="img1.jpg"&gt;
  &lt;h1&gt;header&lt;/h1&gt;
  &lt;p&gt;some text&lt;/p&gt;
  &lt;a href="http://google.com"&gt;some link&lt;/a&gt;
  &lt;img width='100' height='100' src="img2.jpg"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Now I just need some help with figuring out how to implement <code>list($width, $height, $type, $attr);</code> to include right with and height (and obviously only when it is not already set).</p>