Page 1 of 1

GD/FreeType/Config issue

Posted: Tue Dec 19, 2006 11:49 am
by WoodyNaDobhar
I'm writing a handy 'do everything' dynamic image prog, and I've got a wierd problem I was hoping to get a little help with...

When I test it on my local machine, everything works fine...

PHPv 5.2.0
GDversion bundled (2.0.28 compatible)
FreeType version 2.1.9

But when I push it up to the test server, things go a little wonky.

PHPv 4.3.9
GDversion bundled (2.0.28 compatible)
FreeType version unknown (it doesn't list it in phpinfo(), but it does list freetype support enabled).

This is only the beginning of the odd stuff ;)

this works fine: https://alamogordo.insideimi.com/godzil ... ?text=HOME
but this doesn't: https://alamogordo.insideimi.com/godzil ... lor=ffffff

As you can see, the text is reduced to square blocks.

I've ruled out the font, it does the same with arial.
I've figured out it's something to do with the background 'imagefill' stuff (it doesn't do that when I comment it out). The code relating to that follows:

Code: Select all

//convert hex to RGB
		$rgb = explode(':', wordwrap($bg, 2, ':', 1));
		$RR = hexdec($rgb[0]);
		$GG = hexdec($rgb[1]);
		$BB = hexdec($rgb[2]);
		//fill 'er up
		$bgcolor = imagecolorallocate($img, $RR, $GG, $BB);
		imagefill($img, 0, 0, $bgcolor);
The funky thing is, it doesn't do this in my local dev environment. I have to assume the testing server is to blame...thoughts?

Posted: Tue Dec 19, 2006 12:45 pm
by Kieran Huggins
turn on all your error reporting (E_ALL) and see what (if anything) the production server complains about.

Cheers,
Kieran

Posted: Tue Dec 19, 2006 1:15 pm
by WoodyNaDobhar
almost is...everything except E_NOTICE. Not getting any errors that I can tell.

Posted: Tue Dec 19, 2006 2:17 pm
by Kieran Huggins
E_NOTICE is what I was aiming for ;-) That's likely where the hints are hiding...

Cheers,
Kieran

Posted: Tue Dec 19, 2006 2:20 pm
by RobertGonzalez
Your links don't resolve for me.

Posted: Tue Dec 19, 2006 2:37 pm
by Kieran Huggins
me neither, btw

Posted: Wed Dec 20, 2006 9:11 am
by WoodyNaDobhar
I turned it on for a bit, and confirmed there's nothing in the E_NOTIFY...there were a couple 'hey, that var is undefined' things, but I cleaned them up. The problem persists.

Sorry about the links...I've updated them (secure server stuff). If they still aren't showing, well, it doesn't really matter, blocked-out text is fairly self-explainatory ;)

Posted: Wed Dec 20, 2006 10:11 am
by Kieran Huggins
still don't see it :-(

Posted: Wed Dec 20, 2006 10:56 am
by John Cartwright
it might be helpful to repost the updated code

Posted: Wed Dec 20, 2006 2:54 pm
by WoodyNaDobhar
you asked for it ;) Note that if you want to run this you'll need to substitute a font. I can say with assurance it's not the font doing it, I've tested it with arial and got the same results. Also keep in mind I use a lang file not unlike those you find in phpBB, but you can substitute the need for it by changing the &text value to 'raw[sometext]';

I'm leaning heavily towards it being an environmental issue, specifically with freetype. There's a good chance I did something wrong when I set it up, as my local environment (WAMP5) had it bundled in there, while my test server did not. Anybody know any good tutorials on the subject?

Code: Select all

<?PHP

