imagejpeg() not sending output to the browser

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

Post Reply
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

imagejpeg() not sending output to the browser

Post 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
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: imagejpeg() not sending output to the browser

Post 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);
}
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

Re: imagejpeg() not sending output to the browser

Post 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
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: imagejpeg() not sending output to the browser

Post 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.
Post Reply