tags:

views:

99

answers:

3
header("Content-type:   image/gif");  
readfile($filename);

The above can only be used to show gif images.

Is there a header that can be used to show jpg/png/gif?

+4  A: 
header("Content-type:   image/gif");

OR

header("Content-type:   image/jpeg");

OR

header("Content-type:   image/png");
jitter
Do I have other options than list them all?
another
**You can't use more than one.** It should match the content you're sending. If you're readfile'ing a GIF, it should be `image/gif`, a JPEG should be `image/jpeg`, and a PNG should be `image/png`.
ceejayoz
Look <img src="" />,you dont have to specify its exact type.
another
That's because servers send the right content type so the browser knows what to do with it.
ceejayoz
@new - This is called a MIME Type. http://en.wikipedia.org/wiki/Internet_media_type
txyoji
It's also used to server http request,can I leave the content type to servers like <img src="" />?
another
The server will handle `<img>` tags because it's directly serving the image file. It won't handle a PHP script using `readfile()` because all the server knows is that it's a PHP script - it'll try to use `text/html` like all other PHP scripts. This is why you have to specify a `Content-type` header in such a script.
ceejayoz
Then is there a way to let server directly serve the image file?
another
Yes - link to it directly instead of using a PHP script. Otherwise, you'll need to use one of the (easy) MIME type detection techniques described in other answers.
ceejayoz
+3  A: 

You need to know or figure out what type of file it is, and send the proper type. There's no catch-all content type for images that'll work for GIF, PNG, and JPEG all at once.

finfo_file() will let you detect the type of an image (or any other file).

ceejayoz
+4  A: 

This should work for all image types:

$size = getimagesize($filename);

header('Content-type: ' . $size['mime']);
readfile($filename);
Alix Axel