2

I have tried several times the string in the description. But it keeps outputting a string instead of an image. Link is here http://wenti.de/resize.php.

Source code is below,

$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
  $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );

Is there a workaround without base64 conversion?

Sam
  • 6,961
  • 15
  • 44
  • 63
Philipp Braun
  • 1,563
  • 2
  • 23
  • 39
  • 2
    Just tested I get a nice image. Do u have GD library installed ? – Abhik Chakraborty Mar 21 '14 at 14:08
  • tried running it over my domainprovider – Philipp Braun Mar 21 '14 at 14:09
  • you could be right but is there another possibility to display an image. i need this for a conversion class. otherwise probably i need to use a base64 conversion i guess. – Philipp Braun Mar 21 '14 at 14:10
  • 2
    Your code is fine, it's working if I test it on my webserver. So the problem is, that the header function does not change the content type. Maybe this can help: http://stackoverflow.com/questions/2508718/content-type-not-working-in-php – Leif Mar 21 '14 at 14:11
  • 1
    It looks like your PHP script is emitting a UTF-8 BOM (byte-order-marker). Make sure your .php file is saved using the ASCII character set. – Jon Benedicto Mar 21 '14 at 15:07

1 Answers1

12

Your PHP script is emitting a UTF-8 byte order mark (EF BB BF) before outputting the PNG image content. This marker is probably causing PHP to default the content type to text/html. Your subsequent call to set the header is ignored because PHP has already sent the BOM.

The BOM has probably been placed in your PHP script just before the opening tag by your text editor, because it's saving the file in UTF-8 format. Change the format in your editor to ANSI/ASCII so that the BOM is not written.

Alternatively, you could try calling PHP's ob_clean() function to clear the output buffer before changing the header.

Jon Benedicto
  • 10,206
  • 3
  • 26
  • 30
  • ASCII is bad, we just want to get rid of BOM. Every modern editor can save files using UTF encoding without BOM (notepad is *not* a modern text editor :P ). – Ale May 16 '14 at 18:58
  • By placing ob_clean() header('Content-Type: image/png'); and imagepng(); started working. – Kennedy Nyaga Apr 08 '15 at 18:33
  • Many thanks, Jon! I've used ob_clean() and it worked in my case. – Gabriel Apr 24 '16 at 12:19