user3260816
user3260816

Reputation: 91

PHP header ('Content-Type: image/png')

I have a problem with header in php.

I havet this simple code.

<?php
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);
?>

I think it should works fine, but it didn't. There is no error, and in top-left corner of my browser a little crashed picture icon shows up.

I think header is the problem.

other useful infos: PHP Version 5.3.10-1ubuntu3.9 GD Support enabled GD Version 2.0 FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.8

UPDATE In IE even if i have header, i got bunch of data...

Upvotes: 9

Views: 53484

Answers (6)

David Spector
David Spector

Reputation: 1671

The correct (tested) PHP code for obtaining and returning a favicon (just change the MIME content type for other images) is:

$c = file_get_contents($path,true);
$size = filesize($path);
header ('Content-Type: image/x-icon');
header ("Content-length: $size");
echo $c;
exit;

Upvotes: 0

Tuckbros
Tuckbros

Reputation: 437

When I tried, my problem was I didn't have the gd library. Once installed and set up, it worked fine.

Upvotes: 0

dharmabook.ru
dharmabook.ru

Reputation: 137

For draw image in browser its have to send two headers before the main image data. First it is type of the data

header ('Content-Type: image/png'); 
//or
header ('Content-Type: image/jpeg');

and second it is data length. Without that header browser will be not know where the data ended.

header("Content-length: $size");   //$size image file size in bytes

need check php file have not any spaces and new lines before <?php and no php output before headers and image data output.

Upvotes: 9

AzCraft
AzCraft

Reputation: 11

Try with:

header ('Content-type: image/png');

instead of

header ('Content-Type: image/png');

And check the file encoding. It has to be ANSI.

Upvotes: -2

Your problem is path of folder Create a folder for image

for example.: barla

After change your header header("Content-type: barla/image/png");

Your problem will solve

Upvotes: -2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

The "little crashed picture" indicates that the image is invalid. Since your code to generate it seems valid, you must have a PHP error in there.

Comment out the header line, see what you get. If you get something that starts with PNG followed by a bunch of data, then you have a successful PNG image and you can uncomment the header line to see it. Otherwise, you may see a PHP error.

Upvotes: 3

Related Questions