function OK on web, not on shell
Posted: Fri Mar 11, 2005 12:47 am
I am trying to pick up PHP after an abortive attempt to learn it years ago. I have O'Reilly's Programming PHP, and I've just been following along with the examples.
I am running PHP 4.3.10 on Apache (Mac OS X), with GD 2.0.28 enabled.
I followed the example for using GD to draw text over a PNG file and return a new PNG file. After some permissions and font-file-location issues, I got it to work as intended. So I know that the GD is installed and enabled properly.
Here is the code that works correctly:
As I said, this file (png.php) executes correctly when accessed through Safari.
The next step in the book is to rewrite this code as an executable shell script. Here is the code as it is written in the book:
And here is the code as I changed it in order to get it at least execute partially:
The three changes that I made were as follows:
line 1 - changed path:
#!/usr/bin/php
The path as listed in the book caused an error, as my PHP is located on a different path.
line 1 - remove "-q" flag
The book says that this flag will inhibit HTTP headers, but the program would not run with this flag set.
line 7 - changed variable name
list(, $filename, $message) = $argc;
The book has a variable $argc and then a variable $argv later. I assumed that this was a typo, as $argv was never called again in the program.
So, other than the changes to the program as it appears in the book, my version doesn't deviate (I had a second pair of eyes check it). But, even though the Web version works, when I run the shell version, I get the error:
Fatal error: Call to undefined function: imagecreatefrompng()
Now, I've searched on this error, and it seems that in most cases it's caused by not having GD installed or enabled. But my info() tells me that it is enabled, and clearly it has to be working, or it would not work for the first bit of code above.
The only difference that I can see between the Web and shell version, is that I explicitly state a path to PHP at the beginning of the script version.
I can't conjecture any further, because I am relatively new to PHP and Apache.
If this rings a bell with anyone, I'd appreciate input.
Thanks
feyd | Please use
I am running PHP 4.3.10 on Apache (Mac OS X), with GD 2.0.28 enabled.
I followed the example for using GD to draw text over a PNG file and return a new PNG file. After some permissions and font-file-location issues, I got it to work as intended. So I know that the GD is installed and enabled properly.
Here is the code that works correctly:
Code: Select all
<?php
if (isset($_GET['message'])) {
// load font and image, calculate width of text
$font = 'arial.ttf';
$size = 10;
$im = imagecreatefrompng('button.png');
$tsize = imagettfbbox($size,0,$font,$_GET['message']);
// center
$dx = abs($tsize[2] - $tsize[0]);
$dy = abs($tsize[5] - $tsize[3]);
$x = ( imagesx($im) - $dx ) / 2;
$y = ( imagesy($im) - $dy ) / 2 + ($dy + 2);
// draw text
$black = imagecolorallocate($im,0,0,0);
imagettftext($im, $size, 0, $x, $y, $black, $font, $_GET['message']);
// return image
header('Content-type: image/png');
imagepng($im);
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Button Form</title>
</head>
<body>
<form action="<?= $PHP_SELF ?>" method="get">
Enter message to appear on button:
<input type="text" name="message" /><br />
<input type="submit" value="Create Button" />
</form>
</body>
</html>The next step in the book is to rewrite this code as an executable shell script. Here is the code as it is written in the book:
Code: Select all
#!/usr/local/bin/php -q
<?php
if ($argc != 3) {
die("usage: button-cli filename message\n");
}
list(, $filename, $message) = $argv;
$font = 'arial.ttf';
$size = 10;
$im = ImageCreateFromPNG('button.png');
$tsize = ImageTTFBBox($size, 0, $font, $message);
$dx = abs($tsize[2] - $tsize[0]);
$dy = abs($tsize[5] - $tsize[3]);
$x = ( imagesx($im) - $dx ) / 2;
$y = ( imagesy($im) - $dy ) / 2 + $dy;
$black = ImageColorAllocate($im, 0, 0, 0);
ImageTTFText($im, $size, 0, $x, $y, $black, $font, $message);
ImagePNG($im, $filename);
?>Code: Select all
#!/usr/bin/php
<?php
if ($argc != 3) {
die("usage: button-cli filename message\n");
}
list(, $filename, $message) = $argc;
$font = 'arial.ttf';
$size = 10;
$im = ImageCreateFromPNG('button.png');
$tsize = ImageTTFBBox($size, 0, $font, $message);
$dx = abs($tsize[2] - $tsize[0]);
$dy = abs($tsize[5] - $tsize[3]);
$x = ( imagesx($im) - $dx ) / 2;
$y = ( imagesy($im) - $dy ) / 2 + $dy;
$black = ImageColorAllocate($im, 0, 0, 0);
ImageTTFText($im, $size, 0, $x, $y, $black, $font, $message);
ImagePNG($im, $filename);
?>line 1 - changed path:
#!/usr/bin/php
The path as listed in the book caused an error, as my PHP is located on a different path.
line 1 - remove "-q" flag
The book says that this flag will inhibit HTTP headers, but the program would not run with this flag set.
line 7 - changed variable name
list(, $filename, $message) = $argc;
The book has a variable $argc and then a variable $argv later. I assumed that this was a typo, as $argv was never called again in the program.
So, other than the changes to the program as it appears in the book, my version doesn't deviate (I had a second pair of eyes check it). But, even though the Web version works, when I run the shell version, I get the error:
Fatal error: Call to undefined function: imagecreatefrompng()
Now, I've searched on this error, and it seems that in most cases it's caused by not having GD installed or enabled. But my info() tells me that it is enabled, and clearly it has to be working, or it would not work for the first bit of code above.
The only difference that I can see between the Web and shell version, is that I explicitly state a path to PHP at the beginning of the script version.
I can't conjecture any further, because I am relatively new to PHP and Apache.
If this rings a bell with anyone, I'd appreciate input.
Thanks
feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]