PHP Graph giberish

Posted on 2006-11-07
Last Modified: 2012-05-05
I've tried 2-3 different PHP graph classes from this MB thread,

The examples work perfectly if I run them at the very top of my source files but then, only the graph is displayed. It's as if page execution stops after the graph is displayed. When I try to insert the graph into a page I get giberish. They all result in something similar to this,

‰PNG  IHDR&JÛ>‰ÉPLTEÿÿÿHkHkÿÿªªªÿÿÿæü´àœIDATxœí]r£8F™½OOVЙô¤ŠuèE+p•÷¿„Aâ»2„€~¬ï$Ýq0âX÷ K€ºŽBi¥í·Ç ÿûçŒüšwgØo£W¿ÚÇfú×5ëÄ€KÓN´¾çÝ·Nlȸ+L{b'Aì(½J7°çÄ4A®ÝyåÄ´<»Nò-ÎÉð…NÌ”OÚÓ2³½râ¢E -´ý×MuCè6cä%ªÕÊ@!„B!„T„íÓÉ]†ÂP×f§Æ¥XQ6(íœüj”ˆ”©žüõÕ è,9–Nf涘N:¡ t‚Ð B' t‚Ð B' t‚Ð B' t‚Ð ëgSãÙÿtâqÔì£+û¨7h÷í€>r19²:¬'A5¡¨&c:¡“ÅÉÐÓI :¡ t‚Ð B' t‚Ð B' t‚Ð B' t‚Ð =·œc!vÌKÑ Xáu[´â˜—<¬'›jÒñ¼¨&۝­[Iè$B~'ýãë™x“Å;yôý×#í&‹wòÕÑÉ–‡µ’”❤¯&³%ßå8¿“ç³fŠÈŸó;É€w¹Û Ð‰ƒ±ƒÐ R|[œ¥-3JV'ÿ}†¤Únɱ“ÛIä†ltB'Æ"çØL÷Sz†Ÿ÷r; °Ýö&½“Þu -äw䥲\«R´“1vRyõ_9Q:̱™®óz”ì$Ïu^¶ó¨¤Ÿ¨®ˆkßr; ò‰2¦„±ÑüNDwÂÏÅž%ŸðØÞÃÏ€Èä$6‹e2'›Ýÿ×.Ëï$o>)ÐIö±ŒDhÚIdJö¦0Ÿ,,ù„N”ß~cÜ„ãáÂt c§•¾íoî‘îÜ#­ÜÂÎ/|G¼“áuâ>‘¹š0ü°Ï-?üscìø…oÉtÀèj€¶û=§ £ìÂq]©5'v''“WA:_‘šr2ÅÎ&>:ŒÑ‰Í)nœØ½õÑ"æX·`TcÜöÆ×™Ö¡„N!„BH„ÿÐ'Šô=º)ÎIEND®B`‚

This looks to me as if the raw file is being displayed as opposed to being interpreted. The question is, why?

Question by:Exceter
  • 3
LVL 19

Accepted Solution

VoteyDisciple earned 500 total points
ID: 17893498
Keep in mind that any graphing class like that is going to produce an image.  That means the correct way to insert it into a page is:

<img src="your-graphing-script-name.php" alt="Graph" />

And then put JUST the graph code into your-graphing-script-name.php

LVL 19

Expert Comment

ID: 17893507
To get a feel for why, what you've effectively done is opened up an image file in Notepad, and copied & pasted the contents of that file into your HTML document.  Of course that's not how you'd really insert an image on a page; you'd use an <img /> tag.  The fact that this particular image happens to be generated by a script changes nothing.
LVL 11

Expert Comment

by:Joseph Melnick
ID: 17894005
Hello  Exceter,

The problem with your graphing function is that the browser does not know what kind of content you intend to send it so it just output the binary. Your function needs to output the appropriate header to identify the content.

// no space above php or this may still fail.
header('Content-Type: image/png');
echo $png_data_here;

Joseph Melnick
LVL 19

Expert Comment

ID: 17894103
Check the documentation for the graphing class you're using though -- usually you don't have to output any headers of your own; just calling the draw() or stroke() method (or whatever it is that makes the graph draw) will deal with the Content-Type header on its own.  I doubt that's something you have to worry about, especially since when you view the graph on its own it seems to be displayed right.  (That's no guarantee, though, since browsers do sometimes try to be smarter than the headers they receive.  Check the documentation to make sure.)

Author Comment

ID: 17898593

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question