views:

369

answers:

4

Is it possible to use multiple header types in one document? For example:

header("Content-type: image/jpeg");
header('Content-Type: text/html; charset=utf-8');

returns the whole page as text/html... while

header('Content-Type: text/html; charset=utf-8');
header("Content-type: image/jpeg");

Returns the whole page as an image.... How can I use both types of content on the same page? I'm using ob_start() at the top and ob_end_flush() at the beginning.

+2  A: 

No, a resource has just one media type. HTTP allows multipart messages that can contain multiple entities. But that’s not widely supported by today’s browser.

Gumbo
This is helpful... i think my answer is to use <img src="my_img.php" /> where my_img.php is essentially a function that outputs an image.
Jon
A: 

The entire point of content-type is to tell the browser how to handle a request. What would you expect a browser to do if you're sending text/html and image/jpeg? It has to be one or the other, it can't be both. text/html is kind of a catch-all.

ryeguy
A: 

You can't use both at the same time, but you could optionally use one or the other based on a query parameter or similar.

In regards to using both types of content in one page - in a normal HTML page with <img> elements, for example, those are separate HTTP requests so they have their own Content-Type header.

Not exactly sure what you're trying to achieve here...

Peter Bailey
hmm... ok thanks. I'm trying to dynamically generate thumbnails on the fly with a function that returns an image resource using imagejpg($img) after running imagesampleresize etc etc... Problem is I can't seem to do it 'on' the page by calling a function.. .either the page displays text, or it displays an image... can't seem to output an image resource ON a text/html page.
Jon
+1  A: 

You can't. But what you can do is something like this in your HTML:

<img src="my_img.php" />

Of course my_img.php would be a PHP file that has a header("Content-type: image/jpeg"); line, and outputs your image.

Erik
I think this is what I was looking for... thank you!!
Jon