Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Height with PHP

Status
Not open for further replies.

BB101

Programmer
May 23, 2001
337
GB
I need to be able to get the height of some text in php dependant on font-family font-size and width, can anyone help?

I don't really care if the height is in pixels or characters but it must be fast!

The solution im trying to provide is this:
I have two columns of text, data is pulled from a database, into the columns. The columns height are independant from each other, but when the items in the left table are far heigher that the right i end up with a large undesirable whitespace at the bottom which could be used to put another piece of text in.

Anyone?

--BB
 
But I want the answer to be PHP, I need to know in the PHP wether to stick another piece of text on the end of the column

--BB
 
PHP doesn't do any kind of publishing layout.

PHP produces output streams that, when sent to a web browser, can be interpreted as the layout for a page. The question of "What should my output stream to do such-and-such look like?" is an HTML question and as such should be asked in the HTML forum.

The only part of your question that has to do with PHP would be the question, "How do I get PHP to output this HTML stream I just found out about in the HTML forum?" The answer to that question would be, "With print or echo statements."



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
But getting the height of the element in HTML is totally useless to me because the page has already been parsed and rendered.

I dont want the answer in Javascript. I want to know the number of text rows given a font, size, and width, in PHP. Surely someone has written something to do this?

--BB
 
If the page has already been produced, sent to the browser, and rendered, your PHP script isn't even running any more.

The only program languages available to you are the client-side ones.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I know this, this is why its no good that I get the answer in Javascript.

I have been toying with the idea of using the PDF functions to load the font and to get dimensions of text, not sure how fast it would be though.

--BB
 
Balancing text in columns in a page layout program like InDesign and Quark requires manual intervention. Web layout is no easier.

The only thing I can imagine you doing is splitting the database output string in half - - but you will still have uneven columns if the database output has varying sizes of fonts.

As a design choice, two column screen reading is a bad idea since the viewer must scroll to the bottom of the page and then back up to read the second column. It is best to put everything in one column of moderate width. Beware: extremely wide columns introduce more readability problems.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
I'm sure its possible, i have written text wrapping tools for the pdf library before. I just need some way of working out the width of the text, like pdf_stringwidth.

Supposing the above is possible; my next question, would it be possible to load a windows ttf on a linux/bsd box, or do the different compiles for pdflib lack the shared functionality?

--BB
 
PHP doesn't do any kind of publishing layout.
- - - not yet [bigsmile]. Remember that just a few years ago we didn't have embedded systems in refridgerators and toasters. Now I can get a golden brown slice of bread with a few lines of PHP.

Yes, it must be possible through PDF functions. My mind just goes fuzzy thinking about how to do all that calculation to produce the PDF. Determine string width, break into columns, count lines, sum leading of all lines, divide total leading by number of lines to get average leading, chop that into the number of columns... Do you align this all to a baseline grid? - - - this seems too complicated and makes my head hurt.

The idea is intriguing not just for PDF generation: the concept would make a great applescript/VBscript/javascript for InDesign.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
Managed to get something working with 99% of possibilities:

Code:
function textlines($str) {
  $width = 388;
  $font = realpath("priv/verdana.ttf");
  $size = 13;

  $splits = array(" ", "-");
  $ssplits = $splits;
  array_walk($ssplits, "qArr");

  $im = imagecreate(1, 1);
  $black = imagecolorallocate ($im, 0, 0, 0);
  $white = imagecolorallocate ($im, 255, 255, 255);

  $ss = getSplits($str, $splits);

  $words = preg_split("#(".implode("|", $ssplits).")#", $str, -1, PREG_SPLIT_NO_EMPTY);
  $cumstr = "";

  $i = 0;
  $lines = 0;
  foreach ($words as $s) {
    if (isset($ss[$i])) {
      $schar = $ss[$i];
    } else {
      $schar = "";
    }
    $chunk = $s.$schar;
    $w = imagettfbbox($size, 0, $font, trim($cumstr.$chunk));
    $w = $w[2];
    if ($w > $width) {
      $lines++;
      $cumstr = $chunk;
    } else {
      $cumstr .= $chunk;
    }
    $i++;
  }
  if ($cumstr != "") {
    if (isset($ss[$i])) {
      $schar = $ss[$i];
    } else {
      $schar = "";
    }
    $lines++;
  }
  return $lines;
}

function getSplits($str, $splits) {
  $r = array();
  $arr = array();
  foreach ($splits as $c) {
    $x = -1;
    do {
      $x = strpos($str, $c, $x+1);
      if ($x !== FALSE) {
        $r[$x] = $c;
      }
    } while ($x !== FALSE);
  }
  ksort($r);
  $ret = array();
  $l = false;
  $i = 0;
  foreach ($r as $k => $v) {
    if ($i != 0 && ($l+1) == $k) {
      $ret[$i-1] .= $v;
    } else {
      $ret[$i++] = $v;
    }
    $l = $k;
  }
  return $ret;
}

function qArr(&$v, $k) {
  $v = preg_quote($v);
}

Hope this helps someone.

--BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top