473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
21 21213
You're code runs fine on my server. Are you outputting some other
content before this script?

Mar 2 '06 #2
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
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
no i'm not outputing other content before it, i tried running only that
script to be sure

Mar 2 '06 #5
i'm actually testing the code using firefox

Mar 2 '06 #6
i tried with the space after the colon and it doesn't make a difference
either

Mar 2 '06 #7
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
i was hoping that would work, but sadly it didn't
i still see "ÿØÿà" in the browser

Mar 2 '06 #9
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
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
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

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
yes i tryed 'while (@ob_end_clean());' before header and tried it as
the first command also

Mar 3 '06 #14
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: no one | last post by:
I have been searching various boards trying to figure out how to generate an image along with other html. I intend to put the image in a table, but for now I am just having problem getting the...
1
by: Torben Hoffmann | last post by:
Hi, I have searched high and low for an answer to this, but I have not been able to find it. I have found tools (such as Batik) to generate a gif|jpeg|png from a SVG file, but I would also...
0
by: Mark | last post by:
Hi, I am trying to create a "print preview" window for a web site using asp.net. I have got a long way down the road to the solution but the problem I have now is that the text and lines on the...
7
by: Mark | last post by:
Hi, I am trying to create a "print preview" window for a web site using asp.net. I have got a long way down the road to the solution but the problem I have now is that the text and lines on the...
1
by: Thomas Bauer | last post by:
Hi folks, I want to write an aspx page that doesn't output text/html, but an image (such as image/jpeg). What type do I have to choose in MSVS.NET and what are the basic steps? Is there any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.