Geben Sie in PHP mit Pango und Kairo ein Bild für jedes Wort und nicht für die gesamte Zeichenfolge aus

StackOverflow https://stackoverflow.com/questions/7831841

  •  27-10-2019
  •  | 
  •  

Frage

Ich arbeite an einer Lösung, um automatisch ein Bild von jedem Wort eines großen Dokuments zu erstellen, das ein komplexes Skript enthält (Khmer in UTF-8).Ich habe Pango und Kairo gefunden, die Khmer korrekt rendern können.Ich bin kein großer Programmierer, also habe ich mit den PHP-Versionen von Pango und Kairo angefangen.Ich bin mir jedoch nicht sicher, wie ich eine Zeichenfolge auflösen und für jedes Wort automatisch ein Bild erstellen soll.Es gibt keine "echten" Leerzeichen zwischen Wörtern, nur das Unicode-Zeichen U + 0200B (ein Leerzeichen mit der Breite Null).

Wäre jemand bereit, mir zu helfen?

Hier ist der Code, den ich derzeit verwende und der die gesamte Zeichenfolge ausgibt:

<?php
header("Content-Type: image/png");
/* Make a 300x300px image surface */
$s = new CairoImageSurface(CairoFormat::ARGB32, 300, 300);
$c = new CairoContext($s);

/* Set the background to white */
$c->setSourceRGB(1, 1, 1);
$c->paint();

/* Let's draw using black 'ink' */
$c->setSourceRGB(0, 0, 0);

/* Make a Pango layout, set the font, then set the layout size */
$l = new PangoLayout($c);
$desc = new PangoFontDescription("KhmerOS Regular 28");
$l->setFontDescription($desc);
$l->setWidth(250 * PANGO_SCALE);

/* Here is the text */
$l->setMarkup("កាល​ដើម​ដំបូង​ឡើយ ព្រះ​បាន​បង្កើត​ផ្ទៃ​មេឃ និង​ផែនដី។");

/* Draw the layout on the surface */
$l->showLayout($c);

/* Output the PNG to the browser */
$s->writeToPng("php://output");
?>

War es hilfreich?

Lösung

Ich habe es mit foreach herausgefunden:

<?php
//header("Content-Type: image/png");
$str = "កាល​ដើម​ដំបូង​ឡើយ ព្រះ​បាន​បង្កើត​ផ្ទៃ​មេឃ និង​ផែនដី។";
//$words = explode('​', $str);
$words = preg_split('/ |​/', $str);

$i=1;
foreach($words as $word) {
/* Make a 300x300px image surface */
$s = new CairoImageSurface(CairoFormat::ARGB32, 300, 300);
$c = new CairoContext($s);

/* Set the background to white */
$c->setSourceRGB(1, 1, 1);
$c->paint();

/* Let's draw using black 'ink' */
$c->setSourceRGB(0, 0, 0);

/* Make a Pango layout, set the font, then set the layout size */
$l = new PangoLayout($c);
$desc = new PangoFontDescription("KhmerOS Regular 28");
$l->setFontDescription($desc);
$l->setWidth(250 * PANGO_SCALE);


/* Here, we use Pango markup to make part of the text bold */



$i++;

$l->setMarkup($word);

/* Draw the layout on the surface */
$l->showLayout($c);

/* Output the PNG to the browser */
//$s->writeToPng("php://output");
$s->writeToPng(dirname(__FILE__) . '/test'.$i.'.png');
echo $img = "<img src=\"test".$i.".png\">";
echo $i;
}
?>

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top