/** 

@package
@author Woody
@version 12/19/2006

To use simply put in a img tag in html.  Options and values are passed with $_GET.
example: <img src="images/dynImage.php?bg=000000&width=25&height=100&text=rawtest&size=24&txtcolor=ffffff">
vars:
bg - Default is flat color ffffff.  You have many choices for your background:
	- flat color: just enter a hex value, sans the '#'.  For transparent, use 'facade'.
	- bg image: enter the name of the file, make sure it's uploaded to the images directory.
	- complex bg image: if your bg image is made up of a series of images (when expanding would make the side images look goofy, for instance), you need a complex bg image.  You must create a php bg file for it and place it in the images folder.  In it, define the array $background, which has the files required for the bg, including extention but not file path.  Use the keys 'left', 'middle', and 'right'.  For vertically expanding images, the top image goes under the key left, and the bottom under the key right.  See existing $background files for examples
	- gradient: you can fill up the background with a gradient.  Use the following syntax: gradient|firstcolor|secondcolor|direction|step.
        - direction : string, shape of the gradient.
          Can be : vertical, horizontal, rectangle, square, ellipse, ellipse2, circle, circle2, diamond.
        - firstcolor : string, start color in hexadecimal.
        - secondcolor : string, end color in hexadecimal.
        - step : integer, optional, default to 0. Step that breaks the smooth blending effect.   
width - width of the final product.  If set to 0, the width will be dependant on the content.
height - height of the final product.  If set to 0, the height will be dependant on the content.
text - content.  Use the lang.php vars wherever possible.  When not possible to use the Lang file, just put the text after &text, prepending it with 'raw', substituting "_" for " ".  ex: &text=rawthis_is_an_example
layout - 'hor' or 'ver', the layout of the text.  Defaults to horizontal.
size - font size.  default 24.
txtcolor - color of content.  use hexidecimal values.  default black.
lmargin - left margin of text.  default 3.
rmargin - right margin of text.  default 3.
tmargin - top margin of text.  default 3.
tmargin - top margin of text.  default 3.
**/

// first we'll retrieve all our vars from $_GET and set defaults.

//set font.  maybe someday we'll add that as an option, but for now, this one does the trick.
$font = "blue_highway_regular";

if(empty($_GET['bg'])){
	$bg = "FFFFFF";
}else{
	$bg = $_GET['bg'];
};

if(empty($_GET['width'])){
	$width = "";//we'll be setting the defaults later, based off of text.
}else{
	$width = $_GET['width'];
};

if(empty($_GET['height'])){
	$height = "";//ditto
}else{
	$height = $_GET['height'];
};

if(empty($_GET['layout'])){
	$angle = "";
}elseif($_GET['layout'] == "ver"){
	$angle = "90";
}elseif($_GET['layout'] == "hor"){
	$angle = "0";
}

if(empty($_GET['size'])){
	$size = "24";
}else{
	$size = $_GET['size'];
}

if(empty($_GET['text'])){
	$text = "";
}elseif(strncasecmp($_GET['text'], 'raw', 3) == 0){
	$text = substr($_GET['text'], 3);
	$imgtext=str_replace("_", " ", $text);
}else{
	//for now we're just assuming they're using english language, but this is where you'll need to put in switches for language, when it's determined how that's decided.
	$text = $_GET['text'];
	$langfile = "english.php";
	require_once("../language/".$langfile);
	$imgtext = $lang[$text];
}

// Get exact dimensions of text string.
$box = imagettfbbox($size, $angle, $font, $imgtext);
$textwidth = abs($box[4] - $box[0]);
$textheight = abs($box[5] - $box[1]);

if(empty($_GET['txtcolor'])){
	$txtcolor = "000000";
}else{
	$txtcolor = $_GET['txtcolor'];
}

if(empty($_GET['lmargin'])){
	$lmargin = "3";
}else{
	$lmargin = $_GET['lmargin'];
}

if(empty($_GET['rmargin'])){
	$rmargin = "3";
}else{
	$rmargin = $_GET['rmargin'];
}

if(empty($_GET['tmargin'])){
	$tmargin = "3";
}else{
	$tmargin = $_GET['tmargin'];
}

if(empty($_GET['bmargin'])){
	$bmargin = "3";
}else{
	$bmargin = $_GET['bmargin'];
}

//if a dimention isn't set, it's based off of the text, so...
if($width  == ""){
	$width = $textwidth + $lmargin + $rmargin;
}
if($height == ""){
	$height = $textheight + $tmargin + $bmargin;
} 

//draw the basic image framework
$img = imagecreatetruecolor($width, $height);

