Produza uma imagem para cada palavra em vez de toda a string em PHP usando Pango e Cairo

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

  •  27-10-2019
  •  | 
  •  

Pergunta

Estou trabalhando em uma solução para fazer automaticamente uma imagem de cada palavra de um grande documento que contém um script complexo (Khmer em UTF-8).Encontrei Pango e Cairo que podem renderizar Khmer corretamente.Não sou muito programador, então comecei com as versões PHP do Pango e Cairo.Mas não tenho certeza de como quebrar uma string e produzir uma imagem para cada palavra automaticamente.Não há espaços "reais" entre as palavras, apenas o caractere Unicode U + 0200B (um espaço de largura zero).

Alguém estaria disposto a me ajudar?

Este é o código que estou usando atualmente e que gera a string inteira:

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

Solução

Eu descobri 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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top