How to add graph to website? - php

I'm a beginner here and need help. I have this code, which works and outputs a graph in my browser (if this is the only code in php file). I don't know how to add text below or above just like any other site. When I try, it returns my whole code in the browser. How do I go on about this?
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
// Some data
$ydata = array(11,3,8,12,5,1,9,8,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
Thanks in advance!

What you need is quite simply:
<img src="graph.php">
Put that in a separate HTML file or PHP script. You cannot output the image and text in the same script / web page. It needs to be separated.
Don't worry about the .php extension for the image src= attribute. It will display despite the lack of .jpeg extension. (The Graph class already outputs the correct MIME type I assume.)

I'm a bit rusty on PHP, but I believe that all "require" are supposed to be made before any content is output. Otherwise, the normal HTML/XHTML syntax and formatting take precedence.

I haven't actually used this library, but I'm assuming the image is output directly to the browser. It's probably easiest to create a new HTML document (or whatever the rest of your site is powered by) and include this as an image, with the image's src being the name of this script.

Related

Read multiple image files using php readfile

I am trying to read multiple image files from a folder (.htaccess protected) and display in a HTML page using php readfile().
The problem is I can see only the first image is read and the next is not shown in the browser. The code is as below
<?php
$image1 = 'files/com_download\256\50\www\res\icon\android\icon-36-ldpi.png';
$image2 = 'files/com_download\256\50\www\res\icon\android\icon-48-mdpi.png';
$imginfo = getimagesize($image1);
header("Content-type: ".$imginfo['mime']);
readfile($image1);
$imginfo = getimagesize($image2);
header("Content-type: ".$imginfo['mime']);
readfile($image2);
?>
I could see the first image 'icon-36-ldpi.png' successfully read and displayed in the browser and the second image is not read and not displayed in the browser.
Am I missing something? Any advice please.
Sorry if I am doing stupid but the requirement is to read multiple image files and render in the browser like a grid view. I cannot use img tag because of security reasons.
You can't dump both images out at once. Why not make two images in your html so the browser makes two calls to your script. Then use a GET param to pass the filename you want to display.
---Edit---
Important Security Note
There is an attack vector which you open up when doing soething like this. Someone could easily view your source html and change the parameter to get your image script to output any file they want. They could even use "../../" to go up directories and search for well known files that exist. e.g. "../../../wp_config.php". Now the attacker has your wordpress database credentials. The correct way to prevent against this is to always validate the input parameter properly. For example, only output if the file name ends with ".jpg"

How to dynmically draw picture in php gd

Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}

Showing php page as an image

is that possible to show whole php page as an image file like png or jpg ?
for example,
<img src="details.php?id=44">
just like a screenshot of the page.
Thx
<?
// details.php file
// {
// do stuff to create your image and store in $my_image
// }
header("Content-type: image/jpg");
echo $my_image;
?>
1) output the mime type in the header.
2) echo your binary data
easy!
Not in the way you're thinking. You can use the PHP ImageMagick library or the PHP GD Library to generate an image dynamically and output it, but in order to get screenshots of a page you'll really need an outside service.
it depends on what you want to show. if its html/js/css, it varies across browsers.
If its unicode text to be displayed as images, you can use GD library for the purpose
I think you are talking about taking screenshots of a url or webpage. You can you freely available like:
http://www.shrinktheweb.com/
http://picoshot.com/
There are many such freely available services. If you want to host such service on your own server. You will need to create a software which can open your browser and take screenshot of the url. Ther are some softwares available as well. I hope it helps.

HTML2FPDF print page results as pdf

I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.

How to load dynamic image w/php gd library, w/o saving it on server or having src="script.php"?

I would like to generate a dynamic image from a script, and then have it load to the browser without being persistent on the server.
However, I cannot call this by setting the image's src="script.php", since that would require running the script that just generated the page and its data all over again, just to get the final data that will generate the graph.
Is there a way to do this that is similar to setting image's src="script.php", but which is called from within another script, and just sends the image without saving it? I need access to the data that is used in the generation of the markup, in order to create this dynamic image.
Or, if not, what is the easiest way to destroy the image once the page is loaded? a quick ajax call?
Is there any way to cache certain data for some limited time frame in order for it to be available to some other script?
Any ideas would be greatly appreciated, as I'm having a really hard time finding the right solution to this...
Thanks!
You can inline the image into a <img> tag if you need to.
Like
<?php
$final_image_data; // Your image data, generated by GD
$base64_data = base64_encode($final_image_data);
echo "<img src=\"data:image/png;base64,{$base64_data}\" ... />";
?>
That should work on all modern browsers, and IE8. Doesn't work well with some email clients tho (Outlook, for one).
Also, another solution I found is to store the image in a session variable which is then called from a php script in the image tag. This would allow a user specific image to be served, and then removed from memory by the script... This also avoids messy img src="" tags...
Hopefully that is helpful to someone.
Use a rewrite rule.
RewriteRule ^magicimage.jpg$ /myscript.php
Then simply echo your image data from gd, instead of writing it to disk -- which is as simple as not providing a filename to the appropriate image*() function
myscript.php
<?php
$im = imagecreatetruecolor($w, $h);
//...do gd stuff...
header('Content-type: image/jpeg');
//this outputs the content directly to the browser
//without creating a temporary file or anything
imagejpeg($im);
And finally, utilize the above
display.php
<img src="magicimage.jpg">

Categories