Page 1 of 2

header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Tue Jun 09, 2009 10:48 am
by nickcherryjiggz
When I run a script that contains the following code:

Code: Select all

header('Content-Type: image/jpeg');
readfile("$IMAGES_FILE_PATH/oldman.jpg");
I think that my browser should be showing the image that's being referenced (whose contents are, in fact, being read correctly), but instead, it displays the URL of the script that was just run (http://127.0.0.1:8000/Jiggzy%20Quick-St ... custom.php).

I'm currently using MAMP (Version 1.7.2 - default installation) and running Leopard (10.5.7) on a MacBook. My PHP information can be found at http://www.jiggzy.com/phpinfo/.

Could anyone shed some light on the issue of why DreamHost is displaying the image properly, but not my local version of PHP? Thanks in advance for any help!

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Tue Jun 09, 2009 12:25 pm
by pickle
Check the headers sent by the file. You can get a Firefox extension that captures the headers. My guess would be that the headers on your localhost are different than Dreamhost. There are headers that can be sent by the server ahead of the headers you send in your file.

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Wed Jun 10, 2009 11:21 pm
by nickcherryjiggz
The red text at the bottom of the message is the header output from Localhost (doesn't work), and the green text is the header output from Dreamhost (does work). The two are certainly different. It seems that Localhost is sending two headers, while Dreamhost is only sending one. I'm assuming this might be the cause of my problems. I've tried using ob_start() (correctly?)...

Code: Select all

<?php
ob_start();
header('Content-Type: image/png');
imagepng(imagecreatefrompng('images/bob.png'));
ob_end_flush();
?>
...but it doesn't seem to make any difference. Works like a charm on Dreamhost, but on my local machine, I get a "The image http://127.0.0.1:8000/Jiggzy%20Quick-St ... getest.php cannot be displayed, because it contains errors." message. Suggestions???


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

Code: Select all

http://127.0.0.1:8000/Jiggzy%20Quick-St ... getest.php
 
GET /Jiggzy%20Quick-Start%20Package/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: image/png
Content-Length: 158
Server: Jetty(6.1.11)
----------------------------------------------------------
http://127.0.0.1:8000/Jiggzy%20Quick-St ... getest.php
 
GET /Jiggzy%20Quick-Start%20Package/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: image/png
Content-Length: 158
Server: Jetty(6.1.11)
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 04:16:09 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/png

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 9:45 am
by pickle
It doesn't appear this is a problem with your script - assuming the code you posted is the entirety of what exists in your file (please use

Code: Select all

[[/size]/code] tags by the way).

I'd guess this is something to do with how your server is configured.  Unfortunately, I don't know how you would fix this.  Try looking in php.ini & see if there's a setting regarding automatically sending headers or something to that affect.

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 10:07 am
by mikemike
Do http headers read %20 as a space or as '%20', that's the only difference I see between each header?

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 12:32 pm
by nickcherryjiggz
Ah, I was afraid that was the case - that it's an issue with my server configuration, which I know little about. The code is exactly what the PHP consists of, and I've tried about a hundred variations/approaches, and it always works on DreamHost's server, but never my own.

mikemike, I tried changing the file path and URL so that no spaces would be involved, but I still have the same problem. *Sigh*.

Re: header('Content-Type: image/jpeg'); not behaving as I ho

Posted: Thu Jun 11, 2009 1:38 pm
by McInfo
Try adding this to your script.

Code: Select all

header('Transfer-Encoding: chunked');
Edit: This post was recovered from search engine cache.

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 2:16 pm
by nickcherryjiggz
A good thought! Unfortunately, still no go. I've tried with and without the ob_start()/ob_end_flush(), putting the 'Transfer-Encoding' above and below the 'Content-Type', using get_file_contents('images/bob.png'), which simply yields the absolute URL of the PHP field, and imagepng(imagecreatefrompng('images/bob.png')), which tells me that the image cannot be displayed because it has errors (all of my images have errors, according to Localhost, yet DH gets along just fine ).

Code: Select all

