0

I was trying to run an example given in a book that produces a PNG image on the page:

<?php
//set up image
$height = 200;
$width = 200;

$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255);

//draw on image
imagefill($im, 0, 0, $blue);
imageline($im, 0, 0, $width, $height, $white);
imagestring($im, 4, 50, 150, 'Sales', $white);

//output image
Header('Content-type: image/png');
imagepng($im);

//clean up
imagedestroy($im);
?>

The problem is that when I run it, all I get is a broken image icon. Firefox additionally tells me that the image can't be displayed because it contains error. What am I doing wrong?

ankush981
  • 5,159
  • 8
  • 51
  • 96

2 Answers2

3

Your content type is wrong. You're making a JPG but telling the browser it is a PNG:

Header('Content-type: image/png');
imagejpeg($im);

should be

Header('Content-type: image/jpeg');
imagejpeg($im);

edit

This question was edited to correct this. This edit solves the problem: http://codepad.viper-7.com/FTW3g0

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • John, no idea why you think it is not rendering as a PNG, but I am using MAMP with PHP 5.4 on Mac OS X and can see it clear as day. – Giacomo1968 Apr 15 '14 at 01:57
  • Their **original** code had the error I show above. They since edited their question to correct this. I left my answer and then posted the link demonstrating that it now works. – John Conde Apr 15 '14 at 01:59
  • The first part of my answer. They originally had `imagejpeg($im);` instead of `imagepng($im);`. – John Conde Apr 15 '14 at 02:00
  • 1
    But you have `Header('Content-type: image/png'); imagejpeg($im);` `png` + `imagejpeg` and vice-versa. Shouldn't that be `Header('Content-type: image/jpeg'); imagejpeg($im);` and `Header('Content-type: image/png'); imagepng($im);`? – Funk Forty Niner Apr 15 '14 at 02:03
  • Now that's a horse of a different color! That was an unintended mistake on my part. Corrected. – John Conde Apr 15 '14 at 02:04
  • *Ah*, there we go (Et voilà) ;-) Plus uno. – Funk Forty Niner Apr 15 '14 at 02:05
  • This is probably a sign that I need to stop braining for the day to go to bed. – John Conde Apr 15 '14 at 02:05
  • These darn things have a way to play tricks on our eyes for sure. – Funk Forty Niner Apr 15 '14 at 02:07
  • I suspect OP has a space before ` – Funk Forty Niner Apr 15 '14 at 02:10
  • Assuming the code above is really what they are using then that's the only thing it can be. At least they know they have working code. – John Conde Apr 15 '14 at 02:12
  • Which is why I voted to close. – Funk Forty Niner Apr 15 '14 at 02:17
  • Hi folks. Check my answer. Could `Content-type` versus `Content-Type` (note the case difference for `Type`) be a factor? – Giacomo1968 Apr 15 '14 at 02:19
  • @JakeGould I tried with both `header('Content-Type: image/png');` and `Header('Content-type: image/png');` and even mixed case `HeadEr('Content-tYpE: image/png');` and it worked fine. FF 28.0 – Funk Forty Niner Apr 15 '14 at 02:28
  • @JohnConde Thank you! I had to fiddle around a little but your solution showed me the right direction. It looks like I'm able to render jpeg but not png. Changing to `header('Content-Type: image/jpeg')` and `imagejpeg($im)` worked for me. Any idea why that might be? – ankush981 Apr 15 '14 at 14:37
1

I am able to run this in PHP 5.4.10 without issue. And viewable as a PNG:

enter image description here

What version of PHP are you using? Are you sure the GD library is installed? Open up a PHP file & place this phpinfo command at the top of it to see if GD is loaded:

<?php
  phpinfo();
?>

That said, I did notice an odd error in PHP 5.2.17 which I can switch to in my MAMP setup: Even if I have the command die(); after your code, the page bombs out. Something is different in the PHP parser between PHP 5.4.10 and PHP 5.2.17 it seems. But that is just me. Unclear what your setup is like.

EDIT I think I figured out the issue. Some browsers are case sensitive to headers. So change this:

//output image
Header('Content-type: image/png');
imagepng($im);

To this:

//output image
header('Content-Type: image/png');
imagepng($im);

Note the capital T in Content-Type in my edit. Try that out.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103