//figure out what we're doing here.  I seperated this control structure from the guts for the sake of viewability.
//first, see if it's a gradient
if(strncasecmp($bg, 'gradient',  == 0){
	$willdo = 'gradient';
}else{
	switch(preg_match("/[0-9a-fA-F]{6}/", $bg)){
		//if it's NOT a hex...
		case 0:
			//check to see if it's a complex bg, and set $background as appropriate
			switch(strpos($bg, ".php")){
				//simple bg image stuff
				case false:
					$willdo = 'simpleimage';
					break;
				//complex bg image stuff
				case true:
					//NOTE THE BELOW.  It kinda hides.  It's the file defining $background.
					include_once($bg);
					//the bg could be applied vertically or horizontally...so we check
					switch($layout){
						case "ver":
							$willdo = 'compleximagever';
							break;
						default:
							$willdo = 'compleximagehor';
							break;
					}
					break;
			}
			break;
		//otherwise a color
		default:
			$willdo = 'color';
			break;
	}
}

switch($willdo){

	case 'simpleimage':
		$bgsize = getimagesize($bg);
		$background = imagecreatefromgif($bg);
		imagecopyresampled($img, $background, 0, 0, 0, 0, $width, $height, $bgsize[0], $bgsize[1]);
		break;
	case 'compleximagever':
		//middle stuff first
		$tile = imagecreatefromgif($background['middle']);
		imagesettile($img, $tile);
		imagefill($img, 0, 0, IMG_COLOR_TILED);
		//now the top and bottom
		$topsize = getimagesize($background['left']);
		$top = imagecreatefromgif($background['left']);
		imagecopy ($img, $background['left'], 0, 0, 0, 0, $leftsize[0], $leftsize[1]);
		$bottomsize = getimagesize($background['right']);
		$bottom = imagecreatefromgif($background['right']);
		imagecopy ($img, $background['right'], 0, $height-$rightsize[0], 0, 0, $rightsize[0], $rightsize[1]);
		break;
	case 'compleximagehor':
		//middle stuff first
		$tile = imagecreatefromgif($background['middle']);
		imagesettile($img, $tile);
		imagefill($img, 0, 0, IMG_COLOR_TILED);
		//now the sides
		$leftsize = getimagesize($background['left']);
		$left = imagecreatefromgif($background['left']);
		imagecopy($img, $left, 0, 0, 0, 0, $leftsize[0], $leftsize[1]);
		$rightsize = getimagesize($background['right']);
		$right = imagecreatefromgif($background['right']);
		imagecopy($img, $right, $width-$rightsize[0], 0, 0, 0, $rightsize[0], $rightsize[1]);
		break;
	case 'gradient':
		$grarray = explode("|", $bg);
		$gr1 = $grarray[1];
		if(empty($gr1)){
			$gr1 = '000000';
		}
		$gr2 = $grarray[2];
		if(empty($gr2)){
			$gr2 = 'ffffff';
		}
		$direction = $grarray[3];
		if(empty($direction)){
			$direction = 'vertical';
		}
		$step = $grarray[4];
		if(empty($step)){
			$step = 0;
		}
		//convert hex to RGB
		list($r1,$g1,$b1) = hex2rgb($gr1);
		list($r2,$g2,$b2) = hex2rgb($gr2);
		switch($direction) {
			case 'horizontal':
				$line_numbers = imagesx($img);
				$line_width = imagesy($img);
				break;
			case 'vertical':
				$line_numbers = imagesy($img);
				$line_width = imagesx($img);
				break;
			case 'ellipse':
				$width = imagesx($img);
				$height = imagesy($img);
				$rh=$height>$width?1:$width/$height;
				$rw=$width>$height?1:$height/$width;
				$line_numbers = min($width,$height);
				$center_x = $width/2;
				$center_y = $height/2;
				imagefill($img, 0, 0, imagecolorallocate( $img, $r1, $g1, $b1 ));
				break;
			case 'ellipse2':
				$width = imagesx($img);
				$height = imagesy($img);
				$rh=$height>$width?1:$width/$height;
				$rw=$width>$height?1:$height/$width;
				$line_numbers = sqrt(pow($width,2)+pow($height,2));
				$center_x = $width/2;
				$center_y = $height/2;
				break;
			case 'circle':
				$width = imagesx($img);
				$height = imagesy($img);
				$line_numbers = sqrt(pow($width,2)+pow($height,2));
				$center_x = $width/2;
				$center_y = $height/2;
				$rh = $rw = 1;
				break;
			case 'circle2':
				$width = imagesx($img);
				$height = imagesy($img);
				$line_numbers = min($width,$height);
				$center_x = $width/2;
				$center_y = $height/2;
				$rh = $rw = 1;
				imagefill($img, 0, 0, imagecolorallocate( $img, $r1, $g1, $b1 ));
				break;
			case 'square':
			case 'rectangle':
				$width = imagesx($img);
				$height = imagesy($img);
				$line_numbers = max($width,$height)/2;
				break;
			case 'diamond':
				$width = imagesx($img);
				$height = imagesy($img);
				$rh=$height>$width?1:$width/$height;
				$rw=$width>$height?1:$height/$width;
				$line_numbers = min($width,$height);
				break;
			default:
		}
		
		for ( $i = 0; $i < $line_numbers; $i=$i+1+$step ) {
			// old values :
			$old_r=$r;
			$old_g=$g;
			$old_b=$b;
			// new values :
			$r = ( $r2 - $r1 != 0 ) ? intval( $r1 + ( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
			$g = ( $g2 - $g1 != 0 ) ? intval( $g1 + ( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
			$b = ( $b2 - $b1 != 0 ) ? intval( $b1 + ( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
			// if new values are really new ones, allocate a new color, otherwise reuse previous color.
			// There's a "feature" in imagecolorallocate that makes this function
			// always returns '-1' after 255 colors have been allocated in an image that was created with
			// imagecreate (everything works fine with imagecreatetruecolor)
			if ( "$old_r,$old_g,$old_b" != "$r,$g,$b") 
				$fill = imagecolorallocate( $img, $r, $g, $b );
			switch($direction) {
				case 'vertical':
					imagefilledrectangle($img, 0, $i, $line_width, $i+$this->step, $fill);
					break;
				case 'horizontal':
					imagefilledrectangle( $img, $i, 0, $i+$this->step, $line_width, $fill );
					break;
				case 'ellipse':
				case 'ellipse2':
				case 'circle':
				case 'circle2':
					imagefilledellipse ($img,$center_x, $center_y, ($line_numbers-$i)*$rh, ($line_numbers-$i)*$rw,$fill);
					break;
				case 'square':
				case 'rectangle':
					imagefilledrectangle ($img,$i*$width/$height,$i*$height/$width,$width-($i*$width/$height), $height-($i*$height/$width),$fill);
					break;
				case 'diamond':
					imagefilledpolygon($img, array (
						$width/2, $i*$rw-0.5*$height,
						$i*$rh-0.5*$width, $height/2,
						$width/2,1.5*$height-$i*$rw,
						1.5*$width-$i*$rh, $height/2 ), 4, $fill);
					break;
				default:    
			}        
		}
		break;
	case 'color':
		//convert hex to RGB
		list($RR,$GG,$BB) = hex2rgb($bg);
		//set the color.  if it's transparent...
		if($bg == "facade"){
			$bgcolor = imagecolortransparent($img, imagecolorallocate($img, $RR, $GG, $BB));
		}else{
			$bgcolor = imagecolorallocate($img, $RR, $GG, $BB);
		};
		//fill 'er up
		imagefill($img, 0, 0, $bgcolor);
		break;
}

//centering stuff for text...in the future this can be left or right justified, if we need.
$xcord = ($width/2)-($textwidth/2)-2;
$ycord = ($height/3)+($size/2);

//text color handling stuff...from hex to rgb.
list($tRR,$tGG,$tBB) = hex2rgb($txtcolor);
$ttxtcolor = imagecolorallocate($img, $tRR, $tGG, $tBB);

//insert text
imagettftext($img, $size, $angle, $xcord, $ycord, $ttxtcolor, $font, $imgtext);

//hey, browser, this is a png.
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);

//I know, this should be elsewhere, but whatever.  Happy little function...

function hex2rgb($color) {
	$s = strlen($color) / 3;
	$rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));
	$rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));
	$rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));
	return $rgb;
}

?>

Posted: Wed Dec 20, 2006 3:06 pm
by WoodyNaDobhar
here's a crazy twist...it only happens when I have a black background...