Page 1 of 1

imagejpeg() not sending output to the browser

Posted: Mon Mar 07, 2011 4:06 pm
by El Vasco
Hi All,
I am trying to generate and send a Captcha directely to the Browser using imagejpeg() in a php script but it is not working. If I send the image to a file it works fine !

I am using the following script:

imagecreate();
......
header('Content-Type: image/jpeg');
imagejpeg($image);

<img src="captcha.php?width=100&height=40&characters=5"/> // I call the php script using <img/> tag from a html form...

As I mentioned before if I use all the code exactly as it is but I send the image to a file using: imagejpeg($image, file); It Works perfectly...

Any hint will be highly appreciated !

Thanks !
El Vasco

Re: imagejpeg() not sending output to the browser

Posted: Mon Mar 07, 2011 10:26 pm
by greyhoundcode
Without seeing a more complete excerpt of your code it is difficult to say.

As a guess the problem could be that the headers have already been sent (this could happen if you have inadvertently "leaked" some whitespace or otherwise output some text). You could test for this as follows:

Code: Select all

if (headers_sent())
{
    exit('Wooops! Cannot send anymore headers.');
}
else
{
    header('Content-Type: image/jpeg');
    imagejpeg($image);
}

Re: imagejpeg() not sending output to the browser

Posted: Wed Mar 09, 2011 7:13 pm
by El Vasco
Hi greyhoundcode !
Thanks for your piece of advice !
Now it is working but unfortunately I did more than one change at the same time and now I cannot know exactly what was the problem :-(....
Anyway, among the changes I did I found I had a double << sign in the php tag : I had <<?php instead of <?php so I believe that was the mistake...
Thanks for your help !
El Vasco

Re: imagejpeg() not sending output to the browser

Posted: Wed Mar 09, 2011 10:19 pm
by greyhoundcode
Happy to help. :P

Certainly if you opened with <<?php then the headers would already have been sent, they cannot of course be sent more than once.