@Martin I thought that was the issue, but I don't want my entire page to be a PNG type, just the image that is generated. I'm not sure how to isolate the type to just the image.
This IS the issue, you're telling the browser that your page (whatever.php) is a HTML file and then you're telling it that it's a PNG file. This is obviously incorrect as a file can not provide headers to be multiple types at the same time.
What is happening is this:
- Server sends HTML data with headers stating something link
Content-type: text/html
- Server sends [static] HTML contents (which you display).
- Server then sends the output of PHP
process()
function which is:
- Header for PNG stating
Content-type: image/png
- Data contents for PNG file.
Can you see how this causes a clear issue that the browser is loading one output rather than another.
Solutions
1) Just output the PNG, this will involve not sending HTML headers or HTML content (credit to User2342558).
<?php
require_once('functions.php');
if (isset($_POST['submit'])) {
process();
exit;
}
<html>
...
Note the exit;
which forces PHP to stop executing the page once the process
function is done, so that once the PNG is output then no further output is provided to the browser. This means the browser will only see one file type contents and will not present an error.
but I don't want my entire page to be a PNG type,
You state you don't want the page to be just a PNG image, so another solution is here:
- 2) Load the PNG as a seperate
<img>
file
pngfile.php
require_once 'functions.php'; // Requires and includes do not need brackets.
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
whatever.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
?>
<html>
<body>
<form action="/" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
</body>
</html>
This will then reload the current page and pass the input form fields as URL values to the pngfile.php
script which will run as if it is a PNG file, and nothing else.
This means your form page will load both the form and the image at the same time. And as these two files both have separate and distinct headers (one is text/html
, the other is image/png
this will not cause a browser problem.
3) A third and probably more complex solution is to construct the image as an SVG
codeblock and display it inline in the HTML page, this negates the need to provide headers but there may be various secondary issues.
This would also need a fair bit of code adjustment for you current functions.
Finally
As ever with PHP, also keep an eye on your PHP error logs.
Process function Editing
You need to update the process function to accept and use the data value. your default process function is using script-global values ($_POST
) but will need to use the values specified in process($data)
part of your whatever.php
because the image file (pngfile.php
) will get nothing POSTed to it.
<?php
function process($newData){
$image->annotateImage($draw, 10, 45, 0, $newData); //updated
// To use the specified value passed into the function.
...
header('Content-type: image/png');
echo $image;
exit; // should be return not exit here.
}
?>