1

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
8
  • 2
    If you comment out the 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 adding error_reporting(E_ALL); ini_set('display_errors', 1); to the beginning of the file to see the errors. Commented Oct 11, 2015 at 20:47
  • @drew010 exactly what I was thinking, Commented Oct 11, 2015 at 20:47
  • 1
    Never use closing ?> in non-template PHP files, especially for graphic ones! Commented Oct 11, 2015 at 20:50
  • 1
    Thank you for sharing your gained knowledge! To improve the accessability of your answer, you could answer your own question instead of adding [solved] to the title. This way it will be more visible for other users that you have found a solution. Commented Oct 12, 2015 at 19:59
  • 1
    @T3H40, fair enough. Done. Commented Oct 12, 2015 at 20:07

1 Answer 1

1

After running the debugging suggestions, I realised that the function imagecreatefrompng() failed to open the stream. Which lead me to realise the obvious mistake, which is that the function was intended to add some text on an already existing PNG file. Which was indicated on the source (if I had taken the time to actually read everything) as

this "text" string and overlays it on top of a base image which in this case is "images/button1.png" and outputs the resulting image

So I created an images/button1.png (actually from the web), re-enabled the header() and this time it worked.

Thanks for the comments, they helped me learn about some debugging tools for PHP.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.