2009-08-18 3 views
1

Ich frage mich, ob jemand weiß, wie ich diese lästigen N über L (ich vermute, Newline bedeutet) aus meinem PHP-String entfernen oder ersetzen, bevor Sie sie auf ein Bild drucken.NL char bei der Verwendung von Bilderfolge in PHP

der Text sieht aus wie this

jede Hilfe sehr geschätzt, wie ich Stunden auf diesem kleinen Problem ...

<?php 
exec('/usr/games/fortune -s', $fortune); 
for($i = 0; $i <= count($fortune); $i++) { 
    $text = "$text $fortune[$i]"; 
} 
$image = imagecreatefrompng("rex.png"); 
$color = imagecolorallocate($image, 0, 0, 0); 
$newtext = wordwrap($text, 35, "\n", true); 
$newertext2 = explode ("\n", $newtext); 
imagestring ($image, 3, 0, 0, $newertext2[0], $color); 
imagestring ($image, 3, 0, 11, $newertext2[1], $color); 
imagestring ($image, 3, 0, 22, $newertext2[2], $color); 
imagestring ($image, 3, 0, 33, $newertext2[3], $color); 
imagestring ($image, 3, 0, 44, $newertext2[4], $color); 
imagestring ($image, 3, 0, 55, $newertext2[5], $color); 
imagestring ($image, 3, 0, 66, $newertext2[6], $color); 
imagestring ($image, 3, 0, 77, $newertext2[7], $color); 
header("Content-type: image/png"); 
imagepng($image); 
?> 

Antwort

2
$newertext2 = explode ("\n", $newtext); 
imagestring ($image, 3, 0, 0, $newertext[0], $color); 

Sie sollten nicht die Buchstaben in newertext2 drucken verschwendet statt Neuertext?

+0

heh, yeah, ich habe diese Änderung gemacht, bevor ich gepostet und noch nicht gespeichert habe. jetzt bearbeiten. – austin

+0

auch, das hat nichts mit dem Problem zu tun, das ich habe. nur ich schaue nicht bevor ich posten. – austin

0

Späte Antwort:

Ich fand heraus, dass jedes Leerzeichen außer der neuen Linie und Raum durch ein „NL“ Symbol ersetzt wird. z.B. das Tabulatorzeichen.

Eine Lösung könnte sein, sie wie folgt zu ersetzen:

preg_replace all spaces

Natürlich kann man von einem Leerzeichen anstelle eines Unterstrich ersetzen sollte;)

0

Hier ist eine Funktion, schreiben Sie den Inhalt von Whitespaces respektieren.

Diese Funktion überschreitet nicht die Grenzen der Bildgröße. Wenn eine neue Zeile nicht vollständig hinzugefügt werden kann, wird sie in kleinsten Zeilen aufgelöst.

/** 
* @author Booteille 
* 
* @param resource $image 
* @param int $font 
* @param int $x 
* @param int $y 
* @param string $string 
* @param int $color 
*/ 
function whitespaces_imagestring($image, $font, $x, $y, $string, $color) { 
    $font_height = imagefontheight($font); 
    $font_width = imagefontwidth($font); 
    $image_height = imagesy($image); 
    $image_width = imagesx($image); 
    $max_characters = (int) ($image_width - $x)/$font_width ; 
    $next_offset_y = $y; 

    for($i = 0, $exploded_string = explode("\n", $string), $i_count = count($exploded_string); $i < $i_count; $i++) { 
     $exploded_wrapped_string = explode("\n", wordwrap(str_replace("\t", " ", $exploded_string[$i]), $max_characters, "\n")); 

     for($j = 0, $j_count = count($exploded_wrapped_string); $j < $j_count; $j++) { 
      imagestring($image, $font, $x, $next_offset_y, $exploded_wrapped_string[$j], $color); 
      $next_offset_y += $font_height; 

      if($next_offset_y >= $image_height - $y) { 
       return; 
      } 
     } 
    } 
} 
Verwandte Themen