title: Serving image's data string over HTTPS while URLs on HTTP
body: <p>I am attempting to read an image file, as a base64 encoded string and output a data string when the connection uses HTTPS/SSL, and otherwise put out the URL in the IMG src attribute if it is only on HTTP. Here is my current code, however it does not work.</p>
  
  <pre><code>&lt;?php 
  function base64_encode_image($filename, $filetype) {
      if (($_SERVER["HTTPS"] == "on") &amp;&amp; $filename) {
          $file = "/home/content/61/9295861/html/resource/image$filename";
          $imgbinary = fread(fopen($file, "r"), filesize($file));
          return "data:image/$filetype;base64," . base64_encode($imgbinary);
      } else {
          return $filename;
      }
  }
  ?&gt;
  &lt;img src="&lt;?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?&gt;" width="96" height="72" /&gt;
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>&lt;img src="&lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  fopen(/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png) [&lt;a href='function.fopen'&gt;function.fopen&lt;/a&gt;]: failed to open stream: No such file or directory in &lt;b&gt;/home/content/61/9295861/html/theme/latest/index.php&lt;/b&gt; on line &lt;b&gt;5&lt;/b&gt;&lt;br /&gt;
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  filesize() [&lt;a href='function.filesize'&gt;function.filesize&lt;/a&gt;]: stat failed for /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png in &lt;b&gt;/home/content/61/9295861/html/theme/latest/index.php&lt;/b&gt; on line &lt;b&gt;5&lt;/b&gt;&lt;br /&gt;
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  fread() expects parameter 1 to be resource, boolean given in &lt;b&gt;/home/content/61/9295861/html/theme/latest/index.php&lt;/b&gt; on line &lt;b&gt;5&lt;/b&gt;&lt;br /&gt;
  data:image/png;base64," width="96" height="72" /&gt;
  </code></pre>