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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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!
Last edited by Benjamin on Thu Jun 11, 2009 7:26 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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
Last edited by Benjamin on Thu Jun 11, 2009 7:28 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

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

Post by mikemike »

Do http headers read %20 as a space or as '%20', that's the only difference I see between each header?
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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*.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post by McInfo »

Try adding this to your script.

Code: Select all

header('Transfer-Encoding: chunked');
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 10:39 pm, edited 1 time in total.
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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();
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

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

Post by mikemike »

Sounds like it's almost definitely a server config issue
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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
Last edited by Benjamin on Thu Jun 11, 2009 7:30 pm, edited 1 time in total.
Reason: Replaced brightly colored text with [code=text] tags.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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();
?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 10:42 pm, edited 1 time in total.
nickcherryjiggz
Forum Newbie
Posts: 9
Joined: Tue Jun 09, 2009 10:21 am

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

Post 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)
Last edited by Benjamin on Thu Jun 11, 2009 8:21 pm, edited 1 time in total.
Reason: Changed brightly colored text to [code=text]
Post Reply