Forum Moderators: coopster

Message Too Old, No Replies

How to's with GD image tools/functions

This script was workin!

         

HeadBut

3:07 am on Oct 4, 2005 (gmt 0)

10+ Year Member



How do you debug an image creation script, I get nothing in the browser. I've read a bunch of the webs "how to's" and haven't seen an example of this. I have a script that was working and I am trying to get it to load a gif as a background picture to put my text and numbers on it.
Any suggestions would be greatly appreciated!

<?php header("Content-type: image/gif"); // changed this to gif

$font = '/Library/Fonts/Chalkboard.ttf';
// This script worked as : imagecreate(110, 110);
$im = ImageCreateFromGIF("/TemplateButton.gif");

// Create some colors
// transparent may only work for gif?
//$colorTransparent = imagecolortransparent($im);
$background = imagecolorallocate($im, 255, 255, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 57, 140);
$MainButtonShadow = imagecolorallocate($im, 0, 0, 153);
$MainButtonLight = imagecolorallocate($im, 51, 51, 255);

// this is standard submmit string stuff, I know it works!
if(isset($_GET['TextNumber'])){$TextNumber = $_GET['TextNumber'];}
elseif(isset($_POST['TextNumber'])){$TextNumber = $_POST['TextNumber'];}
elseif(isset($_REQUEST['TextNumber'])){$TextNumber = $_REQUEST['TextNumber'];}
else{$TextNumber = "12";}
if(isset($_GET['AnyText'])){$AnyText = $_GET['AnyText'];}
elseif(isset($_POST['AnyText'])){$AnyText = $_POST['AnyText'];}
elseif(isset($_REQUEST['AnyText'])){$AnyText = $_REQUEST['AnyText'];}
else{$AnyText = "Table";}
$text = $TextNumber;

// Add some shadow to the text - this was workin
imagettftext($im, 20, 0, 11, 41, $grey, $font, $AnyText);

// Add the text - this was workin
imagettftext($im, 20, 0, 10, 40, $red, $font, $AnyText);

// Add some shadow to the Number - this was workin
imagettftext($im, 20, 0, 11, 81, $grey, $font, $TextNumber);

// Add the Number - this was workin
imagettftext($im, 20, 0, 10, 80, $black, $font, $TextNumber);

imagegif($im); // this was png and was workin
imagedestroy($im);
?>

whoisgregg

3:43 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To debug imagecreatefrom*, use one of the sample scripts on the php.net manual pages. For example, here's the PHP GD gif error capture [us2.php.net] script:

function LoadGif ($imgname)  
{
$im = @imagecreatefromgif ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im = imagecreatetruecolor (150, 30); /* Create a blank image */
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc = imagecolorallocate ($im, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}

I dropped your sample code onto my server, removed the leading "/" from the filename of the source GIF, made a 110x110 source gif and placed it in the same directory as the script, and the code worked perfectly. Are you sure that the path to the source GIF is correct? Using an absolute file reference like that might mean it's looking for "TemplateButton.gif" at the root of your hard drive as opposed to the DOCUMENT_ROOT of your website.

Also, out of curiosity, are you using the Entropy PHP build, the Server Logistics build, or your own?

HeadBut

4:04 am on Oct 4, 2005 (gmt 0)

10+ Year Member



I'm using a Macmini with the "off the shelf' system OSX.
I removed the header line and now I get error results.
The imagegif() is not present. Seems kinda wierd!

Thanks for your help!

whoisgregg

5:00 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP that ships with Mac OS X Server does *not* include GD image generation. I didn't think the normal Mac OS X had any GD support either, but if your script was working, it must. :)

You can learn alot about what GD support you have by looking at the output of <?php gd_info();?>

If your version doesn't support GIF, you'll need to either recompile PHP with newer versions of GD/PHP or use one of the easy installers that will "replace" the standard PHP with a more feature-packed version. My favorite is the Entropy PHP build [entropy.ch]. I recommend it and so does Apple [developer.apple.com]. (That Apple link also describes how to "do it yourself.")

There's also the "hasn't been updated for a year, but has some extra cool stuff" Server Logistics PHP build [serverlogistics.net] that I've used and like.

I've heard of, but never tried the relative newcomer MAMP PHP build [mamp.info].

HeadBut

5:50 pm on Oct 4, 2005 (gmt 0)

10+ Year Member



So far it only works with the PNG tools. I guess it is because the jpeg and gif image tools (GD) are not supported in this Apple os release.

whoisgregg

11:56 pm on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not that unusual for GIF support to be lacking. Even though the patent expired July (June?) of 2004, there are still a lot of distributions out there that don't include it. It's odd that JPEG support is missing though. :/