Sortie d'une image pour chaque mot plutôt que la chaîne complète en PHP utilisant Pango et Le Caire

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

  •  27-10-2019
  •  | 
  •  

Question

Je travaille sur une solution pour faire automatiquement une image de chaque mot d'un document volumineux qui contient un script complexe (khmer en UTF-8). Je l'ai trouvé Pango et Le Caire qui peut rendre correctement Khmer. Je ne suis pas beaucoup plus d'un programmeur, alors j'ai commencé avec les versions PHP de Pango et au Caire. Mais je ne suis pas sûr de savoir comment briser une chaîne et produire une image pour chaque mot automatiquement. Il n'y a pas d'espace « vrais » entre les mots, juste le caractère Unicode U + 0200B (un espace zéro largeur).

Quelqu'un prêt à me aider?

Voici le code que je suis actuellement en utilisant que les sorties de la chaîne entière:

<?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");
?>
Était-ce utile?

La solution

Je me suis dit qu'il l'aide foreach:

<?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;
}
?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top