By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

imagejpeg generating string not image even with header("Content-type:image/jpeg")

P: n/a
does anyone know why i can't generate images with:
header("Content-type:image/jpeg");
imagejpeg($img_number);

i've tried different examples but i always get a text output as if the
header doesn't make a difference at all.

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type:image/jpeg");
imagejpeg($img_number);
?>

Mar 2 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
You're code runs fine on my server. Are you outputting some other
content before this script?

Mar 2 '06 #2

P: n/a
cman wrote:
does anyone know why i can't generate images with:
header("Content-type:image/jpeg");
imagejpeg($img_number);

i've tried different examples but i always get a text output as if the
header doesn't make a difference at all.

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type:image/jpeg");
imagejpeg($img_number);
?>


You must be testing the code using IE, which has a stupid mime-type
caching mechanism. If your script had initially produced an error
message, IE will keep seeing the contents as text.

Try testing the script again.

Mar 2 '06 #3

P: n/a
cman wrote:
header("Content-type:image/jpeg");


The standard requires a space after the colon, but the mimetype caching
issue is the better bet I think.

--
E. Dronkert
Mar 2 '06 #4

P: n/a
no i'm not outputing other content before it, i tried running only that
script to be sure

Mar 2 '06 #5

P: n/a
i'm actually testing the code using firefox

Mar 2 '06 #6

P: n/a
i tried with the space after the colon and it doesn't make a difference
either

Mar 2 '06 #7

P: n/a
cman wrote:
i tried with the space after the colon and it doesn't make a difference
either


while (@ob_end_clean());

place this before your header function

if you server has output buffering on, then it won't send the image
data correctly.

At least, that is the problem I had for my generated images...

- JS
http://www.endeavorpub.com

Mar 2 '06 #8

P: n/a
i was hoping that would work, but sadly it didn't
i still see "" in the browser

Mar 2 '06 #9

P: n/a
cman wrote:
i tried with the space after the colon and it doesn't make a difference
either


while (@ob_end_clean());

try adding this before the header. If your server has output buffering
on, you need this to generate image.

Hope this helps.

- JS
http://www.endeavorpub.com

Mar 2 '06 #10

P: n/a
cman wrote:
does anyone know why i can't generate images with:
header("Content-type:image/jpeg");
imagejpeg($img_number);

i've tried different examples but i always get a text output as if the
header doesn't make a difference at all.

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type:image/jpeg");
imagejpeg($img_number);
?>


Are you including this file in another possibly? Or do you have
anything else in the file before this code (even a DOCTYPE)?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 2 '06 #11

P: n/a
i'm only running the following code in a file name 'test.php':

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type: image/jpeg");
imagejpeg($img_number);
?>

Mar 2 '06 #12

P: n/a

cman wrote:
i'm only running the following code in a file name 'test.php':

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type: image/jpeg");
imagejpeg($img_number);
?>


Did you try the

while (@ob_end_clean());

as the first call in your document?

- JS
http://www.endeavorpub.com

Mar 3 '06 #13

P: n/a
yes i tryed 'while (@ob_end_clean());' before header and tried it as
the first command also

Mar 3 '06 #14

P: n/a
cman wrote:
i'm only running the following code in a file name 'test.php':

