function OK on web, not on shell

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
textheavy
Forum Newbie
Posts: 6
Joined: Fri Mar 11, 2005 12:16 am

function OK on web, not on shell

Post by textheavy »

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:

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>
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:

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);
?>
And here is the code as I changed it in order to get it at least execute partially:

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);
?>
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

Code: Select all

and

Code: 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when run under the CLI, if you don't provide path information for the php.ini file, the defaults are used. Which means GD is not loaded as a module.
textheavy
Forum Newbie
Posts: 6
Joined: Fri Mar 11, 2005 12:16 am

Post by textheavy »

OK, yes. I thought that it had something to do with the script not knowing where GD was.

But, as I said, I'm not very experienced with any of this yet.
Where would I provide the path information?

Do I add a path to the php.ini file in line one of the program? And if so, what symbol is used to seperate the current path from the path I'm adding.

I appreciate your help with this, and I can probably figure it out from here, but hey, if you could give me one last push I'd get swinging sooner.

Thanks again.
textheavy
Forum Newbie
Posts: 6
Joined: Fri Mar 11, 2005 12:16 am

Post by textheavy »

Yeah, I'm still spinning my wheels here. I understand WHY the script doesn't work now (because the GD module isn't being loaded), but I don't understand HOW to make the script work.

I tried adding the path to the php.ini file into line 1:

#!/usr/bin/php /usr/local/php/lib/php.ini

But that didn't work. Maybe there needs to be a comma separating the two paths? Maybe that's not how it's supposed to work at all.

During my search for more information I came across someone's phpinfo() that had the line: '--enable-cli' in it. But to be honest, I don't know what the "--enable-thing" means or how/where to change it. Is that in the php.ini file? Is that something I type into the command line and then recompile the php? I don't even know what "recompile the php" means, to be honest.

What I'm getting at is, I would really appreciate it if someone could just post a line of code that I should insert into this script, or recommend some change to the php.ini file that will cause the GD module to be loaded when I run a CLI script/program.

I'm trying to learn here, but I'm up against a wall, and I could use a hand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php --help wrote:

Code: Select all

Usage: php &#1111;-q] &#1111;-h] &#1111;-s] &#1111;-v] &#1111;-i] &#1111;-f <file>]
       php <file> &#1111;args...]
  -a               Run interactively
  -C               Do not chdir to the script's directory
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo&#1111;=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
Post Reply