All pastes #417144 Raw Edit

GD Problem - Class

public php v1 · immutable
#417144 ·published 2007-03-30 18:44 UTC
rendered paste body
<?phpputenv('GDFONTPATH=/usr/share/fonts/msfonts/');class GD{	    function GD(){         }   	function createColorFromHex($ctnImg,$hexColor){	  	$tmpColor = (substr($hexColor,0,1) == "#") ? substr($hexColor,1) : $hexColor;	  	$red=hexdec(substr($tmpColor,0,2));	  	$green=hexdec(substr($tmpColor,2,2));	  	$blue=hexdec(substr($tmpColor,4,2));	  	$rtnColor = imagecolorallocate($ctnImg, $red, $green, $blue);	  	return $rtnColor;	}	function createImageBox($ctnImg,$boxObj,$imgPath){		$logo_img = @imagecreatefrompng($imgPath);		if ($logo_img){			list($img_0,$img_1,$img_2,$img_3)=getImageSize($imgPath);			$logoW = $img_0;			$logoH = $img_1;			if ($logoW > $boxObj['w'] || $logoH > $boxObj['h']){				imagecopyresized ( $ctnImg, $logo_img, $boxObj['x'], $boxObj['y'], 0, 0, $boxObj['w'], $boxObj['h'], $logoW, $logoH);			}else{				imagecopy ( $ctnImg, $logo_img, $boxObj['x'], $boxObj['y'], 0, 0, $boxObj['w'], $boxObj['h']);			}		}	}	function createBox($ctnImg, $obj){		$grey = imagecolorallocate($ctnImg, 150, 150, 150);		//echo "x[".$obj['x']."] y[".$obj['y']."] w[".$obj['w']."] h[".$obj['h']."]<br>";		imagefilledrectangle($ctnImg, $obj['x'], $obj['y'], ($obj['x']+$obj['w']), ($obj['y']+$obj['h']), $grey);		//imagettftext ( $ctnImg, 1, 0, $obj['x'], $obj['y'], $black, getFontFile('arial'), "Yeah");		return;	}	function createTextBox($ctnImg,$extParamArr){		$textArr=array();		$paramArr=array();		$paramArr['width']=(isset($extParamArr['width'])) ? $extParamArr['width']: 0;		$paramArr['height']=(isset($extParamArr['height'])) ? $extParamArr['height']: 0;		$paramArr['x']=(isset($extParamArr['x'])) ? $extParamArr['x']: 0;		$paramArr['y']=(isset($extParamArr['y'])) ? $extParamArr['y']: 0;		$paramArr['font-family']=(isset($extParamArr['font-family'])) ? strToLower($extParamArr['font-family']) : '';		$paramArr['font-size']=(isset($extParamArr['font-size'])) ? $extParamArr['font-size']: '';		$paramArr['font-weight']=(isset($extParamArr['font-weight'])) ? $extParamArr['font-weight']: 'normal';		$paramArr['font-color']=(isset($extParamArr['font-color'])) ? $extParamArr['font-color']: '#000000';		$paramArr['text']=(isset($extParamArr['text'])) ? $extParamArr['text']: '';		$paramArr['align']=(isset($extParamArr['align'])) ? $extParamArr['align']: 'left';		$paramArr['valign']=(isset($extParamArr['valign'])) ? $extParamArr['valign']: 'middle';		$paramArr['bgImage']=(isset($extParamArr['bgImage'])) ? $extParamArr['bgImage']: '';		$paramArr['bgColor']=(isset($extParamArr['bgColor'])) ? $extParamArr['bgColor']: '';		$paramArr['wordWrap']=(isset($extParamArr['wordWrap'])) ? $extParamArr['wordWrap']: false;		$paramArr['text_maxLen']=(isset($extParamArr['text_maxLen'])) ? intval($extParamArr['text_maxLen']): 0;		$paramArr['text_maxLenEnd']=(isset($extParamArr['text_maxLenEnd'])) ? $extParamArr['text_maxLenEnd']: '';		$paramArr['padding-top']=(isset($extParamArr['padding-top'])) ? $extParamArr['padding-top']: 0;		$paramArr['padding-bottom']=(isset($extParamArr['padding-bottom'])) ? $extParamArr['padding-bottom']: 0;		$paramArr['padding-left']=(isset($extParamArr['padding-left'])) ? $extParamArr['padding-left']: 0;		$paramArr['padding-right']=(isset($extParamArr['padding-right'])) ? $extParamArr['padding-right']: 0;				$fontFamily=$this->getFontFileFromFamily($paramArr['font-family'],$paramArr['font-weight']);		//echo "len>".strlen($paramArr['text'])."\n";		if ($paramArr['text_maxLen'] > 0 && strlen($paramArr['text']) > $paramArr['text_maxLen']){			$paramArr['text']=substr($paramArr['text'],0,$paramArr['text_maxLen']);			if ($paramArr['text_maxLenEnd']) $paramArr['text'].=$paramArr['text_maxLenEnd'];		}		if ($paramArr['wordWrap']){			$tmpText=str_replace(array('\r','\n'),array('',' '),$paramArr['text']);			$BBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, $tmpText);			$textWidth = max($BBox[2], $BBox[4]) - min($BBox[0], $BBox[6]);			$textHeight = max($BBox[1], $BBox[3]) - min($BBox[5], $BBox[7]);			if ($textWidth > ($paramArr['width'] - $paramArr['padding-left'] - $paramArr['padding-right'])){				$testText=$tmpText;				$lineWidth=$textWidth;				while($lineWidth >= ($paramArr['width'] - $paramArr['padding-left'] - $paramArr['padding-right'])){					$testText=substr($testText,0,strlen($testText)-1);					$BBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, $testText);					$lineWidth = max($BBox[2], $BBox[4]) - min($BBox[0], $BBox[6]);					$lineLen = strlen($testText);				}				$testText=$tmpText;				while(strlen($testText) > 0){					$t1=substr($testText,0,$lineLen);					if (strrpos($t1," ") && strlen($t1) == $lineLen) $t1=substr($t1,0,strrpos($t1," ")+1);					else $t1=substr($t1,0);					//echo "t1>".$t1.":".strlen($t1)."\n";					$textArr[]=array('text'=> $t1);					$testText=substr($testText,strlen($t1));					//echo "testText>".$testText."\n";				}			}else{				$textArr[]=array('text'=> $tmpText);			}		}else{			if (strpos($paramArr['text'],'\n') > -1){				$tmpArr=explode('\n',trim($paramArr['text']));				foreach($tmpArr as $k => $v){					if (strlen(trim($v)) > 0) $textArr[]=array('text'=> trim($v));				}			}else{				$textArr[0]=array('text'=> trim($paramArr['text']));			}		}		$nbOfLines=count($textArr);		$testBBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, 'AMPIZQgjlyqfpi|{(~!@^.,"');		$lineHeight = max($testBBox[1], $testBBox[3]) - min($testBBox[5], $testBBox[7]);		/*		for ($i=0;$i < $nbOfLines;$i++){			$BBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, $textArr[$i]['text']);			$textWidth = max($BBox[2], $BBox[4]) - min($BBox[0], $BBox[6]);			$textHeight = max($BBox[1], $BBox[3]) - min($BBox[5], $BBox[7]);			while($textWidth >= ($paramArr['width'] - $paramArr['padding-left'] - $paramArr['padding-right']) || $textHeight >= ($lineHeight - $paramArr['padding-top'] - $paramArr['padding-bottom'])){				$paramArr['font-size']-=1;				$BBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, $textArr[$i]['text']);				$textWidth = max($BBox[2], $BBox[4]) - min($BBox[0], $BBox[6]);				$textHeight = max($BBox[1], $BBox[3]) - min($BBox[5], $BBox[7]);			}		}		*/		$color = $this->createColorFromHex($ctnImg,$paramArr['font-color']);		while (($nbOfLines*$lineHeight) > ($paramArr['height']-$paramArr['padding-bottom']-$paramArr['padding-top'])){			//echo "[".$nbOfLines."]";			$nbOfLines--;		}		//echo "[".$nbOfLines."] allLineH:".($nbOfLines*$lineHeight)." innerH:".($paramArr['height']-$paramArr['padding-bottom']-$paramArr['padding-top']);		$xAxisCorrection=floor($lineHeight/16);		$yAxisCorrection=floor($lineHeight/4);				$yAxisBegin=intval(($paramArr['height'] - ($lineHeight * $nbOfLines))/2) - $yAxisCorrection;		if ($paramArr['padding-top'] > 0 && $yAxisBegin < $paramArr['padding-top']) $yAxisBegin = $paramArr['padding-top'];		//else if ($yAxisBegin < 0) $yAxisBegin = 0;						for ($i=0;$i < $nbOfLines;$i++){			$BBox = ImageTTFBBox ( $paramArr['font-size'], 0, $fontFamily, $textArr[$i]['text']);			$textWidth = max($BBox[2], $BBox[4]) - min($BBox[0], $BBox[6]);			$textHeight = max($BBox[1], $BBox[3]) - min($BBox[5], $BBox[7]);			if ($paramArr['align'] == 'center'){				$textArr[$i]['x'] = intval((($paramArr['width'] - $textWidth)/2)+$paramArr['x'])-$xAxisCorrection; // -2 is a manual correction			}else if ($paramArr['align'] == 'right'){				$textArr[$i]['x'] = intval(($paramArr['width'] - $textWidth) + $paramArr['x']) - $paramArr['padding-right']-$xAxisCorrection; // -2 is a manual correction			}else{				$textArr[$i]['x'] = intval($paramArr['x']) + $paramArr['padding-left']-$xAxisCorrection; // -2 is a manual correction			}						$textArr[$i]['y'] = $yAxisBegin + ($lineHeight * $i) + $lineHeight + $paramArr['y'];			//echo ">>>>>>> lineHeight:".$lineHeight.", yAxisBegin:".$yAxisBegin.", nbOfLines:".$nbOfLines.", height:".$paramArr['height'].", y:".$textArr[$i]['y']."<br>";			imagettftext ($ctnImg, $paramArr['font-size'], 0, $textArr[$i]['x'], $textArr[$i]['y'], $color, $fontFamily, $textArr[$i]['text']);		}	}	function getFontFileFromFamily($family,$weight){		$fontFile="";		switch(strToLower($family)){			case 'arial':				switch(strToLower($weight)){					case 'bold':						$fontFile = 'arialbd';						break;					case 'italic':						$fontFile = 'ariali';						break;					case 'bold,italic':						$fontFile = 'arialbi';						break;					default: $fontFile = 'arial';				}				break;			case 'courier new':				switch(strToLower($weight)){					case 'bold':						$fontFile = 'courbd';						break;					case 'italic':						$fontFile = 'couri';						break;					case 'bold,italic':						$fontFile = 'courbi';						break;					default: $fontFile = 'cour';				}				break;			case 'times new roman':				switch(strToLower($weight)){					case 'bold':						$fontFile = 'timesbd';						break;					case 'italic':						$fontFile = 'timesi';						break;					case 'bold,italic':						$fontFile = 'timesbi';						break;					default: $fontFile = 'times';				}				break;			case 'verdana':				switch(strToLower($weight)){					case 'bold':						$fontFile = 'verdanabd';						break;					case 'italic':						$fontFile = 'verdanai';						break;					case 'bold,italic':						$fontFile = 'verdanaz';						break;					default: $fontFile = 'verdana';				}				break;			default: $fontFile = 'arial';		}		return $fontFile;	}	function handleError($errCatch){		die($errCatch);	}}?>