title: How to display binary data from curl in php
body: <p>I'm writing simple php proxy and I have trouble displaying png file, the output is</p>
  
  <p><img src="https://i.stack.imgur.com/DrMOI.png" alt="enter image description here"></p>
  
  <p>and it should be:</p>
  
  <p><img src="https://i.stack.imgur.com/y4MTy.png" alt="enter image description here"></p>
  
  <p>The images are opened in Notepad++. My php curl code look like this:</p>
  
  <pre><code>$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, $ua);
  curl_setopt($ch, CURLOPT_HEADER_OUT, 1);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  $content = curl_exec($ch);
  $info = curl_getinfo($ch);
  header('Content-Type:' . $info['content_type']);
  echo $content
  </code></pre>
  
  <p>I try with and without CURLOPT_BINARYTRANSFER the output is the same and the image is not displaying. How can I display the image?</p>
  
  <p><strong>EDIT</strong>: when I'm saving the data to the file and redirect using Location header the image is displayed correctly:</p>
  
  <pre><code>$file = fopen('proxy_tmp~', 'w');
  fwrite($file, $content);
  fclose($file);
  header('Location: ' . DIR . 'proxy_tmp~');
  </code></pre>
  
  <p><strong>EDIT 2</strong>: I had gzip compression, bu when I disabled it I have the same issue, when I open both files in Notepad++ one is DOS/Windows ANSI (original) and the other is DOS/Windows UTF-8 (the file opened by a script). When I open a file in Notepad and change encoding to ANSI and save the file, everything is ok.</p>
  
  <p><strong>EDIT 3</strong>: I think I did the same thing on GNU/Linux but without <code>CURLOPT_BINARYTRANSFER</code> option and it's working fine, here is my project <a href="https://github.com/jcubic/yapp" rel="nofollow noreferrer">https://github.com/jcubic/yapp</a>. I've also test it on Windows 10 with Wamp and also work fine.</p>