Image doesn't display in browser or get ASCII garbage

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
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Image doesn't display in browser or get ASCII garbage

Post by dwoods »

I've been working off and on this issue for weeks and can't seem to crack it.

I've created code that generates an image to be dynamically displayed in the browser. It was originally created and test on a FreeBSD system and worked just great. I'm now trying to get the same code to run on a Linux box with no luck.

The working system is: FreeBSD 10.0-Stable, Apache 2.4.10, PHP 5.4.32, GD 2.1.0 Compatible
The Linux system is: Ubuntu 14.04 LTS, Apache 2.4.7, PHP 5.5.9, GD 2.1.1-dev

If I modify the code to save the image to disk, it saves properly and can be displayed from within the browser with no problems so I believe my code is good.

But when I try to have the code display the image directly I either get the broken image icon.

I've seen some suggestions on changing output_buffering and it does give different results, but still not the correct ones.

output_buffering was set to 4096 - this caused the broken image icon
Setting it to "0" as it is on the BSD box, causes ascii garbage to be displayed. Same as if I comment out the header line.
Setting it to "Off" causes the same
Setting it to "On" causes the broken image icon

I believe that my problem is either something in Apache or PHP, but have not been able to figure out what it is. Everything on the BSD box was compiled from the latest ports collection a week ago and everything works great. The Linux box, everything was installed from apt-get from whatever was available at the time - the reason for the the versions disparity.

I really need to get this issue resolved and appreciate all help or guidance. If there is additional info that will help solve the problem, tell me what you need and I'll get it.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image doesn't display in browser or get ASCII garbage

Post by requinix »

What if you go directly to the URL for the image? If you have a

Code: Select all

header("Content-Type: ...");
then comment that out first.
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Re: Image doesn't display in browser or get ASCII garbage

Post by dwoods »

I think what you're asking is that if I change:

Code: Select all

header ("Content-Type: image/png");
ImagePng($handle);
to

Code: Select all

// header ("Content-Type: image/png");
ImagePng($handle, $outputFile);
With $outputFile being a full path to a file.

That works just fine. I call the script from the browser, the graphic is generated and saved to the path/filename set by $outputFile. It is then possible to view that graphic by it's URL. PHP and GD are creating the images correctly and will save them to the disk if specified - its just trying to get the same image to display dynamically in the browser (without first saving it to the disk) that's the problem.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image doesn't display in browser or get ASCII garbage

Post by requinix »

Right... I meant to edit that script. The one that displays it dynamically, not the one that saves to disk.
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Re: Image doesn't display in browser or get ASCII garbage

Post by dwoods »

Same script, I only have the one. If I make the changes as shown in my previous post, it saves a good image to the disk. If I uncomment the header line and take the outuputfile out of the ImagePng it gives the behavior described in my first post.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image doesn't display in browser or get ASCII garbage

Post by requinix »

Take the outputfile out of the imagepng, leave the header() in actually, and see what it outputs when you go there directly.
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Re: Image doesn't display in browser or get ASCII garbage

Post by dwoods »

I must be thick and just not understanding because I could swear that I've described what happens already.

Ok, if I have in the code:

Code: Select all

header ("Content-Type: image/png");
ImagePng($handle);
Then in Safari I get the broken image / missing image icon. If I try the same URL from Firefox it tells me that "the image cannot be displayed because it contains errors"

If the above is not what you're asking me to do, then I'm sorry, I'm just not understanding.

Somewhere else (another board I think) the suggestion was to write the file to disk from the code and open in text editor to see if there were any errors written in the data. I've done that also, and there are not.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image doesn't display in browser or get ASCII garbage

Post by requinix »

I'll explain what I'm thinking.

The image written to the file should be correct. The problem is that what you're outputting from the script directly (not to the file) because apparently it contains some non-image data. That will be coming from the script/PHP itself. The most likely problem was that there was some output before the image; browsing to the image directly you would get whatever that output was (maybe whitespace), a warning from PHP that it could not send the header, then the raw image data.

However Firefox recognizes the image as, well, an image, which it should only do if the header() worked, which would only happen if there wasn't output before that point in the code. So if there's no output before the image then maybe there's output after, though knowing the structure of PNG data I would have expected Firefox to deal with it gracefully.

So my next request is that you do comment out the header() and try again. In fact you can even comment out the imagepng() call. This time I expect there to be some sort of output you weren't expecting - maybe a PHP error message, maybe some HTML that shouldn't be there, maybe something else. If it's not obvious looking at the page, do a View Source.

Another option that I should have mentioned earlier is that you post the full code to the script. That should be easy to scan through looking for something that shouldn't be in there.
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Re: Image doesn't display in browser or get ASCII garbage

Post by dwoods »

Gotcha. Ok, if I comment out the header and ImagePNG line I get no headers, no nothing. Source from both Safari and Firefox show 2 blank lines, nothing else.

If I comment out just the header, I get:

Code: Select all

