Generating images using imagejpeg, imagegif and imagepng is generating string not image

A common bug I ran into a million of times. Image generator for example for captcha stops working without even touching the code. The problem is that something outputs something before you send the header using:

header(“Content-type: image/jpeg”);

No? You aren’t? No. You are! You just don’t know about it. If you have a PHP file even with just a PHP code and you put the closing PHP tag “?>” there than you might have a white-space character right after it. Space, new line, whatever. And that’s externally what causing the problem. But if you are using a framework this might happen inside that framework and you don’t want to touch the framework code since you want be able to update it. A bit of googling and I found a solution. Just put:

while (@ob_end_clean());

right before the “header” function. That’s it.

Leave a Reply

Your email address will not be published. Required fields are marked *