1

The following code working fine when use in simple php but when i use the same in laravel view then it show me the error

Code

header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);

Error in laravel

enter image description here

Simple PHP output

enter image description here

Sam
  • 151
  • 1
  • 1
  • 9
  • try setting the correct content-type in your header: `header('Content-type:image/png');` – Adrenaxus May 07 '18 at 07:38
  • yes i did it before but the result is the same – Sam May 07 '18 at 07:40
  • 1
    Where is the code you have shown us located? Inside a view? `header()` must be called before any other PHP output. Also check your logfile (laravel.log) for further clues on the issue. – Adrenaxus May 07 '18 at 07:54
  • The "error" you're seeing is the actual PNG data you're trying to send. If you check [the PNG spec](http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html) you'll see that the first eight bytes of any PNG file are 137 80 78 71 13 10 26 10; the three ASCII-printable characters of that (80, 78, 71) are "PNG". This is followed by an "IHDR" chunk. That does rather point to the problem being a Content-Type one. Confirm the problem by using a web debugger (like your browser's debugging tools) to look at the headers sent in the response and see if the Content-Type header is there. – Matt Gibson May 07 '18 at 08:04
  • (Is this your *entire* view code? Can we see it all, for real, from the ` – Matt Gibson May 07 '18 at 08:12
  • As I pointed out earlier, the problem is that you are executing `header();` *inside* your view. You need to outsource that code to your controller. Having code inside your views is not a good practice. You need to use something like this inside your controller: `return response()->view('hello', $data, 200)->header('Content-Type', $type);` – Adrenaxus May 07 '18 at 08:18
  • Please check this https://stackoverflow.com/a/49704951/8317643 – DEarTh May 07 '18 at 08:55

1 Answers1

0

Your code is working fine. All you need is to add an "exit" at the end.

Code:

header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
exit;

EDIT:

The main problem is that you are trying to change the response header into the view. You need to change it on the controller function that call the view. For example:

return \Response::view('myView')->header('Content-Type', 'image/png');

And then, in your PHP code all you need to do is remove the first line:

$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
istaro
  • 139
  • 1
  • 12