我正在研究一种解决方案,以自动生成包含复杂脚本的大型文档的每个单词的图像(高棉语为UTF-8)。我发现Pango和Cairo可以正确地渲染高棉语。我不是一个程序员,所以我开始使用Pango和Cairo的PHP版本。但我不知道如何分解字符串并自动为每个单词生成图像。单词之间没有“真实”空格,只有Unicode字符U + 0200B(零宽度空格)。
有人愿意帮助我吗?
这是我目前使用的代码,它输出整个字符串:
<?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");
?>
答案 0 :(得分:1)
我用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;
}
?>