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.
My problem is that if laravel sometimes overwrites my content-type header.
This is my controller:
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->{'img_'.$algorythm}($im,$w,$h); } header("Content-type: image/png"); imagepng($im); imagedestroy($im); }
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".
I've read https://laravel.com/docs/5.4/responses but to make it this way I need to have a string.
So I've looked at this: PHP: create image with ImagePng and convert with base64_encode in a single file? but I'm not sure if this is the right way, it looks like a dirty hack to me.
Should I put the imagepng call inside a view and add the headers there, isn't this a bit overkill?
How to use functions that output data instead of returning it in laravel.