$width) { $maxwidth = $width; // having the margins take up more space than the entire // image makes no sense either } if ($height < 0) { $height = 100; } $maxheight = $height - $border_top - $border_bottom; if ($maxheight < 0 || $maxheight > $height) { $maxheight = $height; } $sl = strlen($test); $line = ''; $oldline = ''; $word = ''; $token = ''; $cy = $border_top + $fontsize + $linespace; // Y cursor $imi = 1; // image index $height = 0; $lexcc = 0; do { $token = mylex(); if ($token == 'eol') { $oldline .= $word; $cy = flushline(); $oldline = ''; } elseif ($token == 'word') { $line .= $word; // measure if the current line fits horizontally $box = imageftbbox($fontsize, 0, $font, $line); if (($box[2] - $box[0]) > $maxwidth) { // print the last line that fit, move the cursor, // and reset the line variable $cy = flushline(); myless($word); if ($cy > ($maxheight - $border_bottom)) { imagepng($im, "{$img_base}{$imi}.png"); imagedestroy($im); $estimate = ceil(($inputl / $lexcc) * $imi); print "page $imi, of $estimate estimated pages.\n"; $imi++; $im = @imagecreatetruecolor(240, 160) or die("Cannot Initialize new GD image stream"); imagefill($im, 0, 0, $bgc); $cy = 20; } } else { $oldline = $line; } } elseif($token == 'eof') { print "%%"; $cy = flushline(); } } while ($token != 'eof'); imagepng($im, "{$img_base}{$imi}.png"); imagedestroy($im); function mylex() { global $lexcc, $word, $input, $inputl; $i = $lexcc; $out = false; $token = ''; $word = ''; if ($i < $inputl) { do { $a = substr($input, $i, 1); if ($a == "\n" || $a == "\r") { if ($i < $inputl) { $b = substr($input, $i+1, 1); if ($b == "\n" && $a == "\r") { // DOS style newline $i++; } } $token = 'eol'; $word .= ' '; } elseif ($a == ' ') { $token = 'word'; $word .= ' '; } else { $word .= $a; } $i++; if ($i >= $inputl) { $token = 'eof'; } } while ($token == ''); $lexcc = $i; return $token; } else { return 'eof'; } } function flushline () { global $im, $line, $oldline, $fontsize, $font, $cy, $fgc; global $bordertop, $borderleft, $linespace; $line = ''; $box = imageftbbox($fontsize, 0, $font, $oldline); imagettftext($im, $fontsize, 0, 10, $cy, $fgc, $font, $oldline); // $height = $box[1] - $box[5]; $newcy = $cy + $fontsize + $linespace; return $newcy; } function myless($a) { global $lexcc; // push the last word back onto the stack $b = strlen($a); $lexcc -= $b; if ($lexcc < 0) { // This should not be, but we'll let it slide for now print "Lexcc < 0 ?!"; $lexcc = 0; } } function getred($a) { $b = hexdec($a); $c = $b >> 16; return $c; } function getgreen($a) { $b = hexdec($a); $c = $b & 65280; // 65280 = #00ff00 $d = $c >> 8; return $d; } function getblue($a) { $b = hexdec($a); $c = $b & 255; // 255 = #0000ff return $c; } ?>