<?php
ob_start();
header('Transfer-Encoding: chunked');
header('Content-Type: image/png');
file_get_contents('images/bob.png');
ob_end_flush();
?>

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 2:34 pm
by pickle
output nothing and check the headers, then output what you want & check the headers. Post both results from both Dreamhost and your localhost.

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 3:19 pm
by mikemike
Sounds like it's almost definitely a server config issue

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 3:53 pm
by nickcherryjiggz
Here ye be! Thank you for the help again. I do appreciate it.

Code: Select all

<?php
header('Content-Type: image/png');
imagepng(imagecreatefrompng('images/bob.png'));
?>
DREAMHOST
Bob shows up just fine.

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 20:30:46 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/png
LOCALHOST
Prints the following text: The image “http://127.0.0.1:8000/jiggzyquick-start ... getest.php” cannot be displayed, because it contains errors.

Code: Select all

http://127.0.0.1:8000/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=ebbc9ae7c7d5f74ed206a67871b468e7
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: image/png
Content-Length: 158
Server: Jetty(6.1.11)
----------------------------------------------------------
http://127.0.0.1:8000/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=ebbc9ae7c7d5f74ed206a67871b468e7
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: image/png
Content-Length: 158
Server: Jetty(6.1.11)
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

Code: Select all

<?php
header('Content-Type: image/png');
?>
DREAMHOST
Prints the following text: http://www.jiggzy.com/jiggzyquick-start ... getest.php

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 20:36:32 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Content-Length: 0
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: image/png
LOCALHOST
Prints the following text: http://127.0.0.1:8000/jiggzyquick-start ... getest.php

Code: Select all

http://127.0.0.1:8000/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=ebbc9ae7c7d5f74ed206a67871b468e7
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: image/png
Content-Length: 0
Server: Jetty(6.1.11)
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

Code: Select all

<?php ?>
DREAMHOST
Outputs nothing.

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 20:45:57 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/html
LOCALHOST
Outputs nothing.

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Thu, 11 Jun 2009 20:45:57 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/html

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 4:12 pm
by pickle
It looks like when you try to output the image/png header, that your localhost actually sends to sets of complete headers. Or am I reading this wrong?

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 6:01 pm
by nickcherryjiggz
Yes, pickle, I believe you are correct! From what I understand about ob_start(), though (not much), I think all the preceding header information should be cleared?

Code: Select all

<?php
ob_start();
header('Content-Type: image/png');
imagepng(imagecreatefrompng('images/bob.png'));
ob_end_flush();
?>

Re: header('Content-Type: image/jpeg'); not behaving as I ho

Posted: Thu Jun 11, 2009 7:06 pm
by McInfo
nickcherryjiggz wrote:DREAMHOST
Outputs nothing.

Date: Thu, 11 Jun 2009 20:45:57 GMT

LOCALHOST
Outputs nothing.

Date: Thu, 11 Jun 2009 20:45:57 GMT
Are you sure you copied those correctly?

Edit: This post was recovered from search engine cache.

Re: header('Content-Type: image/jpeg'); not behaving as I hoped.

Posted: Thu Jun 11, 2009 7:59 pm
by nickcherryjiggz
Eek, you're right! Sorry about that. :?

DREAMHOST
Outputs nothing.

Code: Select all

http://www.jiggzy.com/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: http://www.jiggzy.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=daeqfu3b9h2dad6bp6e20mlp21
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
Date: Fri, 12 Jun 2009 00:56:25 GMT
Server: Apache/2.0.63 (Unix) PHP/4.4.7 mod_ssl/2.0.63 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
X-Powered-By: PHP/5.2.6
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/html

LOCALHOST
Outputs nothing.

Code: Select all

http://127.0.0.1:8000/jiggzyquick-start ... getest.php
 
GET /jiggzyquick-startpackage/imagetest.php HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=ebbc9ae7c7d5f74ed206a67871b468e7
Cache-Control: max-age=0
 
HTTP/1.x 200 OK
X-Powered-By: PHP/5.2.5
Content-Type: text/html
Content-Length: 0
Server: Jetty(6.1.11)