title: Laravel generate image and add content type header
body: <p>Hi I'm using php's gd library to generate images. I've decided to integrate this into laravel, and I managed to make it work.</p>
  
  <p>My problem is that if laravel sometimes overwrites my content-type header.</p>
  
  <p>This is my controller:</p>
  
  <pre><code>public function imga($algorythm,$w=false,$h=false){
      if (!$w) $w=rand(250,750);
      if (!$h) $h=rand(250,750);
      $im = imagecreatetruecolor($w, $h);
  
      //Here some fancy function is called
      if (method_exists($this,'img_'.$algorythm)){
          $this-&gt;{'img_'.$algorythm}($im,$w,$h);
      }
  
      header("Content-type: image/png");
      imagepng($im);
      imagedestroy($im);
  }
  </code></pre>
  
  <p>Most of the times if the image is large enough the browser displays it just as expected, but if the image is to small laravel overwrites the content type header with "text/html; charset=UTF-8".</p>
  
  <p>I've read <a href="https://laravel.com/docs/5.4/responses" rel="nofollow noreferrer">https://laravel.com/docs/5.4/responses</a> but to make it this way I need to have a string.</p>
  
  <p>So I've looked at this: <a href="https://stackoverflow.com/questions/9370847/">PHP: create image with ImagePng and convert with base64_encode in a single file?</a> but I'm not sure if this is the right way, it looks like a dirty hack to me.</p>
  
  <p>Should I put the imagepng call inside a view and add the headers there, isn't this a bit overkill?</p>
  
  <p>How to use functions that output data instead of returning it in laravel.</p>