<?php
//random_number.php
$img_number = imagecreate(100,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
$grey_shade = imagecolorallocate($img_number,204,204,204);

imagefill($img_number,0,0,$grey_shade);
ImageRectangle($img_number,5,5,94,44,$black);
ImageRectangle($img_number,0,0,99,49,$black);

header("Content-type: image/jpeg");
imagejpeg($img_number);
?>


OK, then, are you sure you have the correct libs compiled into PHP?

It works for me, also (on Firefox).

Live HTTP headers is showing a response of :

HTTP/1.x 200 OK
Date: Fri, 03 Mar 2006 03:03:33 GMT
Server: Apache/2.0.50 (Unix) DAV/2 PHP/5.0.4 mod_ssl/2.0.50 OpenSSL/0.9.7e
X-Powered-By: PHP/5.0.4
Content-Length: 1668
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: image/jpeg

Also, when I view the source, the first line I get is:

JFIF>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality
Try reporting all errors to the display and see of you get a message
about something wrong.
Do you have a URL we can try?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '06 #15

P: n/a
you can access it at:
www.marketopp.com/test.php

the thing is i don't get any errors the source only contains ''

Mar 3 '06 #16

P: n/a
cman wrote:
you can access it at:
www.marketopp.com/test.php

the thing is i don't get any errors the source only contains ''


OK, here's the header I get from your page:
HTTP/1.x 200 OK
Date: Fri, 03 Mar 2006 03:36:23 GMT
Server: Apache
X-Powered-By: ModLayout/3.2.1
Cache-Control: no-cache
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

Note the content-type.

Either you have something causing the headers to be sent prematurely
(i.e. even a blank line or a space before the first '<') or you're
getting an error somewhere and not telling us.

What does phpinfo() show about error reporting?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '06 #17

P: n/a
could the problem be on the server side? suppressing the header
function call?

here's the some of the content from phpinfo: www.marketopp.com/test2.php

Mar 3 '06 #18

P: n/a
cman wrote:
could the problem be on the server side? suppressing the header
function call?

here's the some of the content from phpinfo: www.marketopp.com/test2.php


This is something on your server.

Either the server is set up incorrectly or there's something about your
script we don't know about.

You don't have E_NOTICE enabled in your errors; when I'm trying to find
a bug like this I always use E_ALL. It sometimes finds problems I don't
know about.

However, it's hard to tell much from this excerpt of phpinfo. It
doesn't say anything about extensions, for instance.

But this is also "sensitive" information - generally not something you
want others to be able to access. YOU need to look at it and understand
what it's telling you.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '06 #19

P: n/a
cman wrote:
could the problem be on the server side? suppressing the header
function call?

here's the some of the content from phpinfo: www.marketopp.com/test2.php


And BTW - the problem has nothing to do with suppressing the header
info. As I said earlier - your exact script works fine on my server.
It fails on yours.

Either theres something missing (i.e. gdi libs) and your error messages
are being suppressed, or there's something else about your script we
don't know about.

I'm not accusing you of trying to deceive us - it's just that when the
same script works one place but not another, there's SOMETHING
different. And PHP is generally quite good at telling you when
something doesn't work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 3 '06 #20

P: n/a
Jerry Stuckle wrote:
cman wrote:
you can access it at:
www.marketopp.com/test.php

the thing is i don't get any errors the source only contains ''

OK, here's the header I get from your page:
HTTP/1.x 200 OK
Date: Fri, 03 Mar 2006 03:36:23 GMT
Server: Apache
X-Powered-By: ModLayout/3.2.1
Cache-Control: no-cache
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

Note the content-type.

Either you have something causing the headers to be sent prematurely
(i.e. even a blank line or a space before the first '<') or you're
getting an error somewhere and not telling us.

What does phpinfo() show about error reporting?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


X-Powered-By: ModLayout/3.2.1
Could it possibly be this:
http://tangent.org/index.pl?lastnode_id=41&node_id=449
It sounds like ModLayout may be sending premature headers. Can you
disable this in your apache config and test?
Copied from a forum on this page : http://gallery.menalto.com/node/21768

FAQ entry: Apache ModLayout

If you use mod_layout with Apache, make sure it is disabled for gallery
files. Gallery sometimes sends binary data back from a php file, so
adding a header or footer to these requests will corrupt the data!

After adding the following lines to your apache config, it should work:

----------------------------------------------------
# Disable layout header and footer for Gallery2
LayoutIgnoreHeaderURI /path/to/gallery2/*.*
LayoutIgnoreFooterURI /path/to/gallery2/*.*

# PHP Gallery2 overide Layout
<Directory /path/to/gallery2/>
LayoutHeaderOff
LayoutFooterOff
</Directory>
- JS
http://www.endeavorpub.com/

Mar 3 '06 #21

P: n/a
PROBLEM SOLVED!

Thanks guys for all your help to get to the bottom of it.

The problem was in fact because of APACHE MODLAYOUT which corrupted the
outputed image by redisplaying the page as text.

cheers!
-ca

Mar 4 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.