‰PNG  IHDR&–ÝE³(3PLTEÈÿÈÈÈKKKÿÿÿÿõõõ   uuu```ŠŠŠßßßÊÊʵµµ...õÒÀÎ pHYsÄÄ•+IDATxœí‹’£ Eͬ©2FÍüÿ×®o›µƒæ±ì9c,¯- ­Ù€]²?ÀÈ 0€L¾Š,ËöãØãÿˆù!É7‘® V#…ÿñ@&I’Íó¬ûËŠq>X1ı cÜ>Ä·,?ãô3ÏÛYŒjÉ7áÈ$sâ[“1<(“AB*E”uÙ“ÉuÀœÞu£a½ë¹}ÅðÜáèȁ-Ÿ.Ÿ­ÈR&CÂÙ´Q–õ{ïç¢è²bëV5XŽA£@¢Œ‰A&Ë|e¥²l°Sx”ûž×I(öî%,#7ô7’ß)W&K²yÃQ&óº™ÌÖCØ‘WÜtž“Ét0¦³áÙøÏëeò\ºo–I‹mG1ÚŽlZ˜er ÈdµnòŽ›Î<¿Î"ž²©-¨RÇþm'´¹\%¯œ)òU.¬dãêEÖ»p{< ë·˜y_ÅÕ;*‘ÂTLîÞEÊbÿsˆX¸YV¸2iC„L²%çÙ\/)VdÒKãåUØ¥n2—éU,éȲ(ö¯\cùw±Jz¡pw¾r›’YX‘ÐrÚ—ˆ*¶›ŸYkGíYэóeŶÊúÓ3jÛ¬I!K@•žy¾”ÜUºž¸ž®ò¸ƒ%\fCe!¼ÚÙYXm~nƒ Ê@™_bª‚‡ì'› L–‚0ÄÖ¡XU‡ÖÔ;dâîÔ,“©4ü\ú2Ú–‰Wx¯e‘ÉmF®µ&&•Íla3"§Éd.ð5™„²áG[³&Á¥ÍÃÑÉ®\"’ɽ%ï~•ŸÙiI)@Ÿè”V ùœ¼ü©óãÄQÿ9 ¹iläpÝþM©Ï§e³n<‚ð…µœçÀáèÂé]§lL›¿ç¦S5í¬Y‘I¨8ƒM©û BçÆ­öeâÆYÌ»NÜIGíAïê÷ò3]ÓR ºbqsèÛÇ(èÃQ9Ý,7áÈ$sx/—Ëe?ÎJüÕ-_óè¯ìª%·ûñTáY.ÅÆÙ–‘Bñß,“¦­œä÷æxªð,—y~éþ.Å8¬Æ",ÈsX7„ûöèUOˆ›"ùŽL.b.B|k2Ú”1<$“—ïÄtpÚ‘É—¿‚ºIâ8ÖäÈëƒqçJúMÀ½°`™€düÎzª¡àá¾-©EQМÕYI㏬Ç9<´«yÁ§ ÀGÚàΧ À‡O€1²~棂oä¬'Đ4ô‚dðúxýn:`™€!“i¼3õ 2¹÷^6õ™€FȤ.«n.Yàá¾-©|D~7ÒÆ­ÂÞð´òMŽ-÷nöÉüÀW"«°ŸÌ|%ô›€d @&`™€d @&`™€d @&`™€d he°Ë_Û§ØNëŸ=IEND®B`‚
There's nothing in there that appears to be anything other than graphic data (to me)

Keep in mind also, that this EXACT same code is running on a FreeBSD system without any problems at all. So (again, to me) if it were a coding issue it would have to be something really odd that got changed between point releases, defined in my first post.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image doesn't display in browser or get ASCII garbage

Post by requinix »

dwoods wrote:Source from both Safari and Firefox show 2 blank lines, nothing else.
Two? Should be just one...

Looks fine to me too. Can you save the image and upload it somewhere? Not sure if you can do an attachment here - that'd be easiest.
Do Firefox or Safari, or other browsers where it doesn't work, show any sorts of errors? Typically in the Javascript console.
What about the full response headers for the request to the script?
dwoods
Forum Newbie
Posts: 6
Joined: Tue Sep 09, 2014 12:25 pm
Location: Austin, Texas

Re: Image doesn't display in browser or get ASCII garbage

Post by dwoods »

I hope I'm getting you the info you're looking for. In Firefox with header and ImagePNG turned on (uncommented) I get this for the Inspector tab:

Code: Select all

<html>

    <head>
        <meta name="viewport" content="width=device-width; height=device-height;"></meta>
        <link rel="stylesheet" href="resource://gre/res/ImageDocument.css"></link>
        <link rel="stylesheet" href="resource://gre/res/TopLevelImageDocument.css"></link>
        <link rel="stylesheet" href="chrome://global/skin/media/TopLevelImageDocument.css"></link>
        <title>

            myStats.php (PNG Image)

        </title>
    </head>
    <body>
        <img src="http://dilinux/mystats/myStats.php?int=192.168.160.90_2&dev=myStatsDev.conf" alt="The image “http://dilinux/mystats/myStats.php?int=192.168.16…atsDev.conf” cannot be displayed because it contains errors."></img>
    </body>

</html>
There's nothing in the Console.

If I run the same script on the other server (with different data of course) I get the image and this is what the inspector shows:

Code: Select all

<html>

    <head>
        <meta name="viewport" content="width=device-width; height=device-height;"></meta>
        <link rel="stylesheet" href="resource://gre/res/ImageDocument.css"></link>
        <link rel="stylesheet" href="resource://gre/res/TopLevelImageDocument.css"></link>
        <link rel="stylesheet" href="chrome://global/skin/media/TopLevelImageDocument.css"></link>
        <title></title>
    </head>
    <body>
        <img class="decoded" src="http://testdb/myStats/myStats.php?int=xxx.xxx.xxx.20_1&nolog=true&dev=./myConfig.conf" alt="http://testdb/myStats/myStats.php?int=xxx.xxx.xxx.20_1&nolog=true&dev=./myConfig.conf"></img>
    </body>

</html>
Again, no errors in the console. This is the same script, just running on two different servers and therefore a different dataset. Since the server that won't display the data will write it out to disk (I believe) it can't be a dataset error -as that would cause errors in the script that should get caught somewhere (apache error log etc).

Here is the image that the script on the non-working server writes to the disk when I comment out header and add the output file to ImagePNG:
Image
Post Reply