Genere una imagen para cada palabra en lugar de la cadena completa en PHP usando Pango y Cairo

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

  •  27-10-2019
  •  | 
  •  

Pregunta

Estoy trabajando en una solución para hacer automáticamente una imagen de cada palabra de un documento grande que contiene un script complejo (Khmer en UTF-8).Encontré Pango y Cairo que pueden representar Khmer correctamente.No soy muy programador, así que empecé con las versiones PHP de Pango y Cairo.Pero no estoy seguro de cómo dividir una cadena y producir una imagen para cada palabra automáticamente.No hay espacios "reales" entre palabras, solo el carácter Unicode U + 0200B (un espacio de ancho cero).

¿Alguien estaría dispuesto a ayudarme?

Aquí está el código que estoy usando actualmente que genera la cadena completa:

<?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");
?>
¿Fue útil?

Solución

Lo descubrí usando 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;
}
?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top