I am an absolute beginner as far as PHP is concerned, but I was trying to work with PHP GD/image display to make some graphs.
I tried to include <?php echo "<p>Hello, Word!</p>" ?> in an HTML file, and that worked without problem. Next I tried the example from here and copied into a button.php,
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
Then I made a test_button.html as
<html>
<head>
<title>Testing button.php</title>
</head>
<body>
<p>It should be between here...</p>
<p align="center">
<img src="button.php?text=text">
</p>
<p> ... and here</p>
</body>
</html>
And when I opened the file with my browser, I get a broken image symbol. If I try to get on the php file, I have the message
The image http://.../button.php?text=text cannot be displayed because it contains errors.
I tried it on a raspberry pi with php5, apache, etc installed. On a server with LAMP. Or even on my laptop. All run Debian.
On raspberry pi, I tried phpinfo() and get that GD is enabled. Libpng as well.
I have looked around here or on other website, but none of the similar errors relate to me. In the PHP file, I made there that there were no white space/blank line before the <?php and neither after the ?>.
I'm sure I am missing a basic step. But I can't figure out what's wrong. In another similar test, without the header() (commented out), I got a bunch of characters appearing.
As it happened before, I tried to check whether vim did not add some strange characters due to encodings, I tested and got
$ file -i button.php
button.php: text/x-php; charset=us-ascii
and opened with another editor. But no, nothing there again.
Can someone point me to the (possibly obvious) mistake I'm doing?
Edit from first comments, commenting out header(...), removing the trailing ?> and adding error_reporting(E_ALL); ini_set('display_errors', 1);
When I get to button.php?text=text, I get
Warning: imagecreatefrompng(images/button1.png): failed to open stream: No such file or directory in /var/www/therm/button.php on line 6
Warning: imagecolorallocate() expects parameter 1 to be resource, boolean given in /var/www/therm/button.php on line 7
Warning: imagesx() expects parameter 1 to be resource, boolean given in /var/www/therm/button.php on line 8
Warning: imagestring() expects parameter 1 to be resource, boolean given in /var/www/therm/button.php on line 9
Warning: imagepng() expects parameter 1 to be resource, boolean given in /var/www/therm/button.php on line 10
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in /var/www/therm/button.php on line 11
header('Content-type...');line on button.php and then visit it directly in your browser, it should show the error preventing the image from being displayed. If not, try addingerror_reporting(E_ALL); ini_set('display_errors', 1);to the beginning of the file to see the errors.?>in non-template PHP files, especially for graphic ones!