title: Capturing PNG stream with PHP output buffer
body: <p>I am currently attempting to write a web service that generates a QR code for a given string. I am using PHP QR Code (<a href="http://phpqrcode.sourceforge.net/" rel="nofollow noreferrer">http://phpqrcode.sourceforge.net/</a>) to generate the QR code, like this:</p>
  
  <pre><code>QRcode::png('PHP QR Code :)');
  </code></pre>
  
  <p>Result: a PNG stream of the QR Code is displayed on the screen.
  As this is a void function that outputs directly to the browser, this prevents me from properly returning the image.</p>
  
  <p>I would like to capture the PNG stream with PHP's output buffer and have my web service return this captured image.
  I have set up the following test code on a plain php page for testing purposes:</p>
  
  <pre><code>include('/vendor/phpqrcode/qrlib.php');
  
  $scancode = "testcode";
  
  ob_implicit_flush(false); //just in case
  ob_start();
  QRcode::png($scancode);
  $output = ob_get_contents();
  ob_end_clean();
  echo "--" . $output . "--";
  die();
  </code></pre>
  
  <p>However, when I run the code the buffer does not work as I had planned. No matter what I do, a broken image is always displayed as soon as I call the function. When I remove the output buffer code the QR code is rendered on the screen.</p>
  
  <p>Why is some of my output not being captured?</p>
  
  <p>Edit: related: <a href="https://stackoverflow.com/questions/11352334/phpqrcode-library-return-image-as-a-string">phpqrcode library return image as a string</a></p>