• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Viele hassen ihn, manche schwören auf ihn, wir aber möchten unbedingt sehen, welche Bilder Ihr vor Eurem geistigen Auge bzw. vor der Linse Eures iPhone oder iPad sehen könnt, wenn Ihr dieses Wort hört oder lest. Macht mit und beteiligt Euch an unserem Frühjahrsputz ---> Klick

dynamisch buttons generieren

ma.buso

Châtaigne du Léman
Registriert
16.04.05
Beiträge
820
hallo männer,

ich habe ein problem mit einem script zum erstellen dynamischer bilddateien.

Code:
<?

Header("Content-type: image/png");

if(isset($catid) && isset($lang)) {

$result = $db->query("SELECT name_".$lang." FROM gt_categories WHERE cat_id='".$catid."' LIMIT 1");
$result = $db->fetch_array($result);
$msg=trim($result['name_'.$lang]);

if(strlen($msg)>29){

  $msg = substr($msg,0,26)."...";

}

}

// $msg1 = strtoupper($msg);
if(isset($msg)) $msg1=$msg;

class textPNG {
	var $font = 'LTe50541.ttf'; //default font. directory relative to script directory.
	var $msg1 = ""; // default text to display.
	var $size = 11;
	var $rot = 0; // rotation in degrees.
	var $pad = 0; // padding.
	var $transparent = 1; // transparency set to on.
	var $red = 6; // white text...
	var $grn = 24;
	var $blu = 92;
	var $bg_red = 255; // on black background.
	var $bg_grn = 255;
	var $bg_blu = 255;

function draw() {
	$width = 0;
	$height = 0;
	$offset_x = 0;
	$offset_y = 0;
	$bounds = array();
	$image = "";

	// determine font height.
	$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
	if ($this->rot < 0) {
		$font_height = abs($bounds[7]-$bounds[1]);
	} else if ($this->rot > 0) {
		$font_height = abs($bounds[1]-$bounds[7]);
	} else {
		$font_height = abs($bounds[7]-$bounds[1]);
	}

	// determine bounding box.
	$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
	if ($this->rot < 0) {
		$width = abs($bounds[4]-$bounds[0]);
		$height = abs($bounds[3]-$bounds[7]);
		$offset_y = $font_height;
		$offset_x = 0;

	} else if ($this->rot > 0) {
		$width = abs($bounds[2]-$bounds[6]);
		$height = abs($bounds[1]-$bounds[5]);
		$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
		$offset_x = abs($bounds[0]-$bounds[6]);

	} else {
		$width = abs($bounds[4]-$bounds[6]);
		$height = abs($bounds[7]-$bounds[1]);
		$offset_y = $font_height;;
		$offset_x = 0;
	}

	$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);

	$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
	$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);

	if ($this->transparent) ImageColorTransparent($image, $background);
	ImageInterlace($image, false);

	// render it.
	ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);

	// output PNG object.
	imagePNG($image);
}
}

$text = new textPNG;

if (isset($msg1)) $text->msg = $msg1; // text to display
if (isset($font)) $text->font = $font; // font to use (include directory if needed).
if (isset($size)) $text->size = $size; // size in points
if (isset($rot)) $text->rot = $rot; // rotation
if (isset($pad)) $text->pad = $pad; // padding in pixels around text.
if (isset($red)) $text->red = $red; // text color
if (isset($grn)) $text->grn = $grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // background color.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
if (isset($tr)) $text->transparent = $tr; // transparency flag (boolean).

$text->draw();
?>

das script ist an sich ok, ich habe aber folgendes problem: ich will damit buttons für eine navigation generieren lassen und die dürfen nun mal nur eine bestimmte breite haben.
nun habe ich mit ewigem rumprobiere festgelegt dass alle menüpunkte länger als 29 zeichen auf 27 gekürzt werden und drei punkte hinten dran kommen...
so überschreitet keiner der buttons die maximal zulässige länge. nun gut, nun ist es aber so, dass manche buttons abgekürzt werden, obwohl da noch fünf zeichen drin wären - was an den breiten der zeichen liegt.

hat vielleicht jemand eine idee, wie man ihn dazu bringt, wirklich jedem button so viele zeichen wie möglich zu verpassen?
 
Zuletzt bearbeitet: