We help IT Professionals succeed at work.

PHP Graph giberish

Exceter
Exceter asked
on
Medium Priority
271 Views
Last Modified: 2012-05-05
I've tried 2-3 different PHP graph classes from this MB thread,
http://www.phpbuilder.com/board/showthread.php?t=10302425

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?

Cheers!
Exceter
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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.
Joseph MelnickSenior Software Developer - Pharmacy Applications

Commented:
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.

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

Joseph Melnick
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.)
CERTIFIED EXPERT

Author

Commented